/
v4l2-enc.c
1825 lines (1491 loc) · 46 KB
/
v4l2-enc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2010 Bluecherry, LLC www.bluecherrydvr.com
* Copyright (C) 2010 Ben Collins <bcollins@bluecherry.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-common.h>
#include <media/videobuf-dma-sg.h>
#include "solo6x10.h"
#include "tw28.h"
#include "jpeg.h"
#define MIN_VID_BUFFERS 4
#define FRAME_BUF_SIZE (128 * 1024)
#define MP4_QS 16
static int solo_enc_thread(void *data);
extern unsigned video_nr;
struct solo_enc_fh {
struct solo_enc_dev *enc;
u32 fmt;
u16 rd_idx;
u8 enc_on;
enum solo_enc_types type;
struct videobuf_queue vidq;
struct list_head vidq_active;
struct task_struct *kthread;
struct p2m_desc desc[SOLO_NR_P2M_DESC];
};
static const u32 solo_user_ctrls[] = {
V4L2_CID_BRIGHTNESS,
V4L2_CID_CONTRAST,
V4L2_CID_SATURATION,
V4L2_CID_HUE,
V4L2_CID_SHARPNESS,
0
};
static const u32 solo_mpeg_ctrls[] = {
V4L2_CID_MPEG_VIDEO_ENCODING,
V4L2_CID_MPEG_VIDEO_GOP_SIZE,
0
};
static const u32 solo_private_ctrls[] = {
V4L2_CID_MOTION_ENABLE,
V4L2_CID_MOTION_THRESHOLD,
0
};
static const u32 solo_fmtx_ctrls[] = {
V4L2_CID_RDS_TX_RADIO_TEXT,
0
};
static const u32 *solo_ctrl_classes[] = {
solo_user_ctrls,
solo_mpeg_ctrls,
solo_fmtx_ctrls,
solo_private_ctrls,
NULL
};
static int solo_is_motion_on(struct solo_enc_dev *solo_enc)
{
struct solo_dev *solo_dev = solo_enc->solo_dev;
u8 ch = solo_enc->ch;
if (solo_dev->motion_mask & (1 << ch))
return 1;
return 0;
}
static void solo_motion_toggle(struct solo_enc_dev *solo_enc, int on)
{
struct solo_dev *solo_dev = solo_enc->solo_dev;
u8 ch = solo_enc->ch;
spin_lock(&solo_enc->lock);
if (on)
solo_dev->motion_mask |= (1 << ch);
else
solo_dev->motion_mask &= ~(1 << ch);
/* Do this regardless of if we are turning on or off */
solo_reg_write(solo_enc->solo_dev, SOLO_VI_MOT_CLEAR,
1 << solo_enc->ch);
solo_enc->motion_detected = 0;
solo_reg_write(solo_dev, SOLO_VI_MOT_ADR,
SOLO_VI_MOTION_EN(solo_dev->motion_mask) |
(SOLO_MOTION_EXT_ADDR(solo_dev) >> 16));
if (solo_dev->motion_mask)
solo_irq_on(solo_dev, SOLO_IRQ_MOTION);
else
solo_irq_off(solo_dev, SOLO_IRQ_MOTION);
spin_unlock(&solo_enc->lock);
}
/* Should be called with solo_enc->lock held */
static void solo_update_mode(struct solo_enc_dev *solo_enc)
{
struct solo_dev *solo_dev = solo_enc->solo_dev;
assert_spin_locked(&solo_enc->lock);
solo_enc->interlaced = (solo_enc->mode & 0x08) ? 1 : 0;
solo_enc->bw_weight = max(solo_dev->fps / solo_enc->interval, 1);
switch (solo_enc->mode) {
case SOLO_ENC_MODE_CIF:
solo_enc->width = solo_dev->video_hsize >> 1;
solo_enc->height = solo_dev->video_vsize;
break;
case SOLO_ENC_MODE_D1:
solo_enc->width = solo_dev->video_hsize;
solo_enc->height = solo_dev->video_vsize << 1;
solo_enc->bw_weight <<= 2;
break;
default:
WARN(1, "mode is unknown\n");
}
}
/* Should be called with solo_enc->lock held */
static int solo_enc_on(struct solo_enc_fh *fh)
{
struct solo_enc_dev *solo_enc = fh->enc;
u8 ch = solo_enc->ch;
struct solo_dev *solo_dev = solo_enc->solo_dev;
u8 interval;
assert_spin_locked(&solo_enc->lock);
if (fh->enc_on)
return 0;
solo_update_mode(solo_enc);
/* Make sure to bw check on first reader */
if (!atomic_read(&solo_enc->readers)) {
if (solo_enc->bw_weight > solo_dev->enc_bw_remain)
return -EBUSY;
else
solo_dev->enc_bw_remain -= solo_enc->bw_weight;
}
fh->enc_on = 1;
fh->rd_idx = solo_enc->solo_dev->enc_wr_idx;
if (fh->type == SOLO_ENC_TYPE_EXT)
solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(ch), 1);
if (atomic_inc_return(&solo_enc->readers) > 1)
return 0;
/* Disable all encoding for this channel */
solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), 0);
/* Common for both std and ext encoding */
solo_reg_write(solo_dev, SOLO_VE_CH_INTL(ch),
solo_enc->interlaced ? 1 : 0);
if (solo_enc->interlaced)
interval = solo_enc->interval - 1;
else
interval = solo_enc->interval;
/* Standard encoding only */
solo_reg_write(solo_dev, SOLO_VE_CH_GOP(ch), solo_enc->gop);
solo_reg_write(solo_dev, SOLO_VE_CH_QP(ch), solo_enc->qp);
solo_reg_write(solo_dev, SOLO_CAP_CH_INTV(ch), interval);
/* Extended encoding only */
solo_reg_write(solo_dev, SOLO_VE_CH_GOP_E(ch), solo_enc->gop);
solo_reg_write(solo_dev, SOLO_VE_CH_QP_E(ch), solo_enc->qp);
solo_reg_write(solo_dev, SOLO_CAP_CH_INTV_E(ch), interval);
/* Enables the standard encoder */
solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), solo_enc->mode);
/* Settle down Beavis... */
mdelay(10);
return 0;
}
static void solo_enc_off(struct solo_enc_fh *fh)
{
struct solo_enc_dev *solo_enc = fh->enc;
struct solo_dev *solo_dev = solo_enc->solo_dev;
if (!fh->enc_on)
return;
if (fh->kthread) {
kthread_stop(fh->kthread);
fh->kthread = NULL;
}
solo_dev->enc_bw_remain += solo_enc->bw_weight;
fh->enc_on = 0;
if (atomic_dec_return(&solo_enc->readers) > 0)
return;
solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(solo_enc->ch), 0);
solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(solo_enc->ch), 0);
}
static int solo_start_fh_thread(struct solo_enc_fh *fh)
{
struct solo_enc_dev *solo_enc = fh->enc;
fh->kthread = kthread_run(solo_enc_thread, fh, SOLO6X10_NAME "_enc");
/* Oops, we had a problem */
if (IS_ERR(fh->kthread)) {
spin_lock(&solo_enc->lock);
solo_enc_off(fh);
spin_unlock(&solo_enc->lock);
return PTR_ERR(fh->kthread);
}
return 0;
}
static void enc_reset_gop(struct solo_dev *solo_dev, u8 ch)
{
BUG_ON(ch >= solo_dev->nr_chans);
solo_reg_write(solo_dev, SOLO_VE_CH_GOP(ch), 1);
solo_dev->v4l2_enc[ch]->reset_gop = 1;
}
static int enc_gop_reset(struct solo_dev *solo_dev, u8 ch, u8 vop)
{
BUG_ON(ch >= solo_dev->nr_chans);
if (!solo_dev->v4l2_enc[ch]->reset_gop)
return 0;
if (vop)
return 1;
solo_dev->v4l2_enc[ch]->reset_gop = 0;
solo_reg_write(solo_dev, SOLO_VE_CH_GOP(ch),
solo_dev->v4l2_enc[ch]->gop);
return 0;
}
static void enc_write_sg(struct scatterlist *sglist, void *buf, int size)
{
struct scatterlist *sg;
u8 *src = buf;
for (sg = sglist; sg && size > 0; sg = sg_next(sg)) {
u8 *p = sg_virt(sg);
size_t len = sg_dma_len(sg);
int i;
for (i = 0; i < len && size; i++)
p[i] = *(src++);
}
}
static int enc_get_mpeg_dma_sg(struct solo_dev *solo_dev,
struct p2m_desc *desc,
struct scatterlist *sglist, int skip,
unsigned int off, unsigned int size)
{
int ret;
if (off > SOLO_MP4E_EXT_SIZE(solo_dev))
return -EINVAL;
if (off + size <= SOLO_MP4E_EXT_SIZE(solo_dev)) {
return solo_p2m_dma_sg(solo_dev, SOLO_P2M_DMA_ID_MP4E,
desc, 0, sglist, skip,
SOLO_MP4E_EXT_ADDR(solo_dev) + off, size);
}
/* Buffer wrap */
ret = solo_p2m_dma_sg(solo_dev, SOLO_P2M_DMA_ID_MP4E, desc, 0,
sglist, skip, SOLO_MP4E_EXT_ADDR(solo_dev) + off,
SOLO_MP4E_EXT_SIZE(solo_dev) - off);
ret |= solo_p2m_dma_sg(solo_dev, SOLO_P2M_DMA_ID_MP4E, desc, 0,
sglist, skip + SOLO_MP4E_EXT_SIZE(solo_dev) - off,
SOLO_MP4E_EXT_ADDR(solo_dev),
size + off - SOLO_MP4E_EXT_SIZE(solo_dev));
return ret;
}
static int enc_get_mpeg_dma_t(struct solo_dev *solo_dev,
dma_addr_t buf, unsigned int off,
unsigned int size)
{
int ret;
if (off > SOLO_MP4E_EXT_SIZE(solo_dev))
return -EINVAL;
if (off + size <= SOLO_MP4E_EXT_SIZE(solo_dev)) {
return solo_p2m_dma_t(solo_dev, SOLO_P2M_DMA_ID_MP4E, 0, buf,
SOLO_MP4E_EXT_ADDR(solo_dev) + off, size);
}
/* Buffer wrap */
ret = solo_p2m_dma_t(solo_dev, SOLO_P2M_DMA_ID_MP4E, 0, buf,
SOLO_MP4E_EXT_ADDR(solo_dev) + off,
SOLO_MP4E_EXT_SIZE(solo_dev) - off);
ret |= solo_p2m_dma_t(solo_dev, SOLO_P2M_DMA_ID_MP4E, 0,
buf + SOLO_MP4E_EXT_SIZE(solo_dev) - off,
SOLO_MP4E_EXT_ADDR(solo_dev),
size + off - SOLO_MP4E_EXT_SIZE(solo_dev));
return ret;
}
static int enc_get_mpeg_dma(struct solo_dev *solo_dev, void *buf,
unsigned int off, unsigned int size)
{
int ret;
dma_addr_t dma_addr = pci_map_single(solo_dev->pdev, buf, size,
PCI_DMA_FROMDEVICE);
ret = enc_get_mpeg_dma_t(solo_dev, dma_addr, off, size);
pci_unmap_single(solo_dev->pdev, dma_addr, size, PCI_DMA_FROMDEVICE);
return ret;
}
static int enc_get_jpeg_dma_sg(struct solo_dev *solo_dev,
struct p2m_desc *desc,
struct scatterlist *sglist, int skip,
unsigned int off, unsigned int size)
{
int ret;
if (off > SOLO_JPEG_EXT_SIZE(solo_dev))
return -EINVAL;
if (off + size <= SOLO_JPEG_EXT_SIZE(solo_dev)) {
return solo_p2m_dma_sg(solo_dev, SOLO_P2M_DMA_ID_JPEG,
desc, 0, sglist, skip,
SOLO_JPEG_EXT_ADDR(solo_dev) + off, size);
}
/* Buffer wrap */
ret = solo_p2m_dma_sg(solo_dev, SOLO_P2M_DMA_ID_JPEG, desc, 0,
sglist, skip, SOLO_JPEG_EXT_ADDR(solo_dev) + off,
SOLO_JPEG_EXT_SIZE(solo_dev) - off);
ret |= solo_p2m_dma_sg(solo_dev, SOLO_P2M_DMA_ID_JPEG, desc, 0,
sglist, skip + SOLO_JPEG_EXT_SIZE(solo_dev) - off,
SOLO_JPEG_EXT_ADDR(solo_dev),
size + off - SOLO_JPEG_EXT_SIZE(solo_dev));
return ret;
}
/* Returns true of __chk is within the first __range bytes of __off */
#define OFF_IN_RANGE(__off, __range, __chk) \
((__off <= __chk) && ((__off + __range) >= __chk))
static void solo_jpeg_header(struct solo_enc_dev *solo_enc,
struct videobuf_dmabuf *vbuf)
{
struct scatterlist *sg;
void *src = jpeg_header;
size_t copied = 0;
size_t to_copy = sizeof(jpeg_header);
for (sg = vbuf->sglist; sg && copied < to_copy; sg = sg_next(sg)) {
size_t this_copy = min(sg_dma_len(sg),
(unsigned int)(to_copy - copied));
u8 *p = sg_virt(sg);
memcpy(p, src + copied, this_copy);
if (OFF_IN_RANGE(copied, this_copy, SOF0_START + 5))
p[(SOF0_START + 5) - copied] =
0xff & (solo_enc->height >> 8);
if (OFF_IN_RANGE(copied, this_copy, SOF0_START + 6))
p[(SOF0_START + 6) - copied] = 0xff & solo_enc->height;
if (OFF_IN_RANGE(copied, this_copy, SOF0_START + 7))
p[(SOF0_START + 7) - copied] =
0xff & (solo_enc->width >> 8);
if (OFF_IN_RANGE(copied, this_copy, SOF0_START + 8))
p[(SOF0_START + 8) - copied] = 0xff & solo_enc->width;
copied += this_copy;
}
}
static int solo_fill_jpeg(struct solo_enc_fh *fh, struct solo_enc_buf *enc_buf,
struct videobuf_buffer *vb,
struct videobuf_dmabuf *vbuf)
{
struct solo_dev *solo_dev = fh->enc->solo_dev;
int size = enc_buf->jpeg_size;
/* Copy the header first (direct write) */
solo_jpeg_header(fh->enc, vbuf);
vb->size = size + sizeof(jpeg_header);
/* Grab the jpeg frame */
return enc_get_jpeg_dma_sg(solo_dev, fh->desc, vbuf->sglist,
sizeof(jpeg_header),
enc_buf->jpeg_off, size);
}
static inline int vop_interlaced(__le32 *vh)
{
return (__le32_to_cpu(vh[0]) >> 30) & 1;
}
static inline u32 vop_size(__le32 *vh)
{
return __le32_to_cpu(vh[0]) & 0xFFFFF;
}
static inline u8 vop_hsize(__le32 *vh)
{
return (__le32_to_cpu(vh[1]) >> 8) & 0xFF;
}
static inline u8 vop_vsize(__le32 *vh)
{
return __le32_to_cpu(vh[1]) & 0xFF;
}
/* must be called with *bits % 8 = 0 */
static void write_bytes(u8 **out, unsigned *bits, const u8 *src, unsigned count)
{
memcpy(*out, src, count);
*out += count;
*bits += count * 8;
}
static void write_bits(u8 **out, unsigned *bits, u32 value, unsigned count)
{
value <<= 32 - count; // shift to the right
while (count--) {
**out <<= 1;
**out |= !!(value & (1 << 31)); /* MSB */
value <<= 1;
if (++(*bits) % 8 == 0)
(*out)++;
}
}
static void write_ue(u8 **out, unsigned *bits, unsigned value) /* H.264 only */
{
uint32_t max = 0, cnt = 0;
while (value > max) {
max = (max + 2) * 2 - 2;
cnt++;
}
write_bits(out, bits, 1, cnt + 1);
write_bits(out, bits, ~(max - value), cnt);
}
static void write_se(u8 **out, unsigned *bits, int value) /* H.264 only */
{
if (value <= 0)
write_ue(out, bits, -value * 2);
else
write_ue(out, bits, value * 2 - 1);
}
static void write_mpeg4_end(u8 **out, unsigned *bits)
{
write_bits(out, bits, 0, 1);
/* align on 32-bit boundary */
if (*bits % 32)
write_bits(out, bits, 0xFFFFFFFF, 32 - *bits % 32);
}
static void write_h264_end(u8 **out, unsigned *bits, int align)
{
write_bits(out, bits, 1, 1);
while ((*bits) % 8)
write_bits(out, bits, 0, 1);
if (align)
while ((*bits) % 32)
write_bits(out, bits, 0, 1);
}
static void mpeg4_write_vol(u8 **out, struct solo_dev *solo_dev,
__le32 *vh, unsigned fps, unsigned interval)
{
static const u8 hdr[] = {
0, 0, 1, 0x00 /* video_object_start_code */,
0, 0, 1, 0x20 /* video_object_layer_start_code */
};
unsigned bits = 0;
unsigned width = vop_hsize(vh) << 4;
unsigned height = vop_vsize(vh) << 4;
unsigned interlaced = vop_interlaced(vh);
write_bytes(out, &bits, hdr, sizeof(hdr));
write_bits(out, &bits, 0, 1); /* random_accessible_vol */
write_bits(out, &bits, 0x04, 8); /* video_object_type_indication: main */
write_bits(out, &bits, 1, 1); /* is_object_layer_identifier */
write_bits(out, &bits, 2, 4); /* video_object_layer_verid: table V2-39 */
write_bits(out, &bits, 0, 3); /* video_object_layer_priority */
if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
write_bits(out, &bits, 3, 4); /* aspect_ratio_info, assuming 4:3 */
else
write_bits(out, &bits, 2, 4);
write_bits(out, &bits, 1, 1); /* vol_control_parameters */
write_bits(out, &bits, 1, 2); /* chroma_format: 4:2:0 */
write_bits(out, &bits, 1, 1); /* low_delay */
write_bits(out, &bits, 0, 1); /* vbv_parameters */
write_bits(out, &bits, 0, 2); /* video_object_layer_shape: rectangular */
write_bits(out, &bits, 1, 1); /* marker_bit */
write_bits(out, &bits, fps, 16); /* vop_time_increment_resolution */
write_bits(out, &bits, 1, 1); /* marker_bit */
write_bits(out, &bits, 1, 1); /* fixed_vop_rate */
write_bits(out, &bits, interval, 15); /* fixed_vop_time_increment */
write_bits(out, &bits, 1, 1); /* marker_bit */
write_bits(out, &bits, width, 13); /* video_object_layer_width */
write_bits(out, &bits, 1, 1); /* marker_bit */
write_bits(out, &bits, height, 13); /* video_object_layer_height */
write_bits(out, &bits, 1, 1); /* marker_bit */
write_bits(out, &bits, interlaced, 1); /* interlaced */
write_bits(out, &bits, 1, 1); /* obmc_disable */
write_bits(out, &bits, 0, 2); /* sprite_enable */
write_bits(out, &bits, 0, 1); /* not_8_bit */
write_bits(out, &bits, 1, 0); /* quant_type */
write_bits(out, &bits, 0, 1); /* load_intra_quant_mat */
write_bits(out, &bits, 0, 1); /* load_nonintra_quant_mat */
write_bits(out, &bits, 0, 1); /* quarter_sample */
write_bits(out, &bits, 1, 1); /* complexity_estimation_disable */
write_bits(out, &bits, 1, 1); /* resync_marker_disable */
write_bits(out, &bits, 0, 1); /* data_partitioned */
write_bits(out, &bits, 0, 1); /* newpred_enable */
write_bits(out, &bits, 0, 1); /* reduced_resolution_vop_enable */
write_bits(out, &bits, 0, 1); /* scalability */
write_mpeg4_end(out, &bits);
}
static void h264_write_vol(u8 **out, struct solo_dev *solo_dev, __le32 *vh)
{
static const u8 sps[] = {
0, 0, 0, 1 /* start code */, 0x67, 66 /* profile_idc */,
0 /* constraints */, 30 /* level_idc */
};
static const u8 pps[] = {
0, 0, 0, 1 /* start code */, 0x68
};
unsigned bits = 0;
unsigned mbs_w = vop_hsize(vh);
unsigned mbs_h = vop_vsize(vh);
write_bytes(out, &bits, sps, sizeof(sps));
write_ue(out, &bits, 0); /* seq_parameter_set_id */
write_ue(out, &bits, 5); /* log2_max_frame_num_minus4 */
write_ue(out, &bits, 0); /* pic_order_cnt_type */
write_ue(out, &bits, 6); /* log2_max_pic_order_cnt_lsb_minus4 */
write_ue(out, &bits, 1); /* max_num_ref_frames */
write_bits(out, &bits, 0, 1); /* gaps_in_frame_num_value_allowed_flag */
write_ue(out, &bits, mbs_w - 1); /* pic_width_in_mbs_minus1 */
write_ue(out, &bits, mbs_h - 1); /* pic_height_in_map_units_minus1 */
write_bits(out, &bits, 1, 1); /* frame_mbs_only_flag */
write_bits(out, &bits, 1, 1); /* direct_8x8_frame_field_flag */
write_bits(out, &bits, 0, 1); /* frame_cropping_flag */
write_bits(out, &bits, 0, 1); /* vui_parameters_present_flag */
write_h264_end(out, &bits, 0);
write_bytes(out, &bits, pps, sizeof(pps));
write_ue(out, &bits, 0); /* pic_parameter_set_id */
write_ue(out, &bits, 0); /* seq_parameter_set_id */
write_bits(out, &bits, 0, 1); /* entropy_coding_mode_flag */
write_bits(out, &bits, 0, 1); /* bottom_field_pic_order_in_frame_present_flag */
write_ue(out, &bits, 0); /* num_slice_groups_minus1 */
write_ue(out, &bits, 0); /* num_ref_idx_l0_default_active_minus1 */
write_ue(out, &bits, 0); /* num_ref_idx_l1_default_active_minus1 */
write_bits(out, &bits, 0, 1); /* weighted_pred_flag */
write_bits(out, &bits, 0, 2); /* weighted_bipred_idc */
write_se(out, &bits, 0); /* pic_init_qp_minus26 */
write_se(out, &bits, 0); /* pic_init_qs_minus26 */
write_se(out, &bits, 2); /* chroma_qp_index_offset */
write_bits(out, &bits, 0, 1); /* deblocking_filter_control_present_flag */
write_bits(out, &bits, 1, 1); /* constrained_intra_pred_flag */
write_bits(out, &bits, 0, 1); /* redundant_pic_cnt_present_flag */
write_h264_end(out, &bits, 1);
}
static int solo_fill_mpeg(struct solo_enc_fh *fh, struct solo_enc_buf *enc_buf,
struct videobuf_buffer *vb,
struct videobuf_dmabuf *vbuf)
{
struct solo_enc_dev *solo_enc = fh->enc;
struct solo_dev *solo_dev = solo_enc->solo_dev;
#define VH_WORDS 16
#define MAX_VOL_HEADER_LENGTH 64
__le32 vh[VH_WORDS];
int ret;
int frame_size, frame_off;
int skip = 0;
if (WARN_ON_ONCE(enc_buf->size <= sizeof(vh)))
return -EINVAL;
/* First get the hardware vop header (not real mpeg) */
ret = enc_get_mpeg_dma(solo_dev, vh, enc_buf->off, sizeof(vh));
if (WARN_ON_ONCE(ret))
return ret;
if (WARN_ON_ONCE(vop_size(vh) > enc_buf->size))
return -EINVAL;
vb->width = vop_hsize(vh) << 4;
vb->height = vop_vsize(vh) << 4;
vb->size = vop_size(vh);
/* If this is a key frame, add extra m4v header */
if (!enc_buf->vop) {
u8 header[MAX_VOL_HEADER_LENGTH], *out = header;
if (solo_dev->flags & FLAGS_6110)
h264_write_vol(&out, solo_dev, vh);
else
mpeg4_write_vol(&out, solo_dev, vh,
solo_dev->fps * 1000,
solo_enc->interval * 1000);
skip = out - header;
enc_write_sg(vbuf->sglist, header, skip);
/* Adjust the dma buffer past this header */
vb->size += skip;
}
/* Now get the actual mpeg payload */
frame_off = (enc_buf->off + sizeof(vh)) % SOLO_MP4E_EXT_SIZE(solo_dev);
frame_size = enc_buf->size - sizeof(vh);
ret = enc_get_mpeg_dma_sg(solo_dev, fh->desc, vbuf->sglist,
skip, frame_off, frame_size);
WARN_ON_ONCE(ret);
return ret;
}
static void solo_enc_fillbuf(struct solo_enc_fh *fh,
struct videobuf_buffer *vb)
{
struct solo_enc_dev *solo_enc = fh->enc;
struct solo_dev *solo_dev = solo_enc->solo_dev;
struct solo_enc_buf *enc_buf = NULL;
struct videobuf_dmabuf *vbuf;
int ret;
int error = 1;
u16 idx = fh->rd_idx;
while (idx != solo_dev->enc_wr_idx) {
struct solo_enc_buf *ebuf = &solo_dev->enc_buf[idx];
idx = (idx + 1) % SOLO_NR_RING_BUFS;
if (ebuf->ch != solo_enc->ch)
continue;
if (fh->fmt == V4L2_PIX_FMT_MPEG) {
if (fh->type == ebuf->type) {
enc_buf = ebuf;
break;
}
} else {
/* For mjpeg, keep reading to the newest frame */
enc_buf = ebuf;
}
}
fh->rd_idx = idx;
if (WARN_ON_ONCE(!enc_buf))
goto buf_err;
if ((fh->fmt == V4L2_PIX_FMT_MPEG &&
vb->bsize < enc_buf->size) ||
(fh->fmt == V4L2_PIX_FMT_MJPEG &&
vb->bsize < (enc_buf->jpeg_size + sizeof(jpeg_header)))) {
WARN_ON_ONCE(1);
goto buf_err;
}
vbuf = videobuf_to_dma(vb);
if (WARN_ON_ONCE(!vbuf))
goto buf_err;
if (fh->fmt == V4L2_PIX_FMT_MPEG)
ret = solo_fill_mpeg(fh, enc_buf, vb, vbuf);
else
ret = solo_fill_jpeg(fh, enc_buf, vb, vbuf);
if (!ret)
error = 0;
buf_err:
if (error) {
vb->state = VIDEOBUF_ERROR;
} else {
vb->field_count++;
vb->ts = enc_buf->ts;
vb->state = VIDEOBUF_DONE;
}
wake_up(&vb->done);
return;
}
static void solo_enc_thread_try(struct solo_enc_fh *fh)
{
struct solo_enc_dev *solo_enc = fh->enc;
struct solo_dev *solo_dev = solo_enc->solo_dev;
struct videobuf_buffer *vb;
for (;;) {
spin_lock(&solo_enc->lock);
if (fh->rd_idx == solo_dev->enc_wr_idx)
break;
if (list_empty(&fh->vidq_active))
break;
vb = list_first_entry(&fh->vidq_active,
struct videobuf_buffer, queue);
if (!waitqueue_active(&vb->done))
break;
list_del(&vb->queue);
spin_unlock(&solo_enc->lock);
solo_enc_fillbuf(fh, vb);
}
assert_spin_locked(&solo_enc->lock);
spin_unlock(&solo_enc->lock);
}
static int solo_enc_thread(void *data)
{
struct solo_enc_fh *fh = data;
struct solo_enc_dev *solo_enc = fh->enc;
DECLARE_WAITQUEUE(wait, current);
set_freezable();
add_wait_queue(&solo_enc->thread_wait, &wait);
for (;;) {
long timeout = schedule_timeout_interruptible(HZ);
if (timeout == -ERESTARTSYS || kthread_should_stop())
break;
solo_enc_thread_try(fh);
try_to_freeze();
}
remove_wait_queue(&solo_enc->thread_wait, &wait);
return 0;
}
void solo_motion_isr(struct solo_dev *solo_dev)
{
u32 status;
int i;
solo_reg_write(solo_dev, SOLO_IRQ_STAT, SOLO_IRQ_MOTION);
status = solo_reg_read(solo_dev, SOLO_VI_MOT_STATUS);
for (i = 0; i < solo_dev->nr_chans; i++) {
struct solo_enc_dev *solo_enc = solo_dev->v4l2_enc[i];
BUG_ON(solo_enc == NULL);
if (solo_enc->motion_detected)
continue;
if (!(status & (1 << i)))
continue;
solo_enc->motion_detected = 1;
}
}
void solo_enc_v4l2_isr(struct solo_dev *solo_dev)
{
struct solo_enc_buf *enc_buf;
u32 mpeg_current, mpeg_next, mpeg_size;
u32 jpeg_current, jpeg_next, jpeg_size;
u32 reg_mpeg_size;
u8 cur_q, vop_type;
u8 ch;
enum solo_enc_types enc_type;
solo_reg_write(solo_dev, SOLO_IRQ_STAT, SOLO_IRQ_ENCODER);
cur_q = ((solo_reg_read(solo_dev, SOLO_VE_STATE(11)) & 0xF) + 1) % MP4_QS;
reg_mpeg_size = ((solo_reg_read(solo_dev, SOLO_VE_STATE(0)) & 0xFFFFF) + 64 + 8) & ~7;
while (solo_dev->enc_idx != cur_q) {
mpeg_current = solo_reg_read(solo_dev,
SOLO_VE_MPEG4_QUE(solo_dev->enc_idx));
jpeg_current = solo_reg_read(solo_dev,
SOLO_VE_JPEG_QUE(solo_dev->enc_idx));
solo_dev->enc_idx = (solo_dev->enc_idx + 1) % MP4_QS;
mpeg_next = solo_reg_read(solo_dev,
SOLO_VE_MPEG4_QUE(solo_dev->enc_idx));
jpeg_next = solo_reg_read(solo_dev,
SOLO_VE_JPEG_QUE(solo_dev->enc_idx));
ch = (mpeg_current >> 24) & 0x1f;
if (ch >= SOLO_MAX_CHANNELS) {
ch -= SOLO_MAX_CHANNELS;
enc_type = SOLO_ENC_TYPE_EXT;
} else
enc_type = SOLO_ENC_TYPE_STD;
vop_type = (mpeg_current >> 29) & 3;
mpeg_current &= 0x00ffffff;
mpeg_next &= 0x00ffffff;
jpeg_current &= 0x00ffffff;
jpeg_next &= 0x00ffffff;
mpeg_size = (SOLO_MP4E_EXT_SIZE(solo_dev) +
mpeg_next - mpeg_current) %
SOLO_MP4E_EXT_SIZE(solo_dev);
jpeg_size = (SOLO_JPEG_EXT_SIZE(solo_dev) +
jpeg_next - jpeg_current) %
SOLO_JPEG_EXT_SIZE(solo_dev);
/* XXX I think this means we had a ring overflow? */
if (mpeg_current > mpeg_next && mpeg_size != reg_mpeg_size) {
enc_reset_gop(solo_dev, ch);
continue;
}
/* When resetting the GOP, skip frames until I-frame */
if (enc_gop_reset(solo_dev, ch, vop_type))
continue;
enc_buf = &solo_dev->enc_buf[solo_dev->enc_wr_idx];
enc_buf->vop = vop_type;
enc_buf->ch = ch;
enc_buf->off = mpeg_current;
enc_buf->size = mpeg_size;
enc_buf->jpeg_off = jpeg_current;
enc_buf->jpeg_size = jpeg_size;
enc_buf->type = enc_type;
do_gettimeofday(&enc_buf->ts);
solo_dev->enc_wr_idx = (solo_dev->enc_wr_idx + 1) %
SOLO_NR_RING_BUFS;
wake_up_interruptible(&solo_dev->v4l2_enc[ch]->thread_wait);
}
return;
}
static int solo_enc_buf_setup(struct videobuf_queue *vq, unsigned int *count,
unsigned int *size)
{
*size = FRAME_BUF_SIZE;
if (*count < MIN_VID_BUFFERS)
*count = MIN_VID_BUFFERS;
return 0;
}
static int solo_enc_buf_prepare(struct videobuf_queue *vq,
struct videobuf_buffer *vb,
enum v4l2_field field)
{
struct solo_enc_fh *fh = vq->priv_data;
struct solo_enc_dev *solo_enc = fh->enc;
vb->size = FRAME_BUF_SIZE;
if (vb->baddr != 0 && vb->bsize < vb->size)
return -EINVAL;
/* These properties only change when queue is idle */
vb->width = solo_enc->width;
vb->height = solo_enc->height;
vb->field = field;
if (vb->state == VIDEOBUF_NEEDS_INIT) {
int rc = videobuf_iolock(vq, vb, NULL);
if (rc < 0) {
struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
videobuf_dma_unmap(vq->dev, dma);
videobuf_dma_free(dma);
vb->state = VIDEOBUF_NEEDS_INIT;
return rc;
}
}
vb->state = VIDEOBUF_PREPARED;
return 0;
}
static void solo_enc_buf_queue(struct videobuf_queue *vq,
struct videobuf_buffer *vb)
{
struct solo_enc_fh *fh = vq->priv_data;
vb->state = VIDEOBUF_QUEUED;
list_add_tail(&vb->queue, &fh->vidq_active);
wake_up_interruptible(&fh->enc->thread_wait);
}
static void solo_enc_buf_release(struct videobuf_queue *vq,
struct videobuf_buffer *vb)
{
struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
videobuf_dma_unmap(vq->dev, dma);
videobuf_dma_free(dma);
vb->state = VIDEOBUF_NEEDS_INIT;
}
static struct videobuf_queue_ops solo_enc_video_qops = {
.buf_setup = solo_enc_buf_setup,
.buf_prepare = solo_enc_buf_prepare,
.buf_queue = solo_enc_buf_queue,
.buf_release = solo_enc_buf_release,
};
static unsigned int solo_enc_poll(struct file *file,
struct poll_table_struct *wait)
{
struct solo_enc_fh *fh = file->private_data;
return videobuf_poll_stream(file, &fh->vidq, wait);
}
static int solo_enc_mmap(struct file *file, struct vm_area_struct *vma)
{
struct solo_enc_fh *fh = file->private_data;
return videobuf_mmap_mapper(&fh->vidq, vma);
}
static int solo_enc_open(struct file *file)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
struct solo_enc_fh *fh;
fh = kzalloc(sizeof(*fh), GFP_KERNEL);
if (fh == NULL)
return -ENOMEM;
fh->enc = solo_enc;
file->private_data = fh;
INIT_LIST_HEAD(&fh->vidq_active);
fh->fmt = V4L2_PIX_FMT_MPEG;
fh->type = SOLO_ENC_TYPE_STD;