37e9b70ac50ca4fa5d5f9783ffdba7a8750384e0
[libav.git] / libavcodec / mpeg12.c
1 /*
2 * MPEG1 encoder / MPEG2 decoder
3 * Copyright (c) 2000,2001 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 //#define DEBUG
20 #include "avcodec.h"
21 #include "dsputil.h"
22 #include "mpegvideo.h"
23
24 #include "mpeg12data.h"
25
26 /* Start codes. */
27 #define SEQ_END_CODE 0x000001b7
28 #define SEQ_START_CODE 0x000001b3
29 #define GOP_START_CODE 0x000001b8
30 #define PICTURE_START_CODE 0x00000100
31 #define SLICE_MIN_START_CODE 0x00000101
32 #define SLICE_MAX_START_CODE 0x000001af
33 #define EXT_START_CODE 0x000001b5
34 #define USER_START_CODE 0x000001b2
35
36 static void mpeg1_encode_block(MpegEncContext *s,
37 DCTELEM *block,
38 int component);
39 static void mpeg1_encode_motion(MpegEncContext *s, int val);
40 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num);
41 static int mpeg1_decode_block(MpegEncContext *s,
42 DCTELEM *block,
43 int n);
44 static int mpeg2_decode_block_non_intra(MpegEncContext *s,
45 DCTELEM *block,
46 int n);
47 static int mpeg2_decode_block_intra(MpegEncContext *s,
48 DCTELEM *block,
49 int n);
50 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
51
52 static UINT16 mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
53 static UINT8 fcode_tab[MAX_MV*2+1];
54
55 static void put_header(MpegEncContext *s, int header)
56 {
57 align_put_bits(&s->pb);
58 put_bits(&s->pb, 16, header>>16);
59 put_bits(&s->pb, 16, header&0xFFFF);
60 }
61
62 /* put sequence header if needed */
63 static void mpeg1_encode_sequence_header(MpegEncContext *s)
64 {
65 unsigned int vbv_buffer_size;
66 unsigned int fps, v;
67 int n;
68 UINT64 time_code;
69
70 if (s->picture_in_gop_number == 0) {
71 /* mpeg1 header repeated every gop */
72 put_header(s, SEQ_START_CODE);
73
74 /* search closest frame rate */
75 {
76 int i, dmin, d;
77 s->frame_rate_index = 0;
78 dmin = 0x7fffffff;
79 for(i=1;i<9;i++) {
80 d = abs(s->frame_rate - frame_rate_tab[i]);
81 if (d < dmin) {
82 dmin = d;
83 s->frame_rate_index = i;
84 }
85 }
86 }
87
88 put_bits(&s->pb, 12, s->width);
89 put_bits(&s->pb, 12, s->height);
90 put_bits(&s->pb, 4, 1); /* 1/1 aspect ratio */
91 put_bits(&s->pb, 4, s->frame_rate_index);
92 v = s->bit_rate / 400;
93 if (v > 0x3ffff)
94 v = 0x3ffff;
95 put_bits(&s->pb, 18, v);
96 put_bits(&s->pb, 1, 1); /* marker */
97 /* vbv buffer size: slightly greater than an I frame. We add
98 some margin just in case */
99 vbv_buffer_size = (3 * s->I_frame_bits) / (2 * 8);
100 put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384);
101 put_bits(&s->pb, 1, 1); /* constrained parameter flag */
102 put_bits(&s->pb, 1, 0); /* no custom intra matrix */
103 put_bits(&s->pb, 1, 0); /* no custom non intra matrix */
104
105 put_header(s, GOP_START_CODE);
106 put_bits(&s->pb, 1, 0); /* do drop frame */
107 /* time code : we must convert from the real frame rate to a
108 fake mpeg frame rate in case of low frame rate */
109 fps = frame_rate_tab[s->frame_rate_index];
110 time_code = (INT64)s->fake_picture_number * FRAME_RATE_BASE;
111 s->gop_picture_number = s->fake_picture_number;
112 put_bits(&s->pb, 5, (UINT32)((time_code / (fps * 3600)) % 24));
113 put_bits(&s->pb, 6, (UINT32)((time_code / (fps * 60)) % 60));
114 put_bits(&s->pb, 1, 1);
115 put_bits(&s->pb, 6, (UINT32)((time_code / fps) % 60));
116 put_bits(&s->pb, 6, (UINT32)((time_code % fps) / FRAME_RATE_BASE));
117 put_bits(&s->pb, 1, 1); /* closed gop */
118 put_bits(&s->pb, 1, 0); /* broken link */
119 }
120
121 if (s->frame_rate < (24 * FRAME_RATE_BASE) && s->picture_number > 0) {
122 /* insert empty P pictures to slow down to the desired
123 frame rate. Each fake pictures takes about 20 bytes */
124 fps = frame_rate_tab[s->frame_rate_index];
125 n = (((INT64)s->picture_number * fps) / s->frame_rate) - 1;
126 while (s->fake_picture_number < n) {
127 mpeg1_skip_picture(s, s->fake_picture_number -
128 s->gop_picture_number);
129 s->fake_picture_number++;
130 }
131
132 }
133 }
134
135
136 /* insert a fake P picture */
137 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num)
138 {
139 unsigned int mb_incr;
140
141 /* mpeg1 picture header */
142 put_header(s, PICTURE_START_CODE);
143 /* temporal reference */
144 put_bits(&s->pb, 10, pict_num & 0x3ff);
145
146 put_bits(&s->pb, 3, P_TYPE);
147 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
148
149 put_bits(&s->pb, 1, 1); /* integer coordinates */
150 put_bits(&s->pb, 3, 1); /* forward_f_code */
151
152 put_bits(&s->pb, 1, 0); /* extra bit picture */
153
154 /* only one slice */
155 put_header(s, SLICE_MIN_START_CODE);
156 put_bits(&s->pb, 5, 1); /* quantizer scale */
157 put_bits(&s->pb, 1, 0); /* slice extra information */
158
159 mb_incr = 1;
160 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
161 mbAddrIncrTable[mb_incr - 1][0]);
162
163 /* empty macroblock */
164 put_bits(&s->pb, 3, 1); /* motion only */
165
166 /* zero motion x & y */
167 put_bits(&s->pb, 1, 1);
168 put_bits(&s->pb, 1, 1);
169
170 /* output a number of empty slice */
171 mb_incr = s->mb_width * s->mb_height - 1;
172 while (mb_incr > 33) {
173 put_bits(&s->pb, 11, 0x008);
174 mb_incr -= 33;
175 }
176 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
177 mbAddrIncrTable[mb_incr - 1][0]);
178
179 /* empty macroblock */
180 put_bits(&s->pb, 3, 1); /* motion only */
181
182 /* zero motion x & y */
183 put_bits(&s->pb, 1, 1);
184 put_bits(&s->pb, 1, 1);
185 }
186
187 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
188 {
189 static int done=0;
190
191 if (!done) {
192 int i;
193 done = 1;
194 init_rl(&rl_mpeg1);
195
196 for(i=0; i<64; i++)
197 {
198 mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
199 mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
200 }
201
202 /* build unified dc encoding tables */
203 for(i=-255; i<256; i++)
204 {
205 int adiff, index;
206 int bits, code;
207 int diff=i;
208
209 adiff = ABS(diff);
210 if(diff<0) diff--;
211 index = vlc_dc_table[adiff];
212
213 bits= vlc_dc_lum_bits[index] + index;
214 code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
215 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
216
217 bits= vlc_dc_chroma_bits[index] + index;
218 code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
219 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
220 }
221 }
222 mpeg1_encode_sequence_header(s);
223
224 /* mpeg1 picture header */
225 put_header(s, PICTURE_START_CODE);
226 /* temporal reference */
227 put_bits(&s->pb, 10, (s->fake_picture_number -
228 s->gop_picture_number) & 0x3ff);
229 s->fake_picture_number++;
230
231 put_bits(&s->pb, 3, s->pict_type);
232 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
233
234 if (s->pict_type == P_TYPE) {
235 put_bits(&s->pb, 1, 0); /* half pel coordinates */
236 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
237 }
238
239 put_bits(&s->pb, 1, 0); /* extra bit picture */
240
241 /* only one slice */
242 put_header(s, SLICE_MIN_START_CODE);
243 put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
244 put_bits(&s->pb, 1, 0); /* slice extra information */
245 }
246
247 void mpeg1_encode_mb(MpegEncContext *s,
248 DCTELEM block[6][64],
249 int motion_x, int motion_y)
250 {
251 int mb_incr, i, cbp, mb_x, mb_y;
252
253 mb_x = s->mb_x;
254 mb_y = s->mb_y;
255
256 /* compute cbp */
257 cbp = 0;
258 for(i=0;i<6;i++) {
259 if (s->block_last_index[i] >= 0)
260 cbp |= 1 << (5 - i);
261 }
262
263 /* skip macroblock, except if first or last macroblock of a slice */
264 if ((cbp | motion_x | motion_y) == 0 &&
265 (!((mb_x | mb_y) == 0 ||
266 (mb_x == s->mb_width - 1 && mb_y == s->mb_height - 1)))) {
267 s->mb_incr++;
268 } else {
269 /* output mb incr */
270 mb_incr = s->mb_incr;
271
272 while (mb_incr > 33) {
273 put_bits(&s->pb, 11, 0x008);
274 mb_incr -= 33;
275 }
276 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
277 mbAddrIncrTable[mb_incr - 1][0]);
278
279 if (s->pict_type == I_TYPE) {
280 put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */
281 } else {
282 if (s->mb_intra) {
283 put_bits(&s->pb, 5, 0x03);
284 } else {
285 if (cbp != 0) {
286 if (motion_x == 0 && motion_y == 0) {
287 put_bits(&s->pb, 2, 1); /* macroblock_pattern only */
288 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
289 } else {
290 put_bits(&s->pb, 1, 1); /* motion + cbp */
291 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]);
292 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]);
293 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
294 }
295 } else {
296 put_bits(&s->pb, 3, 1); /* motion only */
297 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]);
298 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]);
299 }
300 }
301 }
302 for(i=0;i<6;i++) {
303 if (cbp & (1 << (5 - i))) {
304 mpeg1_encode_block(s, block[i], i);
305 }
306 }
307 s->mb_incr = 1;
308 }
309 s->last_mv[0][0][0] = motion_x;
310 s->last_mv[0][0][1] = motion_y;
311 }
312
313 static void mpeg1_encode_motion(MpegEncContext *s, int val)
314 {
315 int code, bit_size, l, m, bits, range, sign;
316
317 if (val == 0) {
318 /* zero vector */
319 code = 0;
320 put_bits(&s->pb,
321 mbMotionVectorTable[0][1],
322 mbMotionVectorTable[0][0]);
323 } else {
324 bit_size = s->f_code - 1;
325 range = 1 << bit_size;
326 /* modulo encoding */
327 l = 16 * range;
328 m = 2 * l;
329 if (val < -l) {
330 val += m;
331 } else if (val >= l) {
332 val -= m;
333 }
334
335 if (val >= 0) {
336 val--;
337 code = (val >> bit_size) + 1;
338 bits = val & (range - 1);
339 sign = 0;
340 } else {
341 val = -val;
342 val--;
343 code = (val >> bit_size) + 1;
344 bits = val & (range - 1);
345 sign = 1;
346 }
347 put_bits(&s->pb,
348 mbMotionVectorTable[code][1],
349 mbMotionVectorTable[code][0]);
350 put_bits(&s->pb, 1, sign);
351 if (bit_size > 0) {
352 put_bits(&s->pb, bit_size, bits);
353 }
354 }
355 }
356
357 void mpeg1_encode_init(MpegEncContext *s)
358 {
359 static int done=0;
360 if(!done){
361 int f_code;
362 int mv;
363
364 done=1;
365 for(f_code=1; f_code<=MAX_FCODE; f_code++){
366 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
367 int len;
368
369 if(mv==0) len= mbMotionVectorTable[0][1];
370 else{
371 int val, bit_size, range, code;
372
373 bit_size = s->f_code - 1;
374 range = 1 << bit_size;
375
376 val=mv;
377 if (val < 0)
378 val = -val;
379 val--;
380 code = (val >> bit_size) + 1;
381 if(code<17){
382 len= mbMotionVectorTable[code][1] + 1 + bit_size;
383 }else{
384 len= mbMotionVectorTable[16][1] + 2 + bit_size;
385 }
386 }
387
388 mv_penalty[f_code][mv+MAX_MV]= len;
389 }
390 }
391
392
393 for(f_code=MAX_FCODE; f_code>0; f_code--){
394 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
395 fcode_tab[mv+MAX_MV]= f_code;
396 }
397 }
398 }
399 s->mv_penalty= mv_penalty;
400 s->fcode_tab= fcode_tab;
401 s->min_qcoeff=-255;
402 s->max_qcoeff= 255;
403 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
404 s->inter_quant_bias= 0;
405 }
406
407 static inline void encode_dc(MpegEncContext *s, int diff, int component)
408 {
409 if (component == 0) {
410 put_bits(
411 &s->pb,
412 mpeg1_lum_dc_uni[diff+255]&0xFF,
413 mpeg1_lum_dc_uni[diff+255]>>8);
414 } else {
415 put_bits(
416 &s->pb,
417 mpeg1_chr_dc_uni[diff+255]&0xFF,
418 mpeg1_chr_dc_uni[diff+255]>>8);
419 }
420 }
421
422 static void mpeg1_encode_block(MpegEncContext *s,
423 DCTELEM *block,
424 int n)
425 {
426 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
427 int code, component;
428 // RLTable *rl = &rl_mpeg1;
429
430 last_index = s->block_last_index[n];
431
432 /* DC coef */
433 if (s->mb_intra) {
434 component = (n <= 3 ? 0 : n - 4 + 1);
435 dc = block[0]; /* overflow is impossible */
436 diff = dc - s->last_dc[component];
437 encode_dc(s, diff, component);
438 s->last_dc[component] = dc;
439 i = 1;
440 } else {
441 /* encode the first coefficient : needs to be done here because
442 it is handled slightly differently */
443 level = block[0];
444 if (abs(level) == 1) {
445 code = ((UINT32)level >> 31); /* the sign bit */
446 put_bits(&s->pb, 2, code | 0x02);
447 i = 1;
448 } else {
449 i = 0;
450 last_non_zero = -1;
451 goto next_coef;
452 }
453 }
454
455 /* now quantify & encode AC coefs */
456 last_non_zero = i - 1;
457
458 for(;i<=last_index;i++) {
459 j = zigzag_direct[i];
460 level = block[j];
461 next_coef:
462 #if 0
463 if (level != 0)
464 dprintf("level[%d]=%d\n", i, level);
465 #endif
466 /* encode using VLC */
467 if (level != 0) {
468 run = i - last_non_zero - 1;
469 #ifdef ARCH_X86
470 asm volatile(
471 "movl %2, %1 \n\t"
472 "movl %1, %0 \n\t"
473 "addl %1, %1 \n\t"
474 "sbbl %1, %1 \n\t"
475 "xorl %1, %0 \n\t"
476 "subl %1, %0 \n\t"
477 "andl $1, %1 \n\t"
478 : "=&r" (alevel), "=&r" (sign)
479 : "g" (level)
480 );
481 #else
482 sign = 0;
483 alevel = level;
484 if (alevel < 0) {
485 sign = 1;
486 alevel = -alevel;
487 }
488 #endif
489 // code = get_rl_index(rl, 0, run, alevel);
490 if (alevel > mpeg1_max_level[0][run])
491 code= 111; /*rl->n*/
492 else
493 code= mpeg1_index_run[0][run] + alevel - 1;
494
495 if (code < 111 /* rl->n */) {
496 /* store the vlc & sign at once */
497 put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign);
498 } else {
499 /* escape seems to be pretty rare <5% so i dont optimize it */
500 put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]);
501 /* escape: only clip in this case */
502 put_bits(&s->pb, 6, run);
503 if (alevel < 128) {
504 put_bits(&s->pb, 8, level & 0xff);
505 } else {
506 if (level < 0) {
507 put_bits(&s->pb, 16, 0x8001 + level + 255);
508 } else {
509 put_bits(&s->pb, 16, level & 0xffff);
510 }
511 }
512 }
513 last_non_zero = i;
514 }
515 }
516 /* end of block */
517 put_bits(&s->pb, 2, 0x2);
518 }
519
520 /******************************************/
521 /* decoding */
522
523 static VLC dc_lum_vlc;
524 static VLC dc_chroma_vlc;
525 static VLC mv_vlc;
526 static VLC mbincr_vlc;
527 static VLC mb_ptype_vlc;
528 static VLC mb_btype_vlc;
529 static VLC mb_pat_vlc;
530
531 void mpeg1_init_vlc(MpegEncContext *s)
532 {
533 static int done = 0;
534
535 if (!done) {
536
537 init_vlc(&dc_lum_vlc, 9, 12,
538 vlc_dc_lum_bits, 1, 1,
539 vlc_dc_lum_code, 2, 2);
540 init_vlc(&dc_chroma_vlc, 9, 12,
541 vlc_dc_chroma_bits, 1, 1,
542 vlc_dc_chroma_code, 2, 2);
543 init_vlc(&mv_vlc, 9, 17,
544 &mbMotionVectorTable[0][1], 2, 1,
545 &mbMotionVectorTable[0][0], 2, 1);
546 init_vlc(&mbincr_vlc, 9, 35,
547 &mbAddrIncrTable[0][1], 2, 1,
548 &mbAddrIncrTable[0][0], 2, 1);
549 init_vlc(&mb_pat_vlc, 9, 63,
550 &mbPatTable[0][1], 2, 1,
551 &mbPatTable[0][0], 2, 1);
552
553 init_vlc(&mb_ptype_vlc, 6, 32,
554 &table_mb_ptype[0][1], 2, 1,
555 &table_mb_ptype[0][0], 2, 1);
556 init_vlc(&mb_btype_vlc, 6, 32,
557 &table_mb_btype[0][1], 2, 1,
558 &table_mb_btype[0][0], 2, 1);
559 init_rl(&rl_mpeg1);
560 init_rl(&rl_mpeg2);
561 /* cannot use generic init because we must add the EOB code */
562 init_vlc(&rl_mpeg1.vlc, 9, rl_mpeg1.n + 2,
563 &rl_mpeg1.table_vlc[0][1], 4, 2,
564 &rl_mpeg1.table_vlc[0][0], 4, 2);
565 init_vlc(&rl_mpeg2.vlc, 9, rl_mpeg2.n + 2,
566 &rl_mpeg2.table_vlc[0][1], 4, 2,
567 &rl_mpeg2.table_vlc[0][0], 4, 2);
568 }
569 }
570
571 static inline int get_dmv(MpegEncContext *s)
572 {
573 if(get_bits1(&s->gb))
574 return 1 - (get_bits1(&s->gb) << 1);
575 else
576 return 0;
577 }
578
579 static inline int get_qscale(MpegEncContext *s)
580 {
581 int qscale;
582 if (s->mpeg2) {
583 if (s->q_scale_type) {
584 qscale = non_linear_qscale[get_bits(&s->gb, 5)];
585 } else {
586 qscale = get_bits(&s->gb, 5) << 1;
587 }
588 } else {
589 /* for mpeg1, we use the generic unquant code */
590 qscale = get_bits(&s->gb, 5);
591 }
592 return qscale;
593 }
594
595 /* motion type (for mpeg2) */
596 #define MT_FIELD 1
597 #define MT_FRAME 2
598 #define MT_16X8 2
599 #define MT_DMV 3
600
601 static int mpeg_decode_mb(MpegEncContext *s,
602 DCTELEM block[6][64])
603 {
604 int i, j, k, cbp, val, code, mb_type, motion_type;
605
606 /* skip mb handling */
607 if (s->mb_incr == 0) {
608 /* read again increment */
609 s->mb_incr = 1;
610 for(;;) {
611 code = get_vlc(&s->gb, &mbincr_vlc);
612 if (code < 0)
613 return 1; /* error = end of slice */
614 if (code >= 33) {
615 if (code == 33) {
616 s->mb_incr += 33;
617 }
618 /* otherwise, stuffing, nothing to do */
619 } else {
620 s->mb_incr += code;
621 break;
622 }
623 }
624 }
625 if (++s->mb_x >= s->mb_width) {
626 s->mb_x = 0;
627 if (s->mb_y >= (s->mb_height - 1))
628 return -1;
629 s->mb_y++;
630 }
631 dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
632
633 if (--s->mb_incr != 0) {
634 /* skip mb */
635 s->mb_intra = 0;
636 for(i=0;i<6;i++)
637 s->block_last_index[i] = -1;
638 s->mv_type = MV_TYPE_16X16;
639 if (s->pict_type == P_TYPE) {
640 /* if P type, zero motion vector is implied */
641 s->mv_dir = MV_DIR_FORWARD;
642 s->mv[0][0][0] = s->mv[0][0][1] = 0;
643 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
644 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
645 } else {
646 /* if B type, reuse previous vectors and directions */
647 s->mv[0][0][0] = s->last_mv[0][0][0];
648 s->mv[0][0][1] = s->last_mv[0][0][1];
649 s->mv[1][0][0] = s->last_mv[1][0][0];
650 s->mv[1][0][1] = s->last_mv[1][0][1];
651 }
652 s->mb_skiped = 1;
653 return 0;
654 }
655
656 switch(s->pict_type) {
657 default:
658 case I_TYPE:
659 if (get_bits1(&s->gb) == 0) {
660 if (get_bits1(&s->gb) == 0)
661 return -1;
662 mb_type = MB_QUANT | MB_INTRA;
663 } else {
664 mb_type = MB_INTRA;
665 }
666 break;
667 case P_TYPE:
668 mb_type = get_vlc(&s->gb, &mb_ptype_vlc);
669 if (mb_type < 0)
670 return -1;
671 break;
672 case B_TYPE:
673 mb_type = get_vlc(&s->gb, &mb_btype_vlc);
674 if (mb_type < 0)
675 return -1;
676 break;
677 }
678 dprintf("mb_type=%x\n", mb_type);
679 motion_type = 0; /* avoid warning */
680 if (mb_type & (MB_FOR|MB_BACK)) {
681 /* get additionnal motion vector type */
682 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct)
683 motion_type = MT_FRAME;
684 else
685 motion_type = get_bits(&s->gb, 2);
686 }
687 /* compute dct type */
688 if (s->picture_structure == PICT_FRAME &&
689 !s->frame_pred_frame_dct &&
690 (mb_type & (MB_PAT | MB_INTRA))) {
691 s->interlaced_dct = get_bits1(&s->gb);
692 #ifdef DEBUG
693 if (s->interlaced_dct)
694 printf("interlaced_dct\n");
695 #endif
696 } else {
697 s->interlaced_dct = 0; /* frame based */
698 }
699
700 if (mb_type & MB_QUANT) {
701 s->qscale = get_qscale(s);
702 }
703 if (mb_type & MB_INTRA) {
704 if (s->concealment_motion_vectors) {
705 /* just parse them */
706 if (s->picture_structure != PICT_FRAME)
707 skip_bits1(&s->gb); /* field select */
708 mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0);
709 mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0);
710 }
711 s->mb_intra = 1;
712 cbp = 0x3f;
713 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
714 } else {
715 s->mb_intra = 0;
716 cbp = 0;
717 }
718 /* special case of implicit zero motion vector */
719 if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) {
720 s->mv_dir = MV_DIR_FORWARD;
721 s->mv_type = MV_TYPE_16X16;
722 s->last_mv[0][0][0] = 0;
723 s->last_mv[0][0][1] = 0;
724 s->last_mv[0][1][0] = 0;
725 s->last_mv[0][1][1] = 0;
726 s->mv[0][0][0] = 0;
727 s->mv[0][0][1] = 0;
728 } else if (mb_type & (MB_FOR | MB_BACK)) {
729 /* motion vectors */
730 s->mv_dir = 0;
731 for(i=0;i<2;i++) {
732 if (mb_type & (MB_FOR >> i)) {
733 s->mv_dir |= (MV_DIR_FORWARD >> i);
734 dprintf("motion_type=%d\n", motion_type);
735 switch(motion_type) {
736 case MT_FRAME: /* or MT_16X8 */
737 if (s->picture_structure == PICT_FRAME) {
738 /* MT_FRAME */
739 s->mv_type = MV_TYPE_16X16;
740 for(k=0;k<2;k++) {
741 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
742 s->last_mv[i][0][k]);
743 s->last_mv[i][0][k] = val;
744 s->last_mv[i][1][k] = val;
745 /* full_pel: only for mpeg1 */
746 if (s->full_pel[i])
747 val = val << 1;
748 s->mv[i][0][k] = val;
749 dprintf("mv%d: %d\n", k, val);
750 }
751 } else {
752 /* MT_16X8 */
753 s->mv_type = MV_TYPE_16X8;
754 for(j=0;j<2;j++) {
755 s->field_select[i][j] = get_bits1(&s->gb);
756 for(k=0;k<2;k++) {
757 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
758 s->last_mv[i][j][k]);
759 s->last_mv[i][j][k] = val;
760 s->mv[i][j][k] = val;
761 }
762 }
763 }
764 break;
765 case MT_FIELD:
766 if (s->picture_structure == PICT_FRAME) {
767 s->mv_type = MV_TYPE_FIELD;
768 for(j=0;j<2;j++) {
769 s->field_select[i][j] = get_bits1(&s->gb);
770 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
771 s->last_mv[i][j][0]);
772 s->last_mv[i][j][0] = val;
773 s->mv[i][j][0] = val;
774 dprintf("fmx=%d\n", val);
775 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
776 s->last_mv[i][j][1] >> 1);
777 s->last_mv[i][j][1] = val << 1;
778 s->mv[i][j][1] = val;
779 dprintf("fmy=%d\n", val);
780 }
781 } else {
782 s->mv_type = MV_TYPE_16X16;
783 s->field_select[i][0] = get_bits1(&s->gb);
784 for(k=0;k<2;k++) {
785 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
786 s->last_mv[i][0][k]);
787 s->last_mv[i][0][k] = val;
788 s->last_mv[i][1][k] = val;
789 s->mv[i][0][k] = val;
790 }
791 }
792 break;
793 case MT_DMV:
794 {
795 int dmx, dmy, mx, my, m;
796
797 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
798 s->last_mv[i][0][0]);
799 s->last_mv[i][0][0] = mx;
800 s->last_mv[i][1][0] = mx;
801 dmx = get_dmv(s);
802 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
803 s->last_mv[i][0][1] >> 1);
804 dmy = get_dmv(s);
805 s->mv_type = MV_TYPE_DMV;
806 /* XXX: totally broken */
807 if (s->picture_structure == PICT_FRAME) {
808 s->last_mv[i][0][1] = my << 1;
809 s->last_mv[i][1][1] = my << 1;
810
811 m = s->top_field_first ? 1 : 3;
812 /* top -> top pred */
813 s->mv[i][0][0] = mx;
814 s->mv[i][0][1] = my << 1;
815 s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
816 s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
817 m = 4 - m;
818 s->mv[i][2][0] = mx;
819 s->mv[i][2][1] = my << 1;
820 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
821 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
822 } else {
823 s->last_mv[i][0][1] = my;
824 s->last_mv[i][1][1] = my;
825 s->mv[i][0][0] = mx;
826 s->mv[i][0][1] = my;
827 s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
828 s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1
829 /* + 2 * cur_field */;
830 }
831 }
832 break;
833 }
834 }
835 }
836 }
837
838 if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
839 skip_bits1(&s->gb); /* marker */
840 }
841
842 if (mb_type & MB_PAT) {
843 cbp = get_vlc(&s->gb, &mb_pat_vlc);
844 if (cbp < 0)
845 return -1;
846 cbp++;
847 }
848 dprintf("cbp=%x\n", cbp);
849
850 if (s->mpeg2) {
851 if (s->mb_intra) {
852 for(i=0;i<6;i++) {
853 if (cbp & (1 << (5 - i))) {
854 if (mpeg2_decode_block_intra(s, block[i], i) < 0)
855 return -1;
856 } else {
857 s->block_last_index[i] = -1;
858 }
859 }
860 } else {
861 for(i=0;i<6;i++) {
862 if (cbp & (1 << (5 - i))) {
863 if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
864 return -1;
865 } else {
866 s->block_last_index[i] = -1;
867 }
868 }
869 }
870 } else {
871 for(i=0;i<6;i++) {
872 if (cbp & (1 << (5 - i))) {
873 if (mpeg1_decode_block(s, block[i], i) < 0)
874 return -1;
875 } else {
876 s->block_last_index[i] = -1;
877 }
878 }
879 }
880 return 0;
881 }
882
883 /* as h263, but only 17 codes */
884 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
885 {
886 int code, sign, val, m, l, shift;
887
888 code = get_vlc(&s->gb, &mv_vlc);
889 if (code < 0) {
890 return 0xffff;
891 }
892 if (code == 0) {
893 return pred;
894 }
895 sign = get_bits1(&s->gb);
896 shift = fcode - 1;
897 val = (code - 1) << shift;
898 if (shift > 0)
899 val |= get_bits(&s->gb, shift);
900 val++;
901 if (sign)
902 val = -val;
903 val += pred;
904
905 /* modulo decoding */
906 l = (1 << shift) * 16;
907 m = 2 * l;
908 if (val < -l) {
909 val += m;
910 } else if (val >= l) {
911 val -= m;
912 }
913 return val;
914 }
915
916 static inline int decode_dc(MpegEncContext *s, int component)
917 {
918 int code, diff;
919
920 if (component == 0) {
921 code = get_vlc(&s->gb, &dc_lum_vlc);
922 } else {
923 code = get_vlc(&s->gb, &dc_chroma_vlc);
924 }
925 if (code < 0)
926 return 0xffff;
927 if (code == 0) {
928 diff = 0;
929 } else {
930 diff = get_bits(&s->gb, code);
931 if ((diff & (1 << (code - 1))) == 0)
932 diff = (-1 << code) | (diff + 1);
933 }
934 return diff;
935 }
936
937 static int mpeg1_decode_block(MpegEncContext *s,
938 DCTELEM *block,
939 int n)
940 {
941 int level, dc, diff, i, j, run;
942 int code, component;
943 RLTable *rl = &rl_mpeg1;
944
945 if (s->mb_intra) {
946 /* DC coef */
947 component = (n <= 3 ? 0 : n - 4 + 1);
948 diff = decode_dc(s, component);
949 if (diff >= 0xffff)
950 return -1;
951 dc = s->last_dc[component];
952 dc += diff;
953 s->last_dc[component] = dc;
954 block[0] = dc;
955 dprintf("dc=%d diff=%d\n", dc, diff);
956 i = 1;
957 } else {
958 int bit_cnt, v;
959 UINT32 bit_buf;
960 UINT8 *buf_ptr;
961 i = 0;
962 /* special case for the first coef. no need to add a second vlc table */
963 SAVE_BITS(&s->gb);
964 SHOW_BITS(&s->gb, v, 2);
965 if (v & 2) {
966 run = 0;
967 level = 1 - ((v & 1) << 1);
968 FLUSH_BITS(2);
969 RESTORE_BITS(&s->gb);
970 goto add_coef;
971 }
972 RESTORE_BITS(&s->gb);
973 }
974
975 /* now quantify & encode AC coefs */
976 for(;;) {
977 code = get_vlc(&s->gb, &rl->vlc);
978 if (code < 0) {
979 return -1;
980 }
981 if (code == 112) {
982 break;
983 } else if (code == 111) {
984 /* escape */
985 run = get_bits(&s->gb, 6);
986 level = get_bits(&s->gb, 8);
987 level = (level << 24) >> 24;
988 if (level == -128) {
989 level = get_bits(&s->gb, 8) - 256;
990 } else if (level == 0) {
991 level = get_bits(&s->gb, 8);
992 }
993 } else {
994 run = rl->table_run[code];
995 level = rl->table_level[code];
996 if (get_bits1(&s->gb))
997 level = -level;
998 }
999 i += run;
1000 if (i >= 64)
1001 return -1;
1002 add_coef:
1003 dprintf("%d: run=%d level=%d\n", n, run, level);
1004 j = zigzag_direct[i];
1005 block[j] = level;
1006 i++;
1007 }
1008 s->block_last_index[n] = i-1;
1009 return 0;
1010 }
1011
1012 /* Also does unquantization here, since I will never support mpeg2
1013 encoding */
1014 static int mpeg2_decode_block_non_intra(MpegEncContext *s,
1015 DCTELEM *block,
1016 int n)
1017 {
1018 int level, i, j, run;
1019 int code;
1020 RLTable *rl = &rl_mpeg1;
1021 const UINT8 *scan_table;
1022 const UINT16 *matrix;
1023 int mismatch;
1024
1025 if (s->alternate_scan)
1026 scan_table = ff_alternate_vertical_scan;
1027 else
1028 scan_table = zigzag_direct;
1029 mismatch = 1;
1030
1031 {
1032 int bit_cnt, v;
1033 UINT32 bit_buf;
1034 UINT8 *buf_ptr;
1035 i = 0;
1036 if (n < 4)
1037 matrix = s->inter_matrix;
1038 else
1039 matrix = s->chroma_inter_matrix;
1040
1041 /* special case for the first coef. no need to add a second vlc table */
1042 SAVE_BITS(&s->gb);
1043 SHOW_BITS(&s->gb, v, 2);
1044 if (v & 2) {
1045 run = 0;
1046 level = 1 - ((v & 1) << 1);
1047 FLUSH_BITS(2);
1048 RESTORE_BITS(&s->gb);
1049 goto add_coef;
1050 }
1051 RESTORE_BITS(&s->gb);
1052 }
1053
1054 /* now quantify & encode AC coefs */
1055 for(;;) {
1056 code = get_vlc(&s->gb, &rl->vlc);
1057 if (code < 0)
1058 return -1;
1059 if (code == 112) {
1060 break;
1061 } else if (code == 111) {
1062 /* escape */
1063 run = get_bits(&s->gb, 6);
1064 level = get_bits(&s->gb, 12);
1065 level = (level << 20) >> 20;
1066 } else {
1067 run = rl->table_run[code];
1068 level = rl->table_level[code];
1069 if (get_bits1(&s->gb))
1070 level = -level;
1071 }
1072 i += run;
1073 if (i >= 64)
1074 return -1;
1075 add_coef:
1076 j = scan_table[i];
1077 dprintf("%d: run=%d level=%d\n", n, run, level);
1078 /* XXX: optimize */
1079 if (level > 0) {
1080 level = ((level * 2 + 1) * s->qscale * matrix[j]) >> 5;
1081 } else {
1082 level = ((-level * 2 + 1) * s->qscale * matrix[j]) >> 5;
1083 level = -level;
1084 }
1085 /* XXX: is it really necessary to saturate since the encoder
1086 knows whats going on ? */
1087 mismatch ^= level;
1088 block[j] = level;
1089 i++;
1090 }
1091 block[63] ^= (mismatch & 1);
1092 s->block_last_index[n] = i;
1093 return 0;
1094 }
1095
1096 static int mpeg2_decode_block_intra(MpegEncContext *s,
1097 DCTELEM *block,
1098 int n)
1099 {
1100 int level, dc, diff, i, j, run;
1101 int code, component;
1102 RLTable *rl;
1103 const UINT8 *scan_table;
1104 const UINT16 *matrix;
1105 int mismatch;
1106
1107 if (s->alternate_scan)
1108 scan_table = ff_alternate_vertical_scan;
1109 else
1110 scan_table = zigzag_direct;
1111
1112 /* DC coef */
1113 component = (n <= 3 ? 0 : n - 4 + 1);
1114 diff = decode_dc(s, component);
1115 if (diff >= 0xffff)
1116 return -1;
1117 dc = s->last_dc[component];
1118 dc += diff;
1119 s->last_dc[component] = dc;
1120 block[0] = dc << (3 - s->intra_dc_precision);
1121 dprintf("dc=%d\n", block[0]);
1122 mismatch = block[0] ^ 1;
1123 i = 1;
1124 if (s->intra_vlc_format)
1125 rl = &rl_mpeg2;
1126 else
1127 rl = &rl_mpeg1;
1128 if (n < 4)
1129 matrix = s->intra_matrix;
1130 else
1131 matrix = s->chroma_intra_matrix;
1132
1133 /* now quantify & encode AC coefs */
1134 for(;;) {
1135 code = get_vlc(&s->gb, &rl->vlc);
1136 if (code < 0)
1137 return -1;
1138 if (code == 112) {
1139 break;
1140 } else if (code == 111) {
1141 /* escape */
1142 run = get_bits(&s->gb, 6);
1143 level = get_bits(&s->gb, 12);
1144 level = (level << 20) >> 20;
1145 } else {
1146 run = rl->table_run[code];
1147 level = rl->table_level[code];
1148 if (get_bits1(&s->gb))
1149 level = -level;
1150 }
1151 i += run;
1152 if (i >= 64)
1153 return -1;
1154 j = scan_table[i];
1155 dprintf("%d: run=%d level=%d\n", n, run, level);
1156 level = (level * s->qscale * matrix[j]) / 16;
1157 /* XXX: is it really necessary to saturate since the encoder
1158 knows whats going on ? */
1159 mismatch ^= level;
1160 block[j] = level;
1161 i++;
1162 }
1163 block[63] ^= (mismatch & 1);
1164 s->block_last_index[n] = i;
1165 return 0;
1166 }
1167
1168 /* compressed picture size */
1169 #define PICTURE_BUFFER_SIZE 100000
1170
1171 typedef struct Mpeg1Context {
1172 MpegEncContext mpeg_enc_ctx;
1173 UINT32 header_state;
1174 int start_code; /* current start code */
1175 UINT8 buffer[PICTURE_BUFFER_SIZE];
1176 UINT8 *buf_ptr;
1177 int buffer_size;
1178 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1179 int repeat_field; /* true if we must repeat the field */
1180 } Mpeg1Context;
1181
1182 static int mpeg_decode_init(AVCodecContext *avctx)
1183 {
1184 Mpeg1Context *s = avctx->priv_data;
1185
1186 s->header_state = 0xff;
1187 s->mpeg_enc_ctx_allocated = 0;
1188 s->buffer_size = PICTURE_BUFFER_SIZE;
1189 s->start_code = -1;
1190 s->buf_ptr = s->buffer;
1191 s->mpeg_enc_ctx.picture_number = 0;
1192 s->repeat_field = 0;
1193 s->mpeg_enc_ctx.codec_id= avctx->codec->id;
1194 avctx->mbskip_table= s->mpeg_enc_ctx.mbskip_table;
1195 s->mpeg_enc_ctx.flags= avctx->flags;
1196 return 0;
1197 }
1198
1199 /* return the 8 bit start code value and update the search
1200 state. Return -1 if no start code found */
1201 static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end,
1202 UINT32 *header_state)
1203 {
1204 UINT8 *buf_ptr;
1205 unsigned int state, v;
1206 int val;
1207
1208 state = *header_state;
1209 buf_ptr = *pbuf_ptr;
1210 while (buf_ptr < buf_end) {
1211 v = *buf_ptr++;
1212 if (state == 0x000001) {
1213 state = ((state << 8) | v) & 0xffffff;
1214 val = state;
1215 goto found;
1216 }
1217 state = ((state << 8) | v) & 0xffffff;
1218 }
1219 val = -1;
1220 found:
1221 *pbuf_ptr = buf_ptr;
1222 *header_state = state;
1223 return val;
1224 }
1225
1226 static int mpeg1_decode_picture(AVCodecContext *avctx,
1227 UINT8 *buf, int buf_size)
1228 {
1229 Mpeg1Context *s1 = avctx->priv_data;
1230 MpegEncContext *s = &s1->mpeg_enc_ctx;
1231 int ref, f_code;
1232
1233 init_get_bits(&s->gb, buf, buf_size);
1234
1235 ref = get_bits(&s->gb, 10); /* temporal ref */
1236 s->pict_type = get_bits(&s->gb, 3);
1237 dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
1238 skip_bits(&s->gb, 16);
1239 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
1240 s->full_pel[0] = get_bits1(&s->gb);
1241 f_code = get_bits(&s->gb, 3);
1242 if (f_code == 0)
1243 return -1;
1244 s->mpeg_f_code[0][0] = f_code;
1245 s->mpeg_f_code[0][1] = f_code;
1246 }
1247 if (s->pict_type == B_TYPE) {
1248 s->full_pel[1] = get_bits1(&s->gb);
1249 f_code = get_bits(&s->gb, 3);
1250 if (f_code == 0)
1251 return -1;
1252 s->mpeg_f_code[1][0] = f_code;
1253 s->mpeg_f_code[1][1] = f_code;
1254 }
1255 s->y_dc_scale = 8;
1256 s->c_dc_scale = 8;
1257 s->first_slice = 1;
1258 return 0;
1259 }
1260
1261 static void mpeg_decode_sequence_extension(MpegEncContext *s)
1262 {
1263 int horiz_size_ext, vert_size_ext;
1264 int bit_rate_ext, vbv_buf_ext, low_delay;
1265 int frame_rate_ext_n, frame_rate_ext_d;
1266
1267 skip_bits(&s->gb, 8); /* profil and level */
1268 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1269 skip_bits(&s->gb, 2); /* chroma_format */
1270 horiz_size_ext = get_bits(&s->gb, 2);
1271 vert_size_ext = get_bits(&s->gb, 2);
1272 s->width |= (horiz_size_ext << 12);
1273 s->height |= (vert_size_ext << 12);
1274 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1275 s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
1276 skip_bits1(&s->gb); /* marker */
1277 vbv_buf_ext = get_bits(&s->gb, 8);
1278 low_delay = get_bits1(&s->gb);
1279 frame_rate_ext_n = get_bits(&s->gb, 2);
1280 frame_rate_ext_d = get_bits(&s->gb, 5);
1281 if (frame_rate_ext_d >= 1)
1282 s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
1283 dprintf("sequence extension\n");
1284 s->mpeg2 = 1;
1285 s->avctx->sub_id = 2; /* indicates mpeg2 found */
1286 }
1287
1288 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1289 {
1290 int i, v, j;
1291
1292 dprintf("matrix extension\n");
1293
1294 if (get_bits1(&s->gb)) {
1295 for(i=0;i<64;i++) {
1296 v = get_bits(&s->gb, 8);
1297 j = zigzag_direct[i];
1298 s->intra_matrix[j] = v;
1299 s->chroma_intra_matrix[j] = v;
1300 }
1301 }
1302 if (get_bits1(&s->gb)) {
1303 for(i=0;i<64;i++) {
1304 v = get_bits(&s->gb, 8);
1305 j = zigzag_direct[i];
1306 s->inter_matrix[j] = v;
1307 s->chroma_inter_matrix[j] = v;
1308 }
1309 }
1310 if (get_bits1(&s->gb)) {
1311 for(i=0;i<64;i++) {
1312 v = get_bits(&s->gb, 8);
1313 j = zigzag_direct[i];
1314 s->chroma_intra_matrix[j] = v;
1315 }
1316 }
1317 if (get_bits1(&s->gb)) {
1318 for(i=0;i<64;i++) {
1319 v = get_bits(&s->gb, 8);
1320 j = zigzag_direct[i];
1321 s->chroma_inter_matrix[j] = v;
1322 }
1323 }
1324 }
1325
1326 static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
1327 {
1328 s->full_pel[0] = s->full_pel[1] = 0;
1329 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1330 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1331 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1332 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1333 s->intra_dc_precision = get_bits(&s->gb, 2);
1334 s->picture_structure = get_bits(&s->gb, 2);
1335 s->top_field_first = get_bits1(&s->gb);
1336 s->frame_pred_frame_dct = get_bits1(&s->gb);
1337 s->concealment_motion_vectors = get_bits1(&s->gb);
1338 s->q_scale_type = get_bits1(&s->gb);
1339 s->intra_vlc_format = get_bits1(&s->gb);
1340 s->alternate_scan = get_bits1(&s->gb);
1341 s->repeat_first_field = get_bits1(&s->gb);
1342 s->chroma_420_type = get_bits1(&s->gb);
1343 s->progressive_frame = get_bits1(&s->gb);
1344 /* composite display not parsed */
1345 dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
1346 dprintf("picture_structure=%d\n", s->picture_structure);
1347 dprintf("top field first=%d\n", s->top_field_first);
1348 dprintf("repeat first field=%d\n", s->repeat_first_field);
1349 dprintf("conceal=%d\n", s->concealment_motion_vectors);
1350 dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
1351 dprintf("alternate_scan=%d\n", s->alternate_scan);
1352 dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1353 dprintf("progressive_frame=%d\n", s->progressive_frame);
1354 }
1355
1356 static void mpeg_decode_extension(AVCodecContext *avctx,
1357 UINT8 *buf, int buf_size)
1358 {
1359 Mpeg1Context *s1 = avctx->priv_data;
1360 MpegEncContext *s = &s1->mpeg_enc_ctx;
1361 int ext_type;
1362
1363 init_get_bits(&s->gb, buf, buf_size);
1364
1365 ext_type = get_bits(&s->gb, 4);
1366 switch(ext_type) {
1367 case 0x1:
1368 /* sequence ext */
1369 mpeg_decode_sequence_extension(s);
1370 break;
1371 case 0x3:
1372 /* quant matrix extension */
1373 mpeg_decode_quant_matrix_extension(s);
1374 break;
1375 case 0x8:
1376 /* picture extension */
1377 mpeg_decode_picture_coding_extension(s);
1378 break;
1379 }
1380 }
1381
1382 /* return 1 if end of frame */
1383 static int mpeg_decode_slice(AVCodecContext *avctx,
1384 AVPicture *pict,
1385 int start_code,
1386 UINT8 *buf, int buf_size)
1387 {
1388 Mpeg1Context *s1 = avctx->priv_data;
1389 MpegEncContext *s = &s1->mpeg_enc_ctx;
1390 int ret;
1391
1392 start_code = (start_code - 1) & 0xff;
1393 if (start_code >= s->mb_height)
1394 return -1;
1395 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
1396 s->last_dc[1] = s->last_dc[0];
1397 s->last_dc[2] = s->last_dc[0];
1398 memset(s->last_mv, 0, sizeof(s->last_mv));
1399 s->mb_x = -1;
1400 s->mb_y = start_code;
1401 s->mb_incr = 0;
1402 /* start frame decoding */
1403 if (s->first_slice) {
1404 s->first_slice = 0;
1405 MPV_frame_start(s);
1406 }
1407
1408 init_get_bits(&s->gb, buf, buf_size);
1409
1410 s->qscale = get_qscale(s);
1411 /* extra slice info */
1412 while (get_bits1(&s->gb) != 0) {
1413 skip_bits(&s->gb, 8);
1414 }
1415
1416 for(;;) {
1417 clear_blocks(s->block[0]);
1418 emms_c();
1419 ret = mpeg_decode_mb(s, s->block);
1420 dprintf("ret=%d\n", ret);
1421 if (ret < 0)
1422 return -1;
1423 if (ret == 1)
1424 break;
1425 MPV_decode_mb(s, s->block);
1426 }
1427 emms_c();
1428
1429 /* end of slice reached */
1430 if (s->mb_x == (s->mb_width - 1) &&
1431 s->mb_y == (s->mb_height - 1)) {
1432 /* end of image */
1433 UINT8 **picture;
1434
1435 MPV_frame_end(s);
1436
1437 /* XXX: incorrect reported qscale for mpeg2 */
1438 if (s->pict_type == B_TYPE) {
1439 picture = s->current_picture;
1440 avctx->quality = s->qscale;
1441 } else {
1442 /* latency of 1 frame for I and P frames */
1443 /* XXX: use another variable than picture_number */
1444 if (s->picture_number == 0) {
1445 picture = NULL;
1446 } else {
1447 picture = s->last_picture;
1448 avctx->quality = s->last_qscale;
1449 }
1450 s->last_qscale = s->qscale;
1451 s->picture_number++;
1452 }
1453 if (picture) {
1454 pict->data[0] = picture[0];
1455 pict->data[1] = picture[1];
1456 pict->data[2] = picture[2];
1457 pict->linesize[0] = s->linesize;
1458 pict->linesize[1] = s->linesize / 2;
1459 pict->linesize[2] = s->linesize / 2;
1460 return 1;
1461 } else {
1462 return 0;
1463 }
1464 } else {
1465 return 0;
1466 }
1467 }
1468
1469 static int mpeg1_decode_sequence(AVCodecContext *avctx,
1470 UINT8 *buf, int buf_size)
1471 {
1472 Mpeg1Context *s1 = avctx->priv_data;
1473 MpegEncContext *s = &s1->mpeg_enc_ctx;
1474 int width, height, i, v, j;
1475
1476 init_get_bits(&s->gb, buf, buf_size);
1477
1478 width = get_bits(&s->gb, 12);
1479 height = get_bits(&s->gb, 12);
1480 skip_bits(&s->gb, 4);
1481 s->frame_rate_index = get_bits(&s->gb, 4);
1482 if (s->frame_rate_index == 0)
1483 return -1;
1484 s->bit_rate = get_bits(&s->gb, 18) * 400;
1485 if (get_bits1(&s->gb) == 0) /* marker */
1486 return -1;
1487 if (width <= 0 || height <= 0 ||
1488 (width % 2) != 0 || (height % 2) != 0)
1489 return -1;
1490 if (width != s->width ||
1491 height != s->height) {
1492 /* start new mpeg1 context decoding */
1493 s->out_format = FMT_MPEG1;
1494 if (s1->mpeg_enc_ctx_allocated) {
1495 MPV_common_end(s);
1496 }
1497 s->width = width;
1498 s->height = height;
1499 s->has_b_frames = 1;
1500 s->avctx = avctx;
1501 avctx->width = width;
1502 avctx->height = height;
1503 if (s->frame_rate_index >= 9) {
1504 /* at least give a valid frame rate (some old mpeg1 have this) */
1505 avctx->frame_rate = 25 * FRAME_RATE_BASE;
1506 } else {
1507 avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
1508 }
1509 s->frame_rate = avctx->frame_rate;
1510 avctx->bit_rate = s->bit_rate;
1511
1512 if (MPV_common_init(s) < 0)
1513 return -1;
1514 mpeg1_init_vlc(s);
1515 s1->mpeg_enc_ctx_allocated = 1;
1516 }
1517
1518 skip_bits(&s->gb, 10); /* vbv_buffer_size */
1519 skip_bits(&s->gb, 1);
1520
1521 /* get matrix */
1522 if (get_bits1(&s->gb)) {
1523 for(i=0;i<64;i++) {
1524 v = get_bits(&s->gb, 8);
1525 j = zigzag_direct[i];
1526 s->intra_matrix[j] = v;
1527 s->chroma_intra_matrix[j] = v;
1528 }
1529 #ifdef DEBUG
1530 dprintf("intra matrix present\n");
1531 for(i=0;i<64;i++)
1532 dprintf(" %d", s->intra_matrix[zigzag_direct[i]]);
1533 printf("\n");
1534 #endif
1535 } else {
1536 for(i=0;i<64;i++) {
1537 v = default_intra_matrix[i];
1538 s->intra_matrix[i] = v;
1539 s->chroma_intra_matrix[i] = v;
1540 }
1541 }
1542 if (get_bits1(&s->gb)) {
1543 for(i=0;i<64;i++) {
1544 v = get_bits(&s->gb, 8);
1545 j = zigzag_direct[i];
1546 s->inter_matrix[j] = v;
1547 s->chroma_inter_matrix[j] = v;
1548 }
1549 #ifdef DEBUG
1550 dprintf("non intra matrix present\n");
1551 for(i=0;i<64;i++)
1552 dprintf(" %d", s->inter_matrix[zigzag_direct[i]]);
1553 printf("\n");
1554 #endif
1555 } else {
1556 for(i=0;i<64;i++) {
1557 v = default_non_intra_matrix[i];
1558 s->inter_matrix[i] = v;
1559 s->chroma_inter_matrix[i] = v;
1560 }
1561 }
1562
1563 /* we set mpeg2 parameters so that it emulates mpeg1 */
1564 s->progressive_sequence = 1;
1565 s->progressive_frame = 1;
1566 s->picture_structure = PICT_FRAME;
1567 s->frame_pred_frame_dct = 1;
1568 s->mpeg2 = 0;
1569 avctx->sub_id = 1; /* indicates mpeg1 */
1570 return 0;
1571 }
1572
1573 /* handle buffering and image synchronisation */
1574 static int mpeg_decode_frame(AVCodecContext *avctx,
1575 void *data, int *data_size,
1576 UINT8 *buf, int buf_size)
1577 {
1578 Mpeg1Context *s = avctx->priv_data;
1579 UINT8 *buf_end, *buf_ptr, *buf_start;
1580 int len, start_code_found, ret, code, start_code, input_size;
1581 AVPicture *picture = data;
1582 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1583
1584 dprintf("fill_buffer\n");
1585
1586 *data_size = 0;
1587
1588 /* special case for last picture */
1589 if (buf_size == 0) {
1590 if (s2->picture_number > 0) {
1591 picture->data[0] = s2->next_picture[0];
1592 picture->data[1] = s2->next_picture[1];
1593 picture->data[2] = s2->next_picture[2];
1594 picture->linesize[0] = s2->linesize;
1595 picture->linesize[1] = s2->linesize / 2;
1596 picture->linesize[2] = s2->linesize / 2;
1597 *data_size = sizeof(AVPicture);
1598 }
1599 return 0;
1600 }
1601
1602 buf_ptr = buf;
1603 buf_end = buf + buf_size;
1604
1605 #if 0
1606 if (s->repeat_field % 2 == 1) {
1607 s->repeat_field++;
1608 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
1609 // s2->picture_number, s->repeat_field);
1610 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
1611 *data_size = sizeof(AVPicture);
1612 goto the_end;
1613 }
1614 }
1615 #endif
1616 while (buf_ptr < buf_end) {
1617 buf_start = buf_ptr;
1618 /* find start next code */
1619 code = find_start_code(&buf_ptr, buf_end, &s->header_state);
1620 if (code >= 0) {
1621 start_code_found = 1;
1622 } else {
1623 start_code_found = 0;
1624 }
1625 /* copy to buffer */
1626 len = buf_ptr - buf_start;
1627 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
1628 /* data too big : flush */
1629 s->buf_ptr = s->buffer;
1630 if (start_code_found)
1631 s->start_code = code;
1632 } else {
1633 memcpy(s->buf_ptr, buf_start, len);
1634 s->buf_ptr += len;
1635
1636 if (start_code_found) {
1637 /* prepare data for next start code */
1638 input_size = s->buf_ptr - s->buffer;
1639 start_code = s->start_code;
1640 s->buf_ptr = s->buffer;
1641 s->start_code = code;
1642 switch(start_code) {
1643 case SEQ_START_CODE:
1644 mpeg1_decode_sequence(avctx, s->buffer,
1645 input_size);
1646 break;
1647
1648 case PICTURE_START_CODE:
1649 /* we have a complete image : we try to decompress it */
1650 mpeg1_decode_picture(avctx,
1651 s->buffer, input_size);
1652 break;
1653 case EXT_START_CODE:
1654 mpeg_decode_extension(avctx,
1655 s->buffer, input_size);
1656 break;
1657 default:
1658 if (start_code >= SLICE_MIN_START_CODE &&
1659 start_code <= SLICE_MAX_START_CODE) {
1660 ret = mpeg_decode_slice(avctx, picture,
1661 start_code, s->buffer, input_size);
1662 if (ret == 1) {
1663 /* got a picture: exit */
1664 /* first check if we must repeat the frame */
1665 avctx->repeat_pict = 0;
1666 #if 0
1667 if (s2->progressive_frame && s2->repeat_first_field) {
1668 //fprintf(stderr,"\nRepeat this frame: %d! pict: %d",avctx->frame_number,s2->picture_number);
1669 //s2->repeat_first_field = 0;
1670 //s2->progressive_frame = 0;
1671 if (++s->repeat_field > 2)
1672 s->repeat_field = 0;
1673 avctx->repeat_pict = 1;
1674 }
1675 #endif
1676 if (s2->repeat_first_field) {
1677 if (s2->progressive_sequence) {
1678 if (s2->top_field_first)
1679 avctx->repeat_pict = 4;
1680 else
1681 avctx->repeat_pict = 2;
1682 } else if (s2->progressive_frame) {
1683 avctx->repeat_pict = 1;
1684 }
1685 }
1686 *data_size = sizeof(AVPicture);
1687 goto the_end;
1688 }
1689 }
1690 break;
1691 }
1692 }
1693 }
1694 }
1695 the_end:
1696 return buf_ptr - buf;
1697 }
1698
1699 static int mpeg_decode_end(AVCodecContext *avctx)
1700 {
1701 Mpeg1Context *s = avctx->priv_data;
1702
1703 if (s->mpeg_enc_ctx_allocated)
1704 MPV_common_end(&s->mpeg_enc_ctx);
1705 return 0;
1706 }
1707
1708 AVCodec mpeg_decoder = {
1709 "mpegvideo",
1710 CODEC_TYPE_VIDEO,
1711 CODEC_ID_MPEG1VIDEO,
1712 sizeof(Mpeg1Context),
1713 mpeg_decode_init,
1714 NULL,
1715 mpeg_decode_end,
1716 mpeg_decode_frame,
1717 };