comparison dv.c @ 723:50f7e3bef20f libavcodec

first working version of DV video decoder
author bellard
date Thu, 03 Oct 2002 21:07:39 +0000
parents
children eac6c71ef30f
comparison
equal deleted inserted replaced
722:ff90043f4a2d 723:50f7e3bef20f
1 /*
2 * DV decoder
3 * Copyright (c) 2002 Fabrice Bellard.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "avcodec.h"
20 #include "dsputil.h"
21 #include "mpegvideo.h"
22 #include "simple_idct.h"
23
24 #define NTSC_FRAME_SIZE 120000
25 #define PAL_FRAME_SIZE 144000
26
27 #define TEX_VLC_BITS 9
28
29 typedef struct DVVideoDecodeContext {
30 AVCodecContext *avctx;
31 GetBitContext gb;
32 VLC *vlc;
33 int sampling_411; /* 0 = 420, 1 = 411 */
34 int width, height;
35 UINT8 *current_picture[3]; /* picture structure */
36 int linesize[3];
37 DCTELEM block[5*6][64] __align8;
38 UINT8 dv_zigzag[2][64];
39 /* XXX: move it to static storage ? */
40 UINT8 dv_shift[2][2][22][64];
41 void (*idct_put[2])(UINT8 *dest, int line_size, DCTELEM *block);
42 } DVVideoDecodeContext;
43
44 #include "dvdata.h"
45
46 static VLC dv_vlc;
47 /* XXX: also include quantization */
48 static RL_VLC_ELEM *dv_rl_vlc[1];
49
50 static void dv_build_unquantize_tables(DVVideoDecodeContext *s)
51 {
52 int i, e, q;
53
54 /* NOTE: max left shift is 6 */
55 for(e = 0; e < 2; e++) {
56 for(q = 0; q < 22; q++) {
57 /* 88 unquant */
58 for(i = 1; i < 64; i++) {
59 /* 88 table */
60 s->dv_shift[0][e][q][i] =
61 dv_quant_shifts[q][dv_88_areas[i]] + e + 1;
62 }
63
64 /* 248 unquant */
65 for(i = 1; i < 64; i++) {
66 /* 248 table */
67 s->dv_shift[1][e][q][i] =
68 dv_quant_shifts[q][dv_248_areas[i]] + e + 1;
69 }
70 }
71 }
72 }
73
74 static int dvvideo_decode_init(AVCodecContext *avctx)
75 {
76 DVVideoDecodeContext *s = avctx->priv_data;
77 static int done;
78
79 if (!done) {
80 int i;
81
82 done = 1;
83
84 /* NOTE: as a trick, we use the fact the no codes are unused
85 to accelerate the parsing of partial codes */
86 init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC,
87 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
88
89 dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
90 for(i = 0; i < dv_vlc.table_size; i++){
91 int code= dv_vlc.table[i][0];
92 int len = dv_vlc.table[i][1];
93 int level, run;
94
95 if(len<0){ //more bits needed
96 run= 0;
97 level= code;
98 } else if (code == (NB_DV_VLC - 1)) {
99 /* EOB */
100 run = 0;
101 level = 256;
102 } else {
103 run= dv_vlc_run[code] + 1;
104 level= dv_vlc_level[code];
105 }
106 dv_rl_vlc[0][i].len = len;
107 dv_rl_vlc[0][i].level = level;
108 dv_rl_vlc[0][i].run = run;
109 }
110 }
111 /* XXX: do it only for constant case */
112 dv_build_unquantize_tables(s);
113 {
114 /* XXX: fix that : use mmx when possible */
115 s->idct_put[0] = simple_idct_put;
116 s->idct_put[1] = simple_idct248_put;
117
118 memcpy(s->dv_zigzag[0], ff_zigzag_direct, 64);
119 memcpy(s->dv_zigzag[1], dv_248_zigzag, 64);
120 }
121 return 0;
122 }
123
124 //#define VLC_DEBUG
125
126 typedef struct MBInfo {
127 const UINT8 *shift_table[6];
128 const UINT8 *scan_table[6];
129 UINT8 pos[6]; /* position in block */
130 UINT8 eob_reached[6]; /* true if EOB has been reached */
131 UINT8 dct_mode[6];
132 UINT8 partial_bit_count[6];
133 UINT16 partial_bit_buffer[6];
134 UINT8 bit_buffer[80 + 4]; /* allow some slack */
135 int bits_left;
136 } MBInfo;
137
138 /* block size in bits */
139 const static UINT16 block_sizes[6] = {
140 112, 112, 112, 112, 80, 80
141 };
142
143 #ifndef ALT_BITSTREAM_READER
144 #error only works with ALT_BITSTREAM_READER
145 #endif
146
147 /* decode ac coefs */
148 static void dv_decode_ac(DVVideoDecodeContext *s,
149 MBInfo *mb, INT16 *block, int j, int last_index)
150 {
151 int last_re_index;
152 const UINT8 *scan_table = mb->scan_table[j];
153 const UINT8 *shift_table = mb->shift_table[j];
154 int pos = mb->pos[j];
155 int level, pos1, sign, run;
156 int partial_bit_count;
157
158 OPEN_READER(re, &s->gb);
159
160 #ifdef VLC_DEBUG
161 printf("start %d\n", j);
162 #endif
163
164 /* if we must parse a partial vlc, we do it here */
165 partial_bit_count = mb->partial_bit_count[j];
166 if (partial_bit_count > 0) {
167 UINT8 buf[4];
168 UINT32 v;
169 int l, l1;
170 GetBitContext gb1;
171
172 /* build the dummy bit buffer */
173 l = 16 - partial_bit_count;
174 UPDATE_CACHE(re, &s->gb);
175 #ifdef VLC_DEBUG
176 printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16));
177 #endif
178 v = (mb->partial_bit_buffer[j] << l) | SHOW_UBITS(re, &s->gb, l);
179 buf[0] = v >> 8;
180 buf[1] = v;
181 #ifdef VLC_DEBUG
182 printf("v=%04x cnt=%d %04x\n",
183 v, partial_bit_count, (mb->partial_bit_buffer[j] << l));
184 #endif
185 /* try to read the codeword */
186 init_get_bits(&gb1, buf, 4);
187 {
188 OPEN_READER(re1, &gb1);
189 UPDATE_CACHE(re1, &gb1);
190 GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0],
191 TEX_VLC_BITS, 2);
192 l = re1_index;
193 CLOSE_READER(re1, &gb1);
194 }
195 #ifdef VLC_DEBUG
196 printf("****run=%d level=%d size=%d\n", run, level, l);
197 #endif
198 /* compute codeword length */
199 l1 = (level != 256 && level != 0);
200 /* if too long, we cannot parse */
201 l -= partial_bit_count;
202 if ((re_index + l + l1) > last_index)
203 return;
204 /* skip read bits */
205 last_re_index = 0; /* avoid warning */
206 re_index += l;
207 /* by redefinition, if we can read the vlc, all partial bits
208 will be read (otherwise we could have read the vlc before) */
209 mb->partial_bit_count[j] = 0;
210 UPDATE_CACHE(re, &s->gb);
211 goto handle_vlc;
212 }
213
214 /* get the AC coefficients until last_index is reached */
215 for(;;) {
216 UPDATE_CACHE(re, &s->gb);
217 #ifdef VLC_DEBUG
218 printf("%2d: bits=%04x index=%d\n",
219 pos, SHOW_UBITS(re, &s->gb, 16), re_index);
220 #endif
221 last_re_index = re_index;
222 GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0],
223 TEX_VLC_BITS, 2);
224 handle_vlc:
225 #ifdef VLC_DEBUG
226 printf("run=%d level=%d\n", run, level);
227 #endif
228 if (level == 256) {
229 if (re_index > last_index) {
230 cannot_read:
231 /* put position before read code */
232 re_index = last_re_index;
233 mb->eob_reached[j] = 0;
234 break;
235 }
236 /* EOB */
237 mb->eob_reached[j] = 1;
238 break;
239 } else if (level != 0) {
240 if ((re_index + 1) > last_index)
241 goto cannot_read;
242 sign = SHOW_SBITS(re, &s->gb, 1);
243 level = (level ^ sign) - sign;
244 LAST_SKIP_BITS(re, &s->gb, 1);
245 pos += run;
246 /* error */
247 if (pos >= 64) {
248 goto read_error;
249 }
250 pos1 = scan_table[pos];
251 level = level << shift_table[pos1];
252 block[pos1] = level;
253 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]);
254 } else {
255 if (re_index > last_index)
256 goto cannot_read;
257 /* level is zero: means run without coding. No
258 sign is coded */
259 pos += run;
260 /* error */
261 if (pos >= 64) {
262 read_error:
263 #if defined(VLC_DEBUG) || 1
264 printf("error pos=%d\n", pos);
265 #endif
266 /* for errors, we consider the eob is reached */
267 mb->eob_reached[j] = 1;
268 break;
269 }
270 }
271 }
272 CLOSE_READER(re, &s->gb);
273 mb->pos[j] = pos;
274 }
275
276 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left)
277 {
278 while (bits_left >= 16) {
279 put_bits(pb, 16, get_bits(gb, 16));
280 bits_left -= 16;
281 }
282 if (bits_left > 0) {
283 put_bits(pb, bits_left, get_bits(gb, bits_left));
284 }
285 }
286
287 /* mb_x and mb_y are in units of 8 pixels */
288 static inline void dv_decode_video_segment(DVVideoDecodeContext *s,
289 UINT8 *buf_ptr1,
290 const UINT16 *mb_pos_ptr)
291 {
292 int quant, dc, dct_mode, class1, j;
293 int mb_index, mb_x, mb_y, v, last_index;
294 DCTELEM *block, *block1;
295 int c_offset, bits_left;
296 UINT8 *y_ptr;
297 MBInfo mb_data[5], *mb;
298 void (*idct_put)(UINT8 *dest, int line_size, DCTELEM *block);
299 UINT8 *buf_ptr;
300 PutBitContext pb, vs_pb;
301 UINT8 vs_bit_buffer[5 * 80 + 4];
302 int vs_bit_count;
303
304 memset(s->block, 0, sizeof(s->block));
305
306 /* pass 1 : read DC and AC coefficients in blocks */
307 buf_ptr = buf_ptr1;
308 block1 = &s->block[0][0];
309 mb = mb_data;
310 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80, NULL, NULL);
311 vs_bit_count = 0;
312 for(mb_index = 0; mb_index < 5; mb_index++) {
313 /* skip header */
314 quant = buf_ptr[3] & 0x0f;
315 buf_ptr += 4;
316 init_put_bits(&pb, mb->bit_buffer, 80, NULL, NULL);
317 mb->bits_left = 0;
318 block = block1;
319 for(j = 0;j < 6; j++) {
320 /* NOTE: size is not important here */
321 init_get_bits(&s->gb, buf_ptr, 14);
322
323 /* get the dc */
324 dc = get_bits(&s->gb, 9);
325 dc = (dc << (32 - 9)) >> (32 - 9);
326 dct_mode = get_bits1(&s->gb);
327 mb->dct_mode[j] = dct_mode;
328 mb->scan_table[j] = s->dv_zigzag[dct_mode];
329 class1 = get_bits(&s->gb, 2);
330 mb->shift_table[j] = s->dv_shift[dct_mode][class1 == 3]
331 [quant + dv_quant_offset[class1]];
332 dc = dc << 2;
333 /* convert to unsigned because 128 is not added in the
334 standard IDCT */
335 dc += 1024;
336 block[0] = dc;
337 last_index = block_sizes[j];
338 buf_ptr += last_index >> 3;
339 mb->pos[j] = 0;
340 mb->partial_bit_count[j] = 0;
341
342 dv_decode_ac(s, mb, block, j, last_index);
343
344 /* write the remaining bits in a new buffer only if the
345 block is finished */
346 bits_left = last_index - s->gb.index;
347 if (mb->eob_reached[j]) {
348 mb->partial_bit_count[j] = 0;
349 mb->bits_left += bits_left;
350 bit_copy(&pb, &s->gb, bits_left);
351 } else {
352 /* should be < 16 bits otherwise a codeword could have
353 been parsed */
354 mb->partial_bit_count[j] = bits_left;
355 mb->partial_bit_buffer[j] = get_bits(&s->gb, bits_left);
356 }
357 block += 64;
358 }
359
360 flush_put_bits(&pb);
361
362 /* pass 2 : we can do it just after */
363 #ifdef VLC_DEBUG
364 printf("***pass 2 size=%d\n", mb->bits_left);
365 #endif
366 block = block1;
367 init_get_bits(&s->gb, mb->bit_buffer, 80);
368 for(j = 0;j < 6; j++) {
369 if (!mb->eob_reached[j] && s->gb.index < mb->bits_left) {
370 dv_decode_ac(s, mb, block, j, mb->bits_left);
371 /* if still not finished, no need to parse other blocks */
372 if (!mb->eob_reached[j]) {
373 /* we could not parse the current AC coefficient,
374 so we add the remaining bytes */
375 bits_left = mb->bits_left - s->gb.index;
376 if (bits_left > 0) {
377 mb->partial_bit_count[j] += bits_left;
378 mb->partial_bit_buffer[j] =
379 (mb->partial_bit_buffer[j] << bits_left) |
380 get_bits(&s->gb, bits_left);
381 }
382 goto next_mb;
383 }
384 }
385 block += 64;
386 }
387 /* all blocks are finished, so the extra bytes can be used at
388 the video segment level */
389 bits_left = mb->bits_left - s->gb.index;
390 vs_bit_count += bits_left;
391 bit_copy(&vs_pb, &s->gb, bits_left);
392 next_mb:
393 mb++;
394 block1 += 6 * 64;
395 }
396
397 /* we need a pass other the whole video segment */
398 flush_put_bits(&vs_pb);
399
400 #ifdef VLC_DEBUG
401 printf("***pass 3 size=%d\n", vs_bit_count);
402 #endif
403 block = &s->block[0][0];
404 mb = mb_data;
405 init_get_bits(&s->gb, vs_bit_buffer, 5 * 80);
406 for(mb_index = 0; mb_index < 5; mb_index++) {
407 for(j = 0;j < 6; j++) {
408 if (!mb->eob_reached[j]) {
409 #ifdef VLC_DEBUG
410 printf("start %d:%d\n", mb_index, j);
411 #endif
412 dv_decode_ac(s, mb, block, j, vs_bit_count);
413 }
414 block += 64;
415 }
416 mb++;
417 }
418
419 /* compute idct and place blocks */
420 block = &s->block[0][0];
421 mb = mb_data;
422 for(mb_index = 0; mb_index < 5; mb_index++) {
423 v = *mb_pos_ptr++;
424 mb_x = v & 0xff;
425 mb_y = v >> 8;
426 y_ptr = s->current_picture[0] + (mb_y * s->linesize[0] * 8) + (mb_x * 8);
427 if (s->sampling_411)
428 c_offset = (mb_y * s->linesize[1] * 8) + ((mb_x >> 2) * 8);
429 else
430 c_offset = ((mb_y >> 1) * s->linesize[1] * 8) + ((mb_x >> 1) * 8);
431 for(j = 0;j < 6; j++) {
432 idct_put = s->idct_put[mb->dct_mode[j]];
433 if (j < 4) {
434 if (s->sampling_411) {
435 idct_put(y_ptr + (j * 8), s->linesize[0], block);
436 } else {
437 idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->linesize[0]),
438 s->linesize[0], block);
439 }
440 } else {
441 /* don't ask me why they inverted Cb and Cr ! */
442 idct_put(s->current_picture[6 - j] + c_offset,
443 s->linesize[6 - j], block);
444 }
445 block += 64;
446 }
447 mb++;
448 }
449 }
450
451
452 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
453 144000 bytes for PAL) */
454 static int dvvideo_decode_frame(AVCodecContext *avctx,
455 void *data, int *data_size,
456 UINT8 *buf, int buf_size)
457 {
458 DVVideoDecodeContext *s = avctx->priv_data;
459 int sct, dsf, apt, ds, nb_dif_segs, vs, size, width, height, i;
460 UINT8 *buf_ptr;
461 const UINT16 *mb_pos_ptr;
462 AVPicture *picture;
463
464 /* parse id */
465 init_get_bits(&s->gb, buf, buf_size);
466 sct = get_bits(&s->gb, 3);
467 if (sct != 0)
468 return -1;
469 skip_bits(&s->gb, 5);
470 get_bits(&s->gb, 4); /* dsn (sequence number */
471 get_bits(&s->gb, 1); /* fsc (channel number) */
472 skip_bits(&s->gb, 3);
473 get_bits(&s->gb, 8); /* dbn (diff block number 0-134) */
474
475 dsf = get_bits(&s->gb, 1); /* 0 = NTSC 1 = PAL */
476 if (get_bits(&s->gb, 1) != 0)
477 return -1;
478 skip_bits(&s->gb, 11);
479 apt = get_bits(&s->gb, 3); /* apt */
480
481 get_bits(&s->gb, 1); /* tf1 */
482 skip_bits(&s->gb, 4);
483 get_bits(&s->gb, 3); /* ap1 */
484
485 get_bits(&s->gb, 1); /* tf2 */
486 skip_bits(&s->gb, 4);
487 get_bits(&s->gb, 3); /* ap2 */
488
489 get_bits(&s->gb, 1); /* tf3 */
490 skip_bits(&s->gb, 4);
491 get_bits(&s->gb, 3); /* ap3 */
492
493 /* init size */
494 width = 720;
495 if (dsf) {
496 if (buf_size != PAL_FRAME_SIZE)
497 return -1;
498 height = 576;
499 nb_dif_segs = 12;
500 } else {
501 if (buf_size != NTSC_FRAME_SIZE)
502 return -1;
503 height = 480;
504 nb_dif_segs = 10;
505 }
506
507 /* (re)alloc picture if needed */
508 if (s->width != width || s->height != height) {
509 for(i=0;i<3;i++)
510 av_freep(&s->current_picture[i]);
511 for(i=0;i<3;i++) {
512 size = width * height;
513 s->linesize[i] = width;
514 if (i >= 1) {
515 size >>= 2;
516 s->linesize[i] >>= 1;
517 }
518 s->current_picture[i] = av_malloc(size);
519 if (!s->current_picture[i])
520 return -1;
521 }
522 s->width = width;
523 s->height = height;
524 }
525
526 /* XXX: is it correct to assume that 420 is always used in PAL
527 mode ? */
528 s->sampling_411 = !dsf;
529 if (s->sampling_411)
530 mb_pos_ptr = dv_place_411;
531 else
532 mb_pos_ptr = dv_place_420;
533
534 /* for each DIF segment */
535 buf_ptr = buf;
536 for (ds = 0; ds < nb_dif_segs; ds++) {
537 buf_ptr += 6 * 80; /* skip DIF segment header */
538
539 for(vs = 0; vs < 27; vs++) {
540 if ((vs % 3) == 0) {
541 /* skip audio block */
542 buf_ptr += 80;
543 }
544 dv_decode_video_segment(s, buf_ptr, mb_pos_ptr);
545 buf_ptr += 5 * 80;
546 mb_pos_ptr += 5;
547 }
548 }
549
550 /* return image */
551 avctx->width = width;
552 avctx->height = height;
553 if (s->sampling_411)
554 avctx->pix_fmt = PIX_FMT_YUV420P; /* XXX: incorrect, add PIX_FMT_YUV411P */
555 else
556 avctx->pix_fmt = PIX_FMT_YUV420P;
557 if (dsf)
558 avctx->frame_rate = 25 * FRAME_RATE_BASE;
559 else
560 avctx->frame_rate = 30 * FRAME_RATE_BASE;
561 *data_size = sizeof(AVPicture);
562 picture = data;
563 for(i=0;i<3;i++) {
564 picture->data[i] = s->current_picture[i];
565 picture->linesize[i] = s->linesize[i];
566 }
567 return buf_size;
568 }
569
570 static int dvvideo_decode_end(AVCodecContext *avctx)
571 {
572 DVVideoDecodeContext *s = avctx->priv_data;
573 int i;
574
575 for(i=0;i<3;i++)
576 av_freep(&s->current_picture[i]);
577 return 0;
578 }
579
580 AVCodec dvvideo_decoder = {
581 "dvvideo",
582 CODEC_TYPE_VIDEO,
583 CODEC_ID_DVVIDEO,
584 sizeof(DVVideoDecodeContext),
585 dvvideo_decode_init,
586 NULL,
587 dvvideo_decode_end,
588 dvvideo_decode_frame,
589 0,
590 NULL
591 };
592
593 typedef struct DVAudioDecodeContext {
594 AVCodecContext *avctx;
595 GetBitContext gb;
596
597 } DVAudioDecodeContext;
598
599 static int dvaudio_decode_init(AVCodecContext *avctx)
600 {
601 // DVAudioDecodeContext *s = avctx->priv_data;
602 return 0;
603 }
604
605 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
606 144000 bytes for PAL) */
607 static int dvaudio_decode_frame(AVCodecContext *avctx,
608 void *data, int *data_size,
609 UINT8 *buf, int buf_size)
610 {
611 // DVAudioDecodeContext *s = avctx->priv_data;
612 return buf_size;
613 }
614
615 static int dvaudio_decode_end(AVCodecContext *avctx)
616 {
617 // DVAudioDecodeContext *s = avctx->priv_data;
618 return 0;
619 }
620
621 AVCodec dvaudio_decoder = {
622 "dvaudio",
623 CODEC_TYPE_AUDIO,
624 CODEC_ID_DVAUDIO,
625 sizeof(DVAudioDecodeContext),
626 dvaudio_decode_init,
627 NULL,
628 dvaudio_decode_end,
629 dvaudio_decode_frame,
630 0,
631 NULL
632 };