3 * Copyright (c) 2002 Fabrice Bellard.
6 * Copyright (c) 2003 Roman Shaposhnik.
8 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
9 * of DV technical info.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "mpegvideo.h"
33 #include "simple_idct.h"
36 typedef struct DVVideoDecodeContext {
40 uint8_t dv_zigzag[2][64];
41 uint8_t dv_idct_shift[2][22][64];
42 uint8_t dv_dct_shift[2][22][64];
44 void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
45 void (*fdct[2])(DCTELEM *block);
46 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
49 DCTELEM block[5*6][64] __align8;
50 } DVVideoDecodeContext;
52 #define TEX_VLC_BITS 9
53 /* XXX: also include quantization */
54 static RL_VLC_ELEM *dv_rl_vlc[1];
55 static VLC_TYPE dv_vlc_codes[15][23];
57 static void dv_build_unquantize_tables(DVVideoDecodeContext *s, uint8_t* perm)
61 /* NOTE: max left shift is 6 */
62 for(q = 0; q < 22; q++) {
64 for(i = 1; i < 64; i++) {
67 s->dv_idct_shift[0][q][j] =
68 dv_quant_shifts[q][dv_88_areas[i]] + 1;
69 s->dv_dct_shift[0][q][i] =
70 dv_quant_shifts[q][dv_88_areas[ff_zigzag_direct[i]]] + 4;
74 for(i = 1; i < 64; i++) {
76 s->dv_idct_shift[1][q][i] =
77 dv_quant_shifts[q][dv_248_areas[i]] + 1;
78 s->dv_dct_shift[1][q][i] =
79 dv_quant_shifts[q][dv_248_areas[ff_zigzag248_direct[i]]] + 4;
84 static int dvvideo_init(AVCodecContext *avctx)
86 DVVideoDecodeContext *s = avctx->priv_data;
97 /* NOTE: as a trick, we use the fact the no codes are unused
98 to accelerate the parsing of partial codes */
99 init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC,
100 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
102 dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
103 for(i = 0; i < dv_vlc.table_size; i++){
104 int code= dv_vlc.table[i][0];
105 int len = dv_vlc.table[i][1];
108 if(len<0){ //more bits needed
111 } else if (code == (NB_DV_VLC - 1)) {
116 run= dv_vlc_run[code] + 1;
117 level= dv_vlc_level[code];
119 dv_rl_vlc[0][i].len = len;
120 dv_rl_vlc[0][i].level = level;
121 dv_rl_vlc[0][i].run = run;
124 memset(dv_vlc_codes, 0xff, sizeof(dv_vlc_codes));
125 for (i = 0; i < NB_DV_VLC - 1; i++) {
126 if (dv_vlc_run[i] < 15 && dv_vlc_level[i] < 23 && dv_vlc_len[i] < 15)
127 dv_vlc_codes[dv_vlc_run[i]][dv_vlc_level[i]] = i;
131 /* Generic DSP setup */
132 dsputil_init(&dsp, avctx);
133 s->get_pixels = dsp.get_pixels;
136 s->fdct[0] = dsp.fdct;
137 s->idct_put[0] = dsp.idct_put;
139 s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]];
142 s->fdct[1] = dsp.fdct248;
143 s->idct_put[1] = simple_idct248_put; // FIXME: need to add it to DSP
144 memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
146 /* XXX: do it only for constant case */
147 dv_build_unquantize_tables(s, dsp.idct_permutation);
149 /* FIXME: I really don't think this should be here */
150 if (dv_codec_profile(avctx))
151 avctx->pix_fmt = dv_codec_profile(avctx)->pix_fmt;
152 avctx->coded_frame = &s->picture;
159 typedef struct BlockInfo {
160 const uint8_t *shift_table;
161 const uint8_t *scan_table;
162 uint8_t pos; /* position in block */
163 uint8_t eob_reached; /* true if EOB has been reached */
165 uint8_t partial_bit_count;
166 uint16_t partial_bit_buffer;
170 /* block size in bits */
171 static const uint16_t block_sizes[6] = {
172 112, 112, 112, 112, 80, 80
175 #ifndef ALT_BITSTREAM_READER
176 #warning only works with ALT_BITSTREAM_READER
179 /* decode ac coefs */
180 static void dv_decode_ac(DVVideoDecodeContext *s,
181 BlockInfo *mb, DCTELEM *block, int last_index)
184 int shift_offset = mb->shift_offset;
185 const uint8_t *scan_table = mb->scan_table;
186 const uint8_t *shift_table = mb->shift_table;
188 int level, pos1, sign, run;
189 int partial_bit_count;
190 #ifndef ALT_BITSTREAM_READER //FIXME
194 OPEN_READER(re, &s->gb);
200 /* if we must parse a partial vlc, we do it here */
201 partial_bit_count = mb->partial_bit_count;
202 if (partial_bit_count > 0) {
208 /* build the dummy bit buffer */
209 l = 16 - partial_bit_count;
210 UPDATE_CACHE(re, &s->gb);
212 printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16));
214 v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l);
218 printf("v=%04x cnt=%d %04x\n",
219 v, partial_bit_count, (mb->partial_bit_buffer << l));
221 /* try to read the codeword */
222 init_get_bits(&gb1, buf, 4*8);
224 OPEN_READER(re1, &gb1);
225 UPDATE_CACHE(re1, &gb1);
226 GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0],
229 CLOSE_READER(re1, &gb1);
232 printf("****run=%d level=%d size=%d\n", run, level, l);
234 /* compute codeword length */
235 l1 = (level != 256 && level != 0);
236 /* if too long, we cannot parse */
237 l -= partial_bit_count;
238 if ((re_index + l + l1) > last_index)
241 last_re_index = 0; /* avoid warning */
243 /* by definition, if we can read the vlc, all partial bits
244 will be read (otherwise we could have read the vlc before) */
245 mb->partial_bit_count = 0;
246 UPDATE_CACHE(re, &s->gb);
250 /* get the AC coefficients until last_index is reached */
252 UPDATE_CACHE(re, &s->gb);
254 printf("%2d: bits=%04x index=%d\n",
255 pos, SHOW_UBITS(re, &s->gb, 16), re_index);
257 last_re_index = re_index;
258 GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0],
262 printf("run=%d level=%d\n", run, level);
265 if (re_index > last_index) {
267 /* put position before read code */
268 re_index = last_re_index;
275 } else if (level != 0) {
276 if ((re_index + 1) > last_index)
278 sign = SHOW_SBITS(re, &s->gb, 1);
279 level = (level ^ sign) - sign;
280 LAST_SKIP_BITS(re, &s->gb, 1);
286 pos1 = scan_table[pos];
287 level = level << (shift_table[pos1] + shift_offset);
289 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]);
291 if (re_index > last_index)
293 /* level is zero: means run without coding. No
299 #if defined(VLC_DEBUG) || 1
300 av_log(NULL, AV_LOG_ERROR, "error pos=%d\n", pos);
302 /* for errors, we consider the eob is reached */
308 CLOSE_READER(re, &s->gb);
312 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left)
314 while (bits_left >= 16) {
315 put_bits(pb, 16, get_bits(gb, 16));
319 put_bits(pb, bits_left, get_bits(gb, bits_left));
323 /* mb_x and mb_y are in units of 8 pixels */
324 static inline void dv_decode_video_segment(DVVideoDecodeContext *s,
326 const uint16_t *mb_pos_ptr)
328 int quant, dc, dct_mode, class1, j;
329 int mb_index, mb_x, mb_y, v, last_index;
330 DCTELEM *block, *block1;
331 int c_offset, bits_left;
333 BlockInfo mb_data[5 * 6], *mb, *mb1;
334 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
336 PutBitContext pb, vs_pb;
337 uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */
339 uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */
342 memset(s->block, 0, sizeof(s->block));
344 /* pass 1 : read DC and AC coefficients in blocks */
346 block1 = &s->block[0][0];
348 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
350 for(mb_index = 0; mb_index < 5; mb_index++) {
352 quant = buf_ptr[3] & 0x0f;
354 init_put_bits(&pb, mb_bit_buffer, 80);
358 for(j = 0;j < 6; j++) {
359 /* NOTE: size is not important here */
360 init_get_bits(&s->gb, buf_ptr, 14*8);
363 dc = get_bits(&s->gb, 9);
364 dc = (dc << (32 - 9)) >> (32 - 9);
365 dct_mode = get_bits1(&s->gb);
366 mb->dct_mode = dct_mode;
367 mb->scan_table = s->dv_zigzag[dct_mode];
368 class1 = get_bits(&s->gb, 2);
369 mb->shift_offset = (class1 == 3);
370 mb->shift_table = s->dv_idct_shift[dct_mode]
371 [quant + dv_quant_offset[class1]];
373 /* convert to unsigned because 128 is not added in the
377 last_index = block_sizes[j];
378 buf_ptr += last_index >> 3;
380 mb->partial_bit_count = 0;
383 printf("MB block: %d, %d ", mb_index, j);
385 dv_decode_ac(s, mb, block, last_index);
387 /* write the remaining bits in a new buffer only if the
389 bits_left = last_index - get_bits_count(&s->gb);
390 if (mb->eob_reached) {
391 mb->partial_bit_count = 0;
392 mb_bit_count += bits_left;
393 bit_copy(&pb, &s->gb, bits_left);
395 /* should be < 16 bits otherwise a codeword could have
397 mb->partial_bit_count = bits_left;
398 mb->partial_bit_buffer = get_bits(&s->gb, bits_left);
406 /* pass 2 : we can do it just after */
408 printf("***pass 2 size=%d MB#=%d\n", mb_bit_count, mb_index);
412 init_get_bits(&s->gb, mb_bit_buffer, 80*8);
413 for(j = 0;j < 6; j++) {
414 if (!mb->eob_reached && get_bits_count(&s->gb) < mb_bit_count) {
415 dv_decode_ac(s, mb, block, mb_bit_count);
416 /* if still not finished, no need to parse other blocks */
417 if (!mb->eob_reached) {
418 /* we could not parse the current AC coefficient,
419 so we add the remaining bytes */
420 bits_left = mb_bit_count - get_bits_count(&s->gb);
422 mb->partial_bit_count += bits_left;
423 mb->partial_bit_buffer =
424 (mb->partial_bit_buffer << bits_left) |
425 get_bits(&s->gb, bits_left);
433 /* all blocks are finished, so the extra bytes can be used at
434 the video segment level */
435 bits_left = mb_bit_count - get_bits_count(&s->gb);
436 vs_bit_count += bits_left;
437 bit_copy(&vs_pb, &s->gb, bits_left);
443 /* we need a pass other the whole video segment */
444 flush_put_bits(&vs_pb);
447 printf("***pass 3 size=%d\n", vs_bit_count);
449 block = &s->block[0][0];
451 init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8);
452 for(mb_index = 0; mb_index < 5; mb_index++) {
453 for(j = 0;j < 6; j++) {
454 if (!mb->eob_reached) {
456 printf("start %d:%d\n", mb_index, j);
458 dv_decode_ac(s, mb, block, vs_bit_count);
465 /* compute idct and place blocks */
466 block = &s->block[0][0];
468 for(mb_index = 0; mb_index < 5; mb_index++) {
472 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
473 if (s->sys->pix_fmt == PIX_FMT_YUV411P)
474 c_offset = (mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8);
476 c_offset = ((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8);
477 for(j = 0;j < 6; j++) {
478 idct_put = s->idct_put[mb->dct_mode];
480 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
481 /* NOTE: at end of line, the macroblock is handled as 420 */
482 idct_put(y_ptr + (j * 8), s->picture.linesize[0], block);
484 idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]),
485 s->picture.linesize[0], block);
488 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
489 uint64_t aligned_pixels[64/8];
490 uint8_t *pixels= (uint8_t*)aligned_pixels;
491 uint8_t *c_ptr, *c_ptr1, *ptr;
493 /* NOTE: at end of line, the macroblock is handled as 420 */
494 idct_put(pixels, 8, block);
495 linesize = s->picture.linesize[6 - j];
496 c_ptr = s->picture.data[6 - j] + c_offset;
498 for(y = 0;y < 8; y++) {
499 /* convert to 411P */
500 c_ptr1 = c_ptr + 8*linesize;
501 c_ptr[0]= ptr[0]; c_ptr1[0]= ptr[4];
502 c_ptr[1]= ptr[1]; c_ptr1[1]= ptr[5];
503 c_ptr[2]= ptr[2]; c_ptr1[2]= ptr[6];
504 c_ptr[3]= ptr[3]; c_ptr1[3]= ptr[7];
509 /* don't ask me why they inverted Cb and Cr ! */
510 idct_put(s->picture.data[6 - j] + c_offset,
511 s->picture.linesize[6 - j], block);
520 /* Converts run and level (where level != 0) pair into vlc, returning bit size */
521 static inline int dv_rl2vlc(int run, int l, uint32_t* vlc)
524 int level = (l ^ sign) - sign;
529 if (run < 15 && level < 23 && dv_vlc_codes[run][level] != -1) {
530 *vlc = (dv_vlc_bits[dv_vlc_codes[run][level]] << 1) | sign;
531 size = dv_vlc_len[dv_vlc_codes[run][level]] + 1;
535 *vlc = (dv_vlc_bits[dv_vlc_codes[0][level]] << 1) | sign;
536 size = dv_vlc_len[dv_vlc_codes[0][level]] + 1;
538 *vlc = 0xfe00 | (level << 1) | sign;
547 *vlc |= ((0x7ce | (run - 1)) << size);
554 *vlc |= ((0xfac | (run - 3)) << size);
558 *vlc |= ((0x1f80 | (run - 1)) << size);
567 typedef struct EncBlockInfo {
574 const uint8_t* zigzag_scan;
578 static inline int dv_bits_left(EncBlockInfo* bi)
580 return (bi->block_size - get_bit_count(&bi->pb));
583 static inline void dv_encode_ac(EncBlockInfo* bi, PutBitContext* heap)
585 int i, level, size, run = 0;
587 PutBitContext* cpb = &bi->pb;
588 int bias = (bi->cno == 3);
590 for (i=1; i<64; i++) {
591 level = bi->mb[bi->zigzag_scan[i]] / (1<<(bi->dv_shift[i] + bias));
593 size = dv_rl2vlc(run, level, &vlc);
597 printf(" %3d:%3d", run, level);
599 if (cpb == &bi->pb && size > dv_bits_left(bi)) {
600 size -= dv_bits_left(bi);
601 put_bits(cpb, dv_bits_left(bi), vlc >> size);
602 vlc = vlc & ((1<<size)-1);
605 put_bits(cpb, size, vlc);
612 size = 4; vlc = 6; /* End Of Block stamp */
617 static inline void dv_redistr_bits(EncBlockInfo* bi, int count, uint8_t* extra_data, int extra_bits, PutBitContext* heap)
622 init_get_bits(&gb, extra_data, extra_bits);
624 for (i=0; i<count; i++) {
625 int bits_left = dv_bits_left(bi);
628 printf("------------> inserting %d bytes in %d:%d\n", bits_left, i/6, i%6);
630 if (bits_left > extra_bits) {
631 bit_copy(&bi->pb, &gb, extra_bits);
635 bit_copy(&bi->pb, &gb, bits_left);
637 extra_bits -= bits_left;
641 if (extra_bits > 0 && heap)
642 bit_copy(heap, &gb, extra_bits);
645 static inline void dv_set_class_number(EncBlockInfo* bi, int j)
649 for (i=1; i<64; i++) {
650 int ac = abs(bi->mb[ff_zigzag_direct[i]]) / 4;
656 else if (max_ac < 24)
658 else if (max_ac < 36)
667 #define SQ(a) ((a)*(a))
668 static int dv_score_lines(DCTELEM *s, int stride) {
674 score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
675 +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
684 * This is a very rough initial implementaion. The performance is
685 * horrible and the weighting is missing. But it's missing from the
686 * decoding step also -- so at least we're on the same page with decoder ;-)
688 static inline void dv_encode_video_segment(DVVideoDecodeContext *s,
690 const uint16_t *mb_pos_ptr)
692 int mb_index, i, j, v;
693 int mb_x, mb_y, c_offset, linesize;
698 EncBlockInfo enc_blks[5*6];
699 EncBlockInfo* enc_blk;
702 PutBitContext extra_vs;
703 uint8_t extra_vs_data[5*6*128];
704 uint8_t extra_mb_data[6*128];
708 /* Stage 1 -- doing DCT on 5 MBs */
709 block = &s->block[0][0];
710 enc_blk = &enc_blks[0];
711 for(mb_index = 0; mb_index < 5; mb_index++) {
715 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
716 c_offset = (s->sys->pix_fmt == PIX_FMT_YUV411P) ?
717 ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8)) :
718 (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
720 for(j = 0;j < 6; j++) {
721 if (j < 4) { /* Four Y blocks */
722 /* NOTE: at end of line, the macroblock is handled as 420 */
723 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
724 data = y_ptr + (j * 8);
726 data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
728 linesize = s->picture.linesize[0];
729 } else { /* Cr and Cb blocks */
730 /* don't ask Fabrice why they inverted Cb and Cr ! */
731 data = s->picture.data[6 - j] + c_offset;
732 linesize = s->picture.linesize[6 - j];
733 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
737 /* Everything is set up -- now just copy data -> DCT block */
738 if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */
742 d = data + 8 * linesize;
743 b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
744 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
748 } else { /* Simple copy: 8x8 -> 8x8 */
749 s->get_pixels(block, data, linesize);
752 if (dv_score_lines(block, 8) + dv_score_lines(block+8*4, 8) - 100 >
753 dv_score_lines(block, 16) + dv_score_lines(block+8, 16)) {
754 enc_blk->dct_mode = 1;
755 enc_blk->zigzag_scan = ff_zigzag248_direct;
757 enc_blk->dct_mode = 0;
758 enc_blk->zigzag_scan = ff_zigzag_direct;
761 enc_blk->block_size = block_sizes[j];
763 s->fdct[enc_blk->dct_mode](block);
765 dv_set_class_number(enc_blk, j/4*(j%2));
772 /* Stage 2 -- encoding by trial-and-error */
774 enc_blk = &enc_blks[0];
775 for (i=0; i<5; i++) {
776 uint8_t* p = dif + i*80 + 4;
777 for (j=0; j<6; j++) {
779 enc_blk->dv_shift = &(s->dv_dct_shift[0]
780 [QNO + dv_quant_offset[enc_blk->cno]][0]);
781 init_put_bits(&enc_blk->pb, p, block_sizes[j]/8);
783 p += block_sizes[j]/8;
787 init_put_bits(&extra_vs, extra_vs_data, sizeof(extra_vs_data));
789 enc_blk = &enc_blks[0];
790 for (i=0; i<5; i++) {
791 PutBitContext extra_mb;
792 EncBlockInfo* enc_blk2 = enc_blk;
793 int free_mb_bits = 0;
795 init_put_bits(&extra_mb, extra_mb_data, sizeof(extra_mb_data));
796 dif[i*80 + 3] = enc_blk->qno;
798 for (j=0; j<6; j++) {
799 uint16_t dc = ((enc_blk->mb[0] >> 3) - 1024) >> 2;
801 put_bits(&enc_blk->pb, 9, dc);
802 put_bits(&enc_blk->pb, 1, enc_blk->dct_mode);
803 put_bits(&enc_blk->pb, 2, enc_blk->cno);
806 printf("[%d, %d]: ", i, j);
808 dv_encode_ac(enc_blk, &extra_mb);
813 free_mb_bits += dv_bits_left(enc_blk);
817 /* We can't flush extra_mb just yet -- since it'll round up bit number */
818 extra_bits = get_bit_count(&extra_mb);
819 if (free_mb_bits > extra_bits)
820 free_vs_bits += free_mb_bits - extra_bits;
822 if (extra_bits) { /* FIXME: speed up things when free_mb_bits == 0 */
823 flush_put_bits(&extra_mb);
824 dv_redistr_bits(enc_blk2, 6, extra_mb_data, extra_bits, &extra_vs);
828 /* We can't flush extra_mb just yet -- since it'll round up bit number */
829 extra_bits = get_bit_count(&extra_vs);
830 if (extra_bits > free_vs_bits && QNO) { /* FIXME: very crude trial-and-error */
836 flush_put_bits(&extra_vs);
837 dv_redistr_bits(&enc_blks[0], 5*6, extra_vs_data, extra_bits, NULL);
840 for (i=0; i<6*5; i++) {
841 flush_put_bits(&enc_blks[i].pb);
843 printf("[%d:%d] qno=%d cno=%d\n", i/6, i%6, enc_blks[i].qno, enc_blks[i].cno);
848 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
849 144000 bytes for PAL) */
850 static int dvvideo_decode_frame(AVCodecContext *avctx,
851 void *data, int *data_size,
852 uint8_t *buf, int buf_size)
854 DVVideoDecodeContext *s = avctx->priv_data;
856 const uint16_t *mb_pos_ptr;
858 s->sys = dv_frame_profile(buf);
859 if (!s->sys || buf_size < s->sys->frame_size)
860 return -1; /* NOTE: we only accept several full frames */
863 if(s->picture.data[0])
864 avctx->release_buffer(avctx, &s->picture);
866 s->picture.reference = 0;
867 avctx->pix_fmt = s->sys->pix_fmt;
868 avctx->width = s->sys->width;
869 avctx->height = s->sys->height;
870 if(avctx->get_buffer(avctx, &s->picture) < 0) {
871 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
874 s->picture.interlaced_frame = 1;
875 s->picture.top_field_first = 0;
877 /* for each DIF segment */
878 mb_pos_ptr = s->sys->video_place;
879 for (ds = 0; ds < s->sys->difseg_size; ds++) {
880 buf += 6 * 80; /* skip DIF segment header */
882 for(vs = 0; vs < 27; vs++) {
884 buf += 80; /* skip audio block */
887 printf("********************* %d, %d **********************\n", ds, vs);
889 dv_decode_video_segment(s, buf, mb_pos_ptr);
898 *data_size = sizeof(AVFrame);
899 *(AVFrame*)data= s->picture;
901 return s->sys->frame_size;
904 static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
907 DVVideoDecodeContext *s = c->priv_data;
908 const uint16_t *mb_pos_ptr;
911 s->sys = dv_codec_profile(c);
915 c->pix_fmt = s->sys->pix_fmt;
916 s->picture = *((AVFrame *)data);
918 /* for each DIF segment */
919 mb_pos_ptr = s->sys->video_place;
920 for (ds = 0; ds < s->sys->difseg_size; ds++) {
921 buf += 6 * 80; /* skip DIF segment header */
923 for(vs = 0; vs < 27; vs++) {
925 buf += 80; /* skip audio block */
928 printf("********************* %d, %d **********************\n", ds, vs);
930 dv_encode_video_segment(s, buf, mb_pos_ptr);
937 return s->sys->frame_size;
940 static int dvvideo_end(AVCodecContext *avctx)
942 avcodec_default_free_buffers(avctx);
946 AVCodec dvvideo_decoder = {
950 sizeof(DVVideoDecodeContext),
952 dvvideo_encode_frame,
954 dvvideo_decode_frame,