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