b frames + slices bugfix
[libav.git] / libavcodec / mpeg12.c
CommitLineData
de6d9b64 1/*
983e3246 2 * MPEG1 codec / MPEG2 decoder
ff4ec49e 3 * Copyright (c) 2000,2001 Fabrice Bellard.
de6d9b64 4 *
ff4ec49e
FB
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.
de6d9b64 9 *
ff4ec49e 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
ff4ec49e
FB
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
de6d9b64 18 */
983e3246
MN
19
20/**
21 * @file mpeg12.c
22 * MPEG1 codec / MPEG2 decoder.
23 */
24
a74127c0 25//#define DEBUG
de6d9b64
FB
26#include "avcodec.h"
27#include "dsputil.h"
28#include "mpegvideo.h"
29
30#include "mpeg12data.h"
31
d87c0267
MN
32#if 1
33#define PRINT_QP(a, b) {}
34#else
35#define PRINT_QP(a, b) printf(a, b)
36#endif
37
de6d9b64
FB
38/* Start codes. */
39#define SEQ_END_CODE 0x000001b7
40#define SEQ_START_CODE 0x000001b3
41#define GOP_START_CODE 0x000001b8
42#define PICTURE_START_CODE 0x00000100
43#define SLICE_MIN_START_CODE 0x00000101
44#define SLICE_MAX_START_CODE 0x000001af
45#define EXT_START_CODE 0x000001b5
46#define USER_START_CODE 0x000001b2
47
4f68b084
MN
48#define DC_VLC_BITS 9
49#define MV_VLC_BITS 9
50#define MBINCR_VLC_BITS 9
51#define MB_PAT_VLC_BITS 9
52#define MB_PTYPE_VLC_BITS 6
53#define MB_BTYPE_VLC_BITS 6
54#define TEX_VLC_BITS 9
55
de6d9b64
FB
56static void mpeg1_encode_block(MpegEncContext *s,
57 DCTELEM *block,
58 int component);
63b15e55 59static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
de6d9b64 60static void mpeg1_skip_picture(MpegEncContext *s, int pict_num);
a0201736
MN
61static inline int mpeg1_decode_block_inter(MpegEncContext *s,
62 DCTELEM *block,
63 int n);
64static inline int mpeg1_decode_block_intra(MpegEncContext *s,
de6d9b64
FB
65 DCTELEM *block,
66 int n);
3729c912 67static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
de6d9b64
FB
68 DCTELEM *block,
69 int n);
3729c912 70static inline int mpeg2_decode_block_intra(MpegEncContext *s,
de6d9b64
FB
71 DCTELEM *block,
72 int n);
73static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
74
c442d75c 75#ifdef CONFIG_ENCODERS
30952237 76static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
0c1a9eda 77static uint8_t fcode_tab[MAX_MV*2+1];
11ce8834 78
c442d75c
MN
79static uint32_t uni_mpeg1_ac_vlc_bits[64*64*2];
80static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
81#endif
82
4f68b084
MN
83static void init_2d_vlc_rl(RLTable *rl)
84{
07787186 85 int i;
4f68b084
MN
86
87 init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2,
88 &rl->table_vlc[0][1], 4, 2,
89 &rl->table_vlc[0][0], 4, 2);
90
91
92 rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
93 for(i=0; i<rl->vlc.table_size; i++){
94 int code= rl->vlc.table[i][0];
95 int len = rl->vlc.table[i][1];
96 int level, run;
97
98 if(len==0){ // illegal code
99 run= 65;
100 level= MAX_LEVEL;
101 }else if(len<0){ //more bits needed
102 run= 0;
103 level= code;
104 }else{
105 if(code==rl->n){ //esc
106 run= 65;
107 level= 0;
108 }else if(code==rl->n+1){ //eob
a0201736
MN
109 run= 0;
110 level= 127;
4f68b084
MN
111 }else{
112 run= rl->table_run [code] + 1;
113 level= rl->table_level[code];
114 }
115 }
116 rl->rl_vlc[0][i].len= len;
117 rl->rl_vlc[0][i].level= level;
118 rl->rl_vlc[0][i].run= run;
119 }
120}
121
c442d75c
MN
122static void init_uni_ac_vlc(RLTable *rl, uint32_t *uni_ac_vlc_bits, uint8_t *uni_ac_vlc_len){
123 int i;
124
125 for(i=0; i<128; i++){
126 int level= i-64;
127 int run;
128 for(run=0; run<64; run++){
129 int len, bits, code;
130
131 int alevel= ABS(level);
132 int sign= (level>>31)&1;
133
134 if (alevel > rl->max_level[0][run])
135 code= 111; /*rl->n*/
136 else
137 code= rl->index_run[0][run] + alevel - 1;
138
139 if (code < 111 /* rl->n */) {
140 /* store the vlc & sign at once */
141 len= mpeg1_vlc[code][1]+1;
142 bits= (mpeg1_vlc[code][0]<<1) + sign;
143 } else {
144 len= mpeg1_vlc[111/*rl->n*/][1]+6;
145 bits= mpeg1_vlc[111/*rl->n*/][0]<<6;
146
147 bits|= run;
148 if (alevel < 128) {
149 bits<<=8; len+=8;
150 bits|= level & 0xff;
151 } else {
152 bits<<=16; len+=16;
153 bits|= level & 0xff;
154 if (level < 0) {
155 bits|= 0x8001 + level + 255;
156 } else {
157 bits|= level & 0xffff;
158 }
159 }
160 }
161
162 uni_ac_vlc_bits[UNI_AC_ENC_INDEX(run, i)]= bits;
163 uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
164 }
165 }
166}
4f68b084 167
de6d9b64
FB
168static void put_header(MpegEncContext *s, int header)
169{
170 align_put_bits(&s->pb);
80feb2a2
MN
171 put_bits(&s->pb, 16, header>>16);
172 put_bits(&s->pb, 16, header&0xFFFF);
de6d9b64
FB
173}
174
175/* put sequence header if needed */
176static void mpeg1_encode_sequence_header(MpegEncContext *s)
177{
178 unsigned int vbv_buffer_size;
d78647e8 179 unsigned int fps, v;
426b8061 180 int n, i;
0c1a9eda 181 uint64_t time_code;
426b8061
MN
182 float best_aspect_error= 1E10;
183 float aspect_ratio= s->avctx->aspect_ratio;
184
185 if(aspect_ratio==0.0) aspect_ratio= s->width / (float)s->height; //pixel aspect 1:1 (VGA)
de6d9b64 186
1e491e29 187 if (s->current_picture.key_frame) {
de6d9b64
FB
188 /* mpeg1 header repeated every gop */
189 put_header(s, SEQ_START_CODE);
190
191 /* search closest frame rate */
192 {
193 int i, dmin, d;
194 s->frame_rate_index = 0;
195 dmin = 0x7fffffff;
14bea432
MN
196 for(i=1;i<14;i++) {
197 if(s->avctx->strict_std_compliance >= 0 && i>=9) break;
198
199 d = abs(MPEG1_FRAME_RATE_BASE*(int64_t)s->avctx->frame_rate/s->avctx->frame_rate_base - frame_rate_tab[i]);
de6d9b64
FB
200 if (d < dmin) {
201 dmin = d;
202 s->frame_rate_index = i;
203 }
204 }
205 }
206
207 put_bits(&s->pb, 12, s->width);
208 put_bits(&s->pb, 12, s->height);
426b8061
MN
209
210 for(i=1; i<15; i++){
211 float error= mpeg1_aspect[i] - s->width/(s->height*aspect_ratio);
212 error= ABS(error);
213
214 if(error < best_aspect_error){
215 best_aspect_error= error;
216 s->aspect_ratio_info= i;
217 }
218 }
219
220 put_bits(&s->pb, 4, s->aspect_ratio_info);
de6d9b64 221 put_bits(&s->pb, 4, s->frame_rate_index);
b536d0aa 222 v = (s->bit_rate + 399) / 400;
de6d9b64
FB
223 if (v > 0x3ffff)
224 v = 0x3ffff;
225 put_bits(&s->pb, 18, v);
226 put_bits(&s->pb, 1, 1); /* marker */
ecfd40b3
HM
227
228 if(s->avctx->rc_buffer_size)
229 vbv_buffer_size = s->avctx->rc_buffer_size;
230 else
231 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
232 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
de6d9b64
FB
233 put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384);
234 put_bits(&s->pb, 1, 1); /* constrained parameter flag */
235 put_bits(&s->pb, 1, 0); /* no custom intra matrix */
236 put_bits(&s->pb, 1, 0); /* no custom non intra matrix */
237
238 put_header(s, GOP_START_CODE);
239 put_bits(&s->pb, 1, 0); /* do drop frame */
240 /* time code : we must convert from the real frame rate to a
241 fake mpeg frame rate in case of low frame rate */
242 fps = frame_rate_tab[s->frame_rate_index];
14bea432 243 time_code = (int64_t)s->fake_picture_number * MPEG1_FRAME_RATE_BASE;
de6d9b64 244 s->gop_picture_number = s->fake_picture_number;
0c1a9eda
ZK
245 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
246 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
de6d9b64 247 put_bits(&s->pb, 1, 1);
0c1a9eda 248 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
14bea432 249 put_bits(&s->pb, 6, (uint32_t)((time_code % fps) / MPEG1_FRAME_RATE_BASE));
de6d9b64
FB
250 put_bits(&s->pb, 1, 1); /* closed gop */
251 put_bits(&s->pb, 1, 0); /* broken link */
252 }
253
14bea432 254 if (s->avctx->frame_rate < (24 * s->avctx->frame_rate_base) && s->picture_number > 0) {
de6d9b64
FB
255 /* insert empty P pictures to slow down to the desired
256 frame rate. Each fake pictures takes about 20 bytes */
257 fps = frame_rate_tab[s->frame_rate_index];
14bea432 258 n = av_rescale((int64_t)s->picture_number * s->avctx->frame_rate_base, fps, s->avctx->frame_rate) / MPEG1_FRAME_RATE_BASE - 1;
de6d9b64
FB
259 while (s->fake_picture_number < n) {
260 mpeg1_skip_picture(s, s->fake_picture_number -
261 s->gop_picture_number);
262 s->fake_picture_number++;
263 }
264
265 }
de6d9b64
FB
266}
267
9b8709d1
MN
268static inline void encode_mb_skip_run(MpegEncContext *s, int run){
269 while (run >= 33) {
270 put_bits(&s->pb, 11, 0x008);
271 run -= 33;
272 }
273 put_bits(&s->pb, mbAddrIncrTable[run][1],
274 mbAddrIncrTable[run][0]);
275}
de6d9b64
FB
276
277/* insert a fake P picture */
278static void mpeg1_skip_picture(MpegEncContext *s, int pict_num)
279{
de6d9b64
FB
280 /* mpeg1 picture header */
281 put_header(s, PICTURE_START_CODE);
282 /* temporal reference */
283 put_bits(&s->pb, 10, pict_num & 0x3ff);
284
285 put_bits(&s->pb, 3, P_TYPE);
286 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
287
288 put_bits(&s->pb, 1, 1); /* integer coordinates */
289 put_bits(&s->pb, 3, 1); /* forward_f_code */
290
291 put_bits(&s->pb, 1, 0); /* extra bit picture */
292
293 /* only one slice */
294 put_header(s, SLICE_MIN_START_CODE);
295 put_bits(&s->pb, 5, 1); /* quantizer scale */
296 put_bits(&s->pb, 1, 0); /* slice extra information */
297
9b8709d1 298 encode_mb_skip_run(s, 0);
de6d9b64
FB
299
300 /* empty macroblock */
301 put_bits(&s->pb, 3, 1); /* motion only */
302
303 /* zero motion x & y */
304 put_bits(&s->pb, 1, 1);
305 put_bits(&s->pb, 1, 1);
306
307 /* output a number of empty slice */
9b8709d1 308 encode_mb_skip_run(s, s->mb_width * s->mb_height - 2);
de6d9b64
FB
309
310 /* empty macroblock */
311 put_bits(&s->pb, 3, 1); /* motion only */
312
313 /* zero motion x & y */
314 put_bits(&s->pb, 1, 1);
315 put_bits(&s->pb, 1, 1);
316}
317
8f8402e4 318static void common_init(MpegEncContext *s)
de6d9b64 319{
8f8402e4
MN
320 s->y_dc_scale_table=
321 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
322}
f004ca1c 323
7604246d 324#ifdef CONFIG_ENCODERS
9b8709d1
MN
325
326void ff_mpeg1_encode_slice_header(MpegEncContext *s){
327 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
328 put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
329 put_bits(&s->pb, 1, 0); /* slice extra information */
330}
331
332void ff_mpeg1_clean_buffers(MpegEncContext *s){
333 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
334 s->last_dc[1] = s->last_dc[0];
335 s->last_dc[2] = s->last_dc[0];
336 memset(s->last_mv, 0, sizeof(s->last_mv));
337}
338
8f8402e4
MN
339void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
340{
de6d9b64
FB
341 mpeg1_encode_sequence_header(s);
342
343 /* mpeg1 picture header */
344 put_header(s, PICTURE_START_CODE);
345 /* temporal reference */
63b15e55
MN
346
347 // RAL: s->picture_number instead of s->fake_picture_number
348 put_bits(&s->pb, 10, (s->picture_number -
de6d9b64 349 s->gop_picture_number) & 0x3ff);
af469427 350 s->fake_picture_number++;
de6d9b64
FB
351
352 put_bits(&s->pb, 3, s->pict_type);
353 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
354
63b15e55
MN
355 // RAL: Forward f_code also needed for B frames
356 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
de6d9b64
FB
357 put_bits(&s->pb, 1, 0); /* half pel coordinates */
358 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
359 }
360
63b15e55
MN
361 // RAL: Backward f_code necessary for B frames
362 if (s->pict_type == B_TYPE) {
363 put_bits(&s->pb, 1, 0); /* half pel coordinates */
364 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
365 }
366
de6d9b64
FB
367 put_bits(&s->pb, 1, 0); /* extra bit picture */
368
9b8709d1
MN
369 s->mb_y=0;
370 ff_mpeg1_encode_slice_header(s);
de6d9b64
FB
371}
372
373void mpeg1_encode_mb(MpegEncContext *s,
374 DCTELEM block[6][64],
375 int motion_x, int motion_y)
376{
9b8709d1
MN
377 int i, cbp;
378 const int mb_x = s->mb_x;
379 const int mb_y = s->mb_y;
380 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
de6d9b64
FB
381
382 /* compute cbp */
383 cbp = 0;
384 for(i=0;i<6;i++) {
385 if (s->block_last_index[i] >= 0)
386 cbp |= 1 << (5 - i);
387 }
388
9b8709d1 389 if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || mb_y != s->mb_height - 1) &&
63b15e55
MN
390 ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) ||
391 (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
392 ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
9b8709d1 393 s->mb_skip_run++;
d87c0267 394 s->qscale -= s->dquant;
49733979
MN
395 s->skip_count++;
396 s->misc_bits++;
397 s->last_bits++;
de6d9b64 398 } else {
9b8709d1
MN
399 if(first_mb){
400 assert(s->mb_skip_run == 0);
401 encode_mb_skip_run(s, s->mb_x);
402 }else{
403 encode_mb_skip_run(s, s->mb_skip_run);
de6d9b64 404 }
de6d9b64
FB
405
406 if (s->pict_type == I_TYPE) {
d87c0267
MN
407 if(s->dquant && cbp){
408 put_bits(&s->pb, 2, 1); /* macroblock_type : macroblock_quant = 1 */
409 put_bits(&s->pb, 5, s->qscale);
410 }else{
411 put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */
412 s->qscale -= s->dquant;
413 }
49733979
MN
414 s->misc_bits+= get_bits_diff(s);
415 s->i_count++;
63b15e55
MN
416 } else if (s->mb_intra) {
417 if(s->dquant && cbp){
418 put_bits(&s->pb, 6, 0x01);
419 put_bits(&s->pb, 5, s->qscale);
420 }else{
421 put_bits(&s->pb, 5, 0x03);
422 s->qscale -= s->dquant;
423 }
424 s->misc_bits+= get_bits_diff(s);
425 s->i_count++;
426 s->last_mv[0][0][0] =
427 s->last_mv[0][0][1] = 0;
428 } else if (s->pict_type == P_TYPE) {
de6d9b64
FB
429 if (cbp != 0) {
430 if (motion_x == 0 && motion_y == 0) {
d87c0267
MN
431 if(s->dquant){
432 put_bits(&s->pb, 5, 1); /* macroblock_pattern & quant */
433 put_bits(&s->pb, 5, s->qscale);
434 }else{
435 put_bits(&s->pb, 2, 1); /* macroblock_pattern only */
436 }
49733979 437 s->misc_bits+= get_bits_diff(s);
de6d9b64
FB
438 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
439 } else {
d87c0267
MN
440 if(s->dquant){
441 put_bits(&s->pb, 5, 2); /* motion + cbp */
442 put_bits(&s->pb, 5, s->qscale);
443 }else{
444 put_bits(&s->pb, 1, 1); /* motion + cbp */
445 }
49733979 446 s->misc_bits+= get_bits_diff(s);
63b15e55
MN
447 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
448 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
49733979 449 s->mv_bits+= get_bits_diff(s);
de6d9b64
FB
450 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
451 }
452 } else {
453 put_bits(&s->pb, 3, 1); /* motion only */
63b15e55
MN
454 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
455 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
d87c0267 456 s->qscale -= s->dquant;
49733979 457 s->mv_bits+= get_bits_diff(s);
de6d9b64 458 }
49733979 459 s->f_count++;
63b15e55
MN
460 } else
461 { // RAL: All the following bloc added for B frames:
462 if (cbp != 0)
463 { // With coded bloc pattern
464 if (s->mv_dir == (MV_DIR_FORWARD | MV_DIR_BACKWARD))
465 { // Bi-directional motion
466 if (s->dquant)
467 { // With QScale
468 put_bits(&s->pb, 5, 2);
469 put_bits(&s->pb, 5, s->qscale);
470 }
471 else // Without QScale
472 put_bits(&s->pb, 2, 3);
473 s->misc_bits += get_bits_diff(s);
474 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
475 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
476 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
477 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
478 s->b_count++;
479 s->f_count++;
480 s->mv_bits += get_bits_diff(s);
481 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
482 }
483 else if (s->mv_dir == MV_DIR_BACKWARD)
484 { // Backward motion
485 if (s->dquant)
486 { // With QScale
487 put_bits(&s->pb, 6, 2);
488 put_bits(&s->pb, 5, s->qscale);
489 }
490 else // Without QScale
491 put_bits(&s->pb, 3, 3);
492 s->misc_bits += get_bits_diff(s);
493 mpeg1_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code);
494 mpeg1_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code);
495 s->b_count++;
496 s->mv_bits += get_bits_diff(s);
497 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
498 }
499 else if (s->mv_dir == MV_DIR_FORWARD)
500 { // Forward motion
501 if (s->dquant)
502 { // With QScale
503 put_bits(&s->pb, 6, 3);
504 put_bits(&s->pb, 5, s->qscale);
505 }
506 else // Without QScale
507 put_bits(&s->pb, 4, 3);
508 s->misc_bits += get_bits_diff(s);
509 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);
510 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);
511 s->f_count++;
512 s->mv_bits += get_bits_diff(s);
513 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
514 }
515 }
516 else
517 { // No coded bloc pattern
518 if (s->mv_dir == (MV_DIR_FORWARD | MV_DIR_BACKWARD))
7604246d 519 { // Bi-directional motion
63b15e55
MN
520 put_bits(&s->pb, 2, 2); /* backward & forward motion */
521 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
522 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
523 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
524 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
525 s->b_count++;
526 s->f_count++;
527 }
528 else if (s->mv_dir == MV_DIR_BACKWARD)
529 { // Backward motion
530 put_bits(&s->pb, 3, 2); /* backward motion only */
531 mpeg1_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code);
532 mpeg1_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code);
533 s->b_count++;
534 }
535 else if (s->mv_dir == MV_DIR_FORWARD)
536 { // Forward motion
537 put_bits(&s->pb, 4, 2); /* forward motion only */
538 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);
539 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);
540 s->f_count++;
541 }
542 s->qscale -= s->dquant;
543 s->mv_bits += get_bits_diff(s);
544 }
545 // End of bloc from RAL
de6d9b64 546 }
de6d9b64
FB
547 for(i=0;i<6;i++) {
548 if (cbp & (1 << (5 - i))) {
549 mpeg1_encode_block(s, block[i], i);
550 }
551 }
9b8709d1 552 s->mb_skip_run = 0;
49733979
MN
553 if(s->mb_intra)
554 s->i_tex_bits+= get_bits_diff(s);
555 else
556 s->p_tex_bits+= get_bits_diff(s);
de6d9b64 557 }
63b15e55
MN
558
559 // RAL: By this:
560 if (s->mv_dir & MV_DIR_FORWARD)
561 {
562 s->last_mv[0][0][0]= s->mv[0][0][0];
563 s->last_mv[0][0][1]= s->mv[0][0][1];
564 }
565 if (s->mv_dir & MV_DIR_BACKWARD)
566 {
567 s->last_mv[1][0][0]= s->mv[1][0][0];
568 s->last_mv[1][0][1]= s->mv[1][0][1];
569 }
de6d9b64
FB
570}
571
63b15e55
MN
572// RAL: Parameter added: f_or_b_code
573static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
de6d9b64
FB
574{
575 int code, bit_size, l, m, bits, range, sign;
576
577 if (val == 0) {
578 /* zero vector */
579 code = 0;
580 put_bits(&s->pb,
581 mbMotionVectorTable[0][1],
582 mbMotionVectorTable[0][0]);
583 } else {
63b15e55 584 bit_size = f_or_b_code - 1;
de6d9b64
FB
585 range = 1 << bit_size;
586 /* modulo encoding */
587 l = 16 * range;
588 m = 2 * l;
589 if (val < -l) {
590 val += m;
591 } else if (val >= l) {
592 val -= m;
593 }
594
595 if (val >= 0) {
596 val--;
597 code = (val >> bit_size) + 1;
598 bits = val & (range - 1);
599 sign = 0;
600 } else {
601 val = -val;
602 val--;
603 code = (val >> bit_size) + 1;
604 bits = val & (range - 1);
605 sign = 1;
606 }
63b15e55
MN
607
608 assert(code > 0 && code <= 16);
609
de6d9b64
FB
610 put_bits(&s->pb,
611 mbMotionVectorTable[code][1],
612 mbMotionVectorTable[code][0]);
63b15e55 613
de6d9b64
FB
614 put_bits(&s->pb, 1, sign);
615 if (bit_size > 0) {
616 put_bits(&s->pb, bit_size, bits);
617 }
618 }
619}
620
8f8402e4 621void ff_mpeg1_encode_init(MpegEncContext *s)
11ce8834
MN
622{
623 static int done=0;
8f8402e4
MN
624
625 common_init(s);
626
11ce8834
MN
627 if(!done){
628 int f_code;
629 int mv;
8f8402e4 630 int i;
11ce8834
MN
631
632 done=1;
8f8402e4 633 init_rl(&rl_mpeg1);
c442d75c 634
8f8402e4
MN
635 for(i=0; i<64; i++)
636 {
637 mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
638 mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
639 }
c442d75c
MN
640
641 init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_bits, uni_mpeg1_ac_vlc_len);
8f8402e4
MN
642
643 /* build unified dc encoding tables */
644 for(i=-255; i<256; i++)
645 {
646 int adiff, index;
647 int bits, code;
648 int diff=i;
649
650 adiff = ABS(diff);
651 if(diff<0) diff--;
652 index = vlc_dc_table[adiff];
653
654 bits= vlc_dc_lum_bits[index] + index;
655 code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
656 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
657
658 bits= vlc_dc_chroma_bits[index] + index;
659 code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
660 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
661 }
662
30952237
MN
663 mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
664
11ce8834
MN
665 for(f_code=1; f_code<=MAX_FCODE; f_code++){
666 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
667 int len;
668
669 if(mv==0) len= mbMotionVectorTable[0][1];
670 else{
671 int val, bit_size, range, code;
672
673 bit_size = s->f_code - 1;
674 range = 1 << bit_size;
675
676 val=mv;
677 if (val < 0)
678 val = -val;
679 val--;
680 code = (val >> bit_size) + 1;
681 if(code<17){
682 len= mbMotionVectorTable[code][1] + 1 + bit_size;
683 }else{
684 len= mbMotionVectorTable[16][1] + 2 + bit_size;
685 }
686 }
687
688 mv_penalty[f_code][mv+MAX_MV]= len;
689 }
690 }
691
692
693 for(f_code=MAX_FCODE; f_code>0; f_code--){
694 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
695 fcode_tab[mv+MAX_MV]= f_code;
696 }
697 }
698 }
1457ab52 699 s->me.mv_penalty= mv_penalty;
11ce8834 700 s->fcode_tab= fcode_tab;
d7e9533a
MN
701 s->min_qcoeff=-255;
702 s->max_qcoeff= 255;
c442d75c
MN
703 s->intra_ac_vlc_length=
704 s->inter_ac_vlc_length= uni_mpeg1_ac_vlc_len;
11ce8834 705}
8f8402e4 706
de6d9b64
FB
707static inline void encode_dc(MpegEncContext *s, int diff, int component)
708{
de6d9b64 709 if (component == 0) {
f004ca1c
MN
710 put_bits(
711 &s->pb,
712 mpeg1_lum_dc_uni[diff+255]&0xFF,
713 mpeg1_lum_dc_uni[diff+255]>>8);
de6d9b64 714 } else {
f004ca1c
MN
715 put_bits(
716 &s->pb,
717 mpeg1_chr_dc_uni[diff+255]&0xFF,
718 mpeg1_chr_dc_uni[diff+255]>>8);
de6d9b64
FB
719 }
720}
721
722static void mpeg1_encode_block(MpegEncContext *s,
723 DCTELEM *block,
724 int n)
725{
726 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
727 int code, component;
80feb2a2 728// RLTable *rl = &rl_mpeg1;
de6d9b64
FB
729
730 last_index = s->block_last_index[n];
731
732 /* DC coef */
733 if (s->mb_intra) {
734 component = (n <= 3 ? 0 : n - 4 + 1);
735 dc = block[0]; /* overflow is impossible */
736 diff = dc - s->last_dc[component];
737 encode_dc(s, diff, component);
738 s->last_dc[component] = dc;
739 i = 1;
740 } else {
741 /* encode the first coefficient : needs to be done here because
742 it is handled slightly differently */
743 level = block[0];
744 if (abs(level) == 1) {
0c1a9eda 745 code = ((uint32_t)level >> 31); /* the sign bit */
de6d9b64
FB
746 put_bits(&s->pb, 2, code | 0x02);
747 i = 1;
748 } else {
749 i = 0;
750 last_non_zero = -1;
751 goto next_coef;
752 }
753 }
754
755 /* now quantify & encode AC coefs */
756 last_non_zero = i - 1;
80feb2a2 757
de6d9b64 758 for(;i<=last_index;i++) {
2ad1516a 759 j = s->intra_scantable.permutated[i];
de6d9b64
FB
760 level = block[j];
761 next_coef:
762#if 0
763 if (level != 0)
764 dprintf("level[%d]=%d\n", i, level);
765#endif
766 /* encode using VLC */
767 if (level != 0) {
768 run = i - last_non_zero - 1;
2ad1516a
MN
769
770 alevel= level;
771 MASK_ABS(sign, alevel)
772 sign&=1;
773
80feb2a2 774// code = get_rl_index(rl, 0, run, alevel);
c442d75c 775 if (alevel <= mpeg1_max_level[0][run]){
80feb2a2 776 code= mpeg1_index_run[0][run] + alevel - 1;
80feb2a2
MN
777 /* store the vlc & sign at once */
778 put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign);
de6d9b64 779 } else {
80feb2a2
MN
780 /* escape seems to be pretty rare <5% so i dont optimize it */
781 put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]);
de6d9b64
FB
782 /* escape: only clip in this case */
783 put_bits(&s->pb, 6, run);
784 if (alevel < 128) {
785 put_bits(&s->pb, 8, level & 0xff);
786 } else {
787 if (level < 0) {
788 put_bits(&s->pb, 16, 0x8001 + level + 255);
789 } else {
790 put_bits(&s->pb, 16, level & 0xffff);
791 }
792 }
793 }
794 last_non_zero = i;
795 }
796 }
797 /* end of block */
798 put_bits(&s->pb, 2, 0x2);
799}
7604246d 800#endif //CONFIG_ENCODERS
de6d9b64
FB
801
802/******************************************/
803/* decoding */
804
805static VLC dc_lum_vlc;
806static VLC dc_chroma_vlc;
807static VLC mv_vlc;
808static VLC mbincr_vlc;
809static VLC mb_ptype_vlc;
810static VLC mb_btype_vlc;
811static VLC mb_pat_vlc;
812
14207b27 813static void init_vlcs(MpegEncContext *s)
de6d9b64
FB
814{
815 static int done = 0;
816
817 if (!done) {
915bbac6 818 done = 1;
de6d9b64 819
9ac7ecd6 820 init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
de6d9b64
FB
821 vlc_dc_lum_bits, 1, 1,
822 vlc_dc_lum_code, 2, 2);
9ac7ecd6 823 init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12,
de6d9b64
FB
824 vlc_dc_chroma_bits, 1, 1,
825 vlc_dc_chroma_code, 2, 2);
8ed2ddb2 826 init_vlc(&mv_vlc, MV_VLC_BITS, 17,
de6d9b64
FB
827 &mbMotionVectorTable[0][1], 2, 1,
828 &mbMotionVectorTable[0][0], 2, 1);
8ed2ddb2 829 init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 35,
de6d9b64
FB
830 &mbAddrIncrTable[0][1], 2, 1,
831 &mbAddrIncrTable[0][0], 2, 1);
8ed2ddb2 832 init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 63,
de6d9b64
FB
833 &mbPatTable[0][1], 2, 1,
834 &mbPatTable[0][0], 2, 1);
835
8ed2ddb2 836 init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 32,
de6d9b64
FB
837 &table_mb_ptype[0][1], 2, 1,
838 &table_mb_ptype[0][0], 2, 1);
8ed2ddb2 839 init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 32,
de6d9b64
FB
840 &table_mb_btype[0][1], 2, 1,
841 &table_mb_btype[0][0], 2, 1);
842 init_rl(&rl_mpeg1);
843 init_rl(&rl_mpeg2);
4f68b084
MN
844
845 init_2d_vlc_rl(&rl_mpeg1);
846 init_2d_vlc_rl(&rl_mpeg2);
de6d9b64
FB
847 }
848}
849
850static inline int get_dmv(MpegEncContext *s)
851{
612476ef
A
852 if(get_bits1(&s->gb))
853 return 1 - (get_bits1(&s->gb) << 1);
de6d9b64
FB
854 else
855 return 0;
856}
857
0ee50938
FB
858static inline int get_qscale(MpegEncContext *s)
859{
860 int qscale;
861 if (s->mpeg2) {
862 if (s->q_scale_type) {
863 qscale = non_linear_qscale[get_bits(&s->gb, 5)];
864 } else {
865 qscale = get_bits(&s->gb, 5) << 1;
866 }
867 } else {
868 /* for mpeg1, we use the generic unquant code */
869 qscale = get_bits(&s->gb, 5);
870 }
871 return qscale;
872}
873
de6d9b64
FB
874/* motion type (for mpeg2) */
875#define MT_FIELD 1
876#define MT_FRAME 2
877#define MT_16X8 2
878#define MT_DMV 3
879
880static int mpeg_decode_mb(MpegEncContext *s,
881 DCTELEM block[6][64])
882{
d2975f8d 883 int i, j, k, cbp, val, mb_type, motion_type;
de6d9b64 884
de6d9b64
FB
885 dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
886
f943e138
MN
887 assert(s->mb_skiped==0);
888
9b8709d1 889 if (s->mb_skip_run-- != 0) {
de6d9b64
FB
890 /* skip mb */
891 s->mb_intra = 0;
892 for(i=0;i<6;i++)
893 s->block_last_index[i] = -1;
894 s->mv_type = MV_TYPE_16X16;
895 if (s->pict_type == P_TYPE) {
896 /* if P type, zero motion vector is implied */
897 s->mv_dir = MV_DIR_FORWARD;
898 s->mv[0][0][0] = s->mv[0][0][1] = 0;
899 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
25ed7f92 900 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
f943e138 901 s->mb_skiped = 1;
de6d9b64
FB
902 } else {
903 /* if B type, reuse previous vectors and directions */
904 s->mv[0][0][0] = s->last_mv[0][0][0];
905 s->mv[0][0][1] = s->last_mv[0][0][1];
906 s->mv[1][0][0] = s->last_mv[1][0][0];
907 s->mv[1][0][1] = s->last_mv[1][0][1];
f943e138
MN
908
909 if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
910 s->mb_skiped = 1;
de6d9b64 911 }
59b571c1 912
de6d9b64
FB
913 return 0;
914 }
915
916 switch(s->pict_type) {
917 default:
918 case I_TYPE:
612476ef
A
919 if (get_bits1(&s->gb) == 0) {
920 if (get_bits1(&s->gb) == 0)
de6d9b64
FB
921 return -1;
922 mb_type = MB_QUANT | MB_INTRA;
923 } else {
924 mb_type = MB_INTRA;
925 }
926 break;
927 case P_TYPE:
8ed2ddb2 928 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
9ac7ecd6
MN
929 if (mb_type < 0){
930 fprintf(stderr, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
de6d9b64 931 return -1;
9ac7ecd6 932 }
de6d9b64
FB
933 break;
934 case B_TYPE:
8ed2ddb2 935 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
9ac7ecd6
MN
936 if (mb_type < 0){
937 fprintf(stderr, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
de6d9b64 938 return -1;
9ac7ecd6 939 }
de6d9b64
FB
940 break;
941 }
942 dprintf("mb_type=%x\n", mb_type);
943 motion_type = 0; /* avoid warning */
944 if (mb_type & (MB_FOR|MB_BACK)) {
945 /* get additionnal motion vector type */
946 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct)
947 motion_type = MT_FRAME;
948 else
949 motion_type = get_bits(&s->gb, 2);
950 }
951 /* compute dct type */
952 if (s->picture_structure == PICT_FRAME &&
953 !s->frame_pred_frame_dct &&
954 (mb_type & (MB_PAT | MB_INTRA))) {
612476ef 955 s->interlaced_dct = get_bits1(&s->gb);
de6d9b64
FB
956#ifdef DEBUG
957 if (s->interlaced_dct)
958 printf("interlaced_dct\n");
959#endif
960 } else {
961 s->interlaced_dct = 0; /* frame based */
962 }
963
964 if (mb_type & MB_QUANT) {
0ee50938 965 s->qscale = get_qscale(s);
de6d9b64
FB
966 }
967 if (mb_type & MB_INTRA) {
968 if (s->concealment_motion_vectors) {
969 /* just parse them */
970 if (s->picture_structure != PICT_FRAME)
612476ef 971 skip_bits1(&s->gb); /* field select */
de6d9b64
FB
972 mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0);
973 mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0);
974 }
975 s->mb_intra = 1;
976 cbp = 0x3f;
977 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
978 } else {
979 s->mb_intra = 0;
980 cbp = 0;
981 }
982 /* special case of implicit zero motion vector */
983 if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) {
984 s->mv_dir = MV_DIR_FORWARD;
985 s->mv_type = MV_TYPE_16X16;
986 s->last_mv[0][0][0] = 0;
987 s->last_mv[0][0][1] = 0;
d753173a
FB
988 s->last_mv[0][1][0] = 0;
989 s->last_mv[0][1][1] = 0;
de6d9b64
FB
990 s->mv[0][0][0] = 0;
991 s->mv[0][0][1] = 0;
992 } else if (mb_type & (MB_FOR | MB_BACK)) {
993 /* motion vectors */
994 s->mv_dir = 0;
995 for(i=0;i<2;i++) {
996 if (mb_type & (MB_FOR >> i)) {
997 s->mv_dir |= (MV_DIR_FORWARD >> i);
d753173a 998 dprintf("motion_type=%d\n", motion_type);
de6d9b64
FB
999 switch(motion_type) {
1000 case MT_FRAME: /* or MT_16X8 */
1001 if (s->picture_structure == PICT_FRAME) {
1002 /* MT_FRAME */
1003 s->mv_type = MV_TYPE_16X16;
1004 for(k=0;k<2;k++) {
1005 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1006 s->last_mv[i][0][k]);
1007 s->last_mv[i][0][k] = val;
1008 s->last_mv[i][1][k] = val;
1009 /* full_pel: only for mpeg1 */
1010 if (s->full_pel[i])
1011 val = val << 1;
1012 s->mv[i][0][k] = val;
1013 dprintf("mv%d: %d\n", k, val);
1014 }
1015 } else {
1016 /* MT_16X8 */
1017 s->mv_type = MV_TYPE_16X8;
1018 for(j=0;j<2;j++) {
612476ef 1019 s->field_select[i][j] = get_bits1(&s->gb);
de6d9b64
FB
1020 for(k=0;k<2;k++) {
1021 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1022 s->last_mv[i][j][k]);
1023 s->last_mv[i][j][k] = val;
1024 s->mv[i][j][k] = val;
1025 }
1026 }
1027 }
1028 break;
1029 case MT_FIELD:
dfb476cb 1030 s->mv_type = MV_TYPE_FIELD;
de6d9b64 1031 if (s->picture_structure == PICT_FRAME) {
de6d9b64 1032 for(j=0;j<2;j++) {
612476ef 1033 s->field_select[i][j] = get_bits1(&s->gb);
de6d9b64
FB
1034 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1035 s->last_mv[i][j][0]);
1036 s->last_mv[i][j][0] = val;
1037 s->mv[i][j][0] = val;
1038 dprintf("fmx=%d\n", val);
1039 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1040 s->last_mv[i][j][1] >> 1);
1041 s->last_mv[i][j][1] = val << 1;
1042 s->mv[i][j][1] = val;
1043 dprintf("fmy=%d\n", val);
1044 }
1045 } else {
612476ef 1046 s->field_select[i][0] = get_bits1(&s->gb);
de6d9b64
FB
1047 for(k=0;k<2;k++) {
1048 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1049 s->last_mv[i][0][k]);
1050 s->last_mv[i][0][k] = val;
1051 s->last_mv[i][1][k] = val;
1052 s->mv[i][0][k] = val;
1053 }
1054 }
1055 break;
1056 case MT_DMV:
1057 {
1058 int dmx, dmy, mx, my, m;
1059
1060 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1061 s->last_mv[i][0][0]);
1062 s->last_mv[i][0][0] = mx;
1063 s->last_mv[i][1][0] = mx;
1064 dmx = get_dmv(s);
1065 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1066 s->last_mv[i][0][1] >> 1);
1067 dmy = get_dmv(s);
1068 s->mv_type = MV_TYPE_DMV;
1069 /* XXX: totally broken */
1070 if (s->picture_structure == PICT_FRAME) {
1071 s->last_mv[i][0][1] = my << 1;
1072 s->last_mv[i][1][1] = my << 1;
1073
1074 m = s->top_field_first ? 1 : 3;
1075 /* top -> top pred */
1076 s->mv[i][0][0] = mx;
1077 s->mv[i][0][1] = my << 1;
1078 s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1079 s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1080 m = 4 - m;
1081 s->mv[i][2][0] = mx;
1082 s->mv[i][2][1] = my << 1;
1083 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1084 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1085 } else {
1086 s->last_mv[i][0][1] = my;
1087 s->last_mv[i][1][1] = my;
1088 s->mv[i][0][0] = mx;
1089 s->mv[i][0][1] = my;
1090 s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
1091 s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1
1092 /* + 2 * cur_field */;
1093 }
1094 }
1095 break;
1096 }
1097 }
1098 }
1099 }
1100
1101 if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
612476ef 1102 skip_bits1(&s->gb); /* marker */
de6d9b64
FB
1103 }
1104
1105 if (mb_type & MB_PAT) {
8ed2ddb2 1106 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
9ac7ecd6
MN
1107 if (cbp < 0){
1108 fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
de6d9b64 1109 return -1;
9ac7ecd6 1110 }
de6d9b64
FB
1111 cbp++;
1112 }
1113 dprintf("cbp=%x\n", cbp);
1114
1115 if (s->mpeg2) {
1116 if (s->mb_intra) {
1117 for(i=0;i<6;i++) {
a0201736
MN
1118 if (mpeg2_decode_block_intra(s, block[i], i) < 0)
1119 return -1;
de6d9b64
FB
1120 }
1121 } else {
1122 for(i=0;i<6;i++) {
a0201736 1123 if (cbp & 32) {
de6d9b64
FB
1124 if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
1125 return -1;
5bd12d12
FB
1126 } else {
1127 s->block_last_index[i] = -1;
de6d9b64 1128 }
a0201736 1129 cbp+=cbp;
de6d9b64
FB
1130 }
1131 }
1132 } else {
a0201736
MN
1133 if (s->mb_intra) {
1134 for(i=0;i<6;i++) {
1135 if (mpeg1_decode_block_intra(s, block[i], i) < 0)
de6d9b64 1136 return -1;
a0201736
MN
1137 }
1138 }else{
1139 for(i=0;i<6;i++) {
1140 if (cbp & 32) {
1141 if (mpeg1_decode_block_inter(s, block[i], i) < 0)
1142 return -1;
1143 } else {
1144 s->block_last_index[i] = -1;
1145 }
1146 cbp+=cbp;
de6d9b64
FB
1147 }
1148 }
1149 }
1150 return 0;
1151}
1152
1153/* as h263, but only 17 codes */
1154static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
1155{
1156 int code, sign, val, m, l, shift;
1157
8ed2ddb2 1158 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
de6d9b64
FB
1159 if (code < 0) {
1160 return 0xffff;
1161 }
1162 if (code == 0) {
1163 return pred;
1164 }
612476ef 1165 sign = get_bits1(&s->gb);
de6d9b64
FB
1166 shift = fcode - 1;
1167 val = (code - 1) << shift;
1168 if (shift > 0)
1169 val |= get_bits(&s->gb, shift);
1170 val++;
1171 if (sign)
1172 val = -val;
1173 val += pred;
1174
1175 /* modulo decoding */
1176 l = (1 << shift) * 16;
1177 m = 2 * l;
1178 if (val < -l) {
1179 val += m;
1180 } else if (val >= l) {
1181 val -= m;
1182 }
1183 return val;
1184}
1185
1186static inline int decode_dc(MpegEncContext *s, int component)
1187{
1188 int code, diff;
1189
1190 if (component == 0) {
9ac7ecd6 1191 code = get_vlc2(&s->gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
de6d9b64 1192 } else {
9ac7ecd6 1193 code = get_vlc2(&s->gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
de6d9b64 1194 }
9ac7ecd6
MN
1195 if (code < 0){
1196 fprintf(stderr, "invalid dc code at %d %d\n", s->mb_x, s->mb_y);
de6d9b64 1197 return 0xffff;
9ac7ecd6 1198 }
de6d9b64
FB
1199 if (code == 0) {
1200 diff = 0;
1201 } else {
1202 diff = get_bits(&s->gb, code);
1203 if ((diff & (1 << (code - 1))) == 0)
1204 diff = (-1 << code) | (diff + 1);
1205 }
1206 return diff;
1207}
1208
a0201736 1209static inline int mpeg1_decode_block_intra(MpegEncContext *s,
de6d9b64
FB
1210 DCTELEM *block,
1211 int n)
1212{
1213 int level, dc, diff, i, j, run;
a0201736 1214 int component;
de6d9b64 1215 RLTable *rl = &rl_mpeg1;
0c1a9eda
ZK
1216 uint8_t * const scantable= s->intra_scantable.permutated;
1217 const uint16_t *quant_matrix= s->intra_matrix;
a0201736 1218 const int qscale= s->qscale;
de6d9b64 1219
a0201736
MN
1220 /* DC coef */
1221 component = (n <= 3 ? 0 : n - 4 + 1);
1222 diff = decode_dc(s, component);
1223 if (diff >= 0xffff)
1224 return -1;
1225 dc = s->last_dc[component];
1226 dc += diff;
1227 s->last_dc[component] = dc;
1228 block[0] = dc<<3;
1229 dprintf("dc=%d diff=%d\n", dc, diff);
1230 i = 0;
1231 {
1232 OPEN_READER(re, &s->gb);
1233 /* now quantify & encode AC coefs */
1234 for(;;) {
1235 UPDATE_CACHE(re, &s->gb);
1236 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1237
1238 if(level == 127){
1239 break;
1240 } else if(level != 0) {
1241 i += run;
1242 j = scantable[i];
1243 level= (level*qscale*quant_matrix[j])>>3;
1244 level= (level-1)|1;
1245 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1246 LAST_SKIP_BITS(re, &s->gb, 1);
1247 } else {
1248 /* escape */
1249 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1250 UPDATE_CACHE(re, &s->gb);
1251 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1252 if (level == -128) {
1253 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1254 } else if (level == 0) {
1255 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
1256 }
1257 i += run;
1258 j = scantable[i];
1259 if(level<0){
1260 level= -level;
1261 level= (level*qscale*quant_matrix[j])>>3;
1262 level= (level-1)|1;
1263 level= -level;
1264 }else{
1265 level= (level*qscale*quant_matrix[j])>>3;
1266 level= (level-1)|1;
1267 }
1268 }
1269 if (i > 63){
1270 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1271 return -1;
1272 }
1273
1274 block[j] = level;
1275 }
1276 CLOSE_READER(re, &s->gb);
1277 }
1278 s->block_last_index[n] = i;
1279 return 0;
1280}
1281
1282static inline int mpeg1_decode_block_inter(MpegEncContext *s,
1283 DCTELEM *block,
1284 int n)
1285{
1286 int level, i, j, run;
1287 RLTable *rl = &rl_mpeg1;
0c1a9eda
ZK
1288 uint8_t * const scantable= s->intra_scantable.permutated;
1289 const uint16_t *quant_matrix= s->inter_matrix;
a0201736
MN
1290 const int qscale= s->qscale;
1291
1292 {
8db1a1dd
MN
1293 int v;
1294 OPEN_READER(re, &s->gb);
a0201736 1295 i = -1;
de6d9b64 1296 /* special case for the first coef. no need to add a second vlc table */
8db1a1dd
MN
1297 UPDATE_CACHE(re, &s->gb);
1298 v= SHOW_UBITS(re, &s->gb, 2);
de6d9b64 1299 if (v & 2) {
3729c912 1300 LAST_SKIP_BITS(re, &s->gb, 2);
a0201736
MN
1301 level= (3*qscale*quant_matrix[0])>>4;
1302 level= (level-1)|1;
1303 if(v&1)
1304 level= -level;
3729c912 1305 block[0] = level;
a0201736 1306 i++;
de6d9b64 1307 }
3729c912 1308
a0201736
MN
1309 /* now quantify & encode AC coefs */
1310 for(;;) {
1311 UPDATE_CACHE(re, &s->gb);
1312 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1313
1314 if(level == 127){
1315 break;
1316 } else if(level != 0) {
1317 i += run;
1318 j = scantable[i];
1319 level= ((level*2+1)*qscale*quant_matrix[j])>>4;
1320 level= (level-1)|1;
1321 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1322 LAST_SKIP_BITS(re, &s->gb, 1);
1323 } else {
1324 /* escape */
1325 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1326 UPDATE_CACHE(re, &s->gb);
1327 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1328 if (level == -128) {
1329 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1330 } else if (level == 0) {
1331 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
1332 }
1333 i += run;
1334 j = scantable[i];
1335 if(level<0){
1336 level= -level;
1337 level= ((level*2+1)*qscale*quant_matrix[j])>>4;
1338 level= (level-1)|1;
1339 level= -level;
1340 }else{
1341 level= ((level*2+1)*qscale*quant_matrix[j])>>4;
1342 level= (level-1)|1;
1343 }
de6d9b64 1344 }
a0201736
MN
1345 if (i > 63){
1346 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1347 return -1;
1348 }
1349
1350 block[j] = level;
de6d9b64 1351 }
a0201736 1352 CLOSE_READER(re, &s->gb);
de6d9b64 1353 }
a0201736 1354 s->block_last_index[n] = i;
de6d9b64
FB
1355 return 0;
1356}
1357
1358/* Also does unquantization here, since I will never support mpeg2
1359 encoding */
3729c912
MN
1360static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
1361 DCTELEM *block,
1362 int n)
de6d9b64
FB
1363{
1364 int level, i, j, run;
de6d9b64 1365 RLTable *rl = &rl_mpeg1;
0c1a9eda
ZK
1366 uint8_t * const scantable= s->intra_scantable.permutated;
1367 const uint16_t *quant_matrix;
3729c912 1368 const int qscale= s->qscale;
de6d9b64
FB
1369 int mismatch;
1370
de6d9b64
FB
1371 mismatch = 1;
1372
1373 {
8db1a1dd
MN
1374 int v;
1375 OPEN_READER(re, &s->gb);
3729c912 1376 i = -1;
8db1a1dd 1377 if (n < 4)
3729c912 1378 quant_matrix = s->inter_matrix;
de6d9b64 1379 else
3729c912 1380 quant_matrix = s->chroma_inter_matrix;
8db1a1dd 1381
de6d9b64 1382 /* special case for the first coef. no need to add a second vlc table */
8db1a1dd
MN
1383 UPDATE_CACHE(re, &s->gb);
1384 v= SHOW_UBITS(re, &s->gb, 2);
de6d9b64 1385 if (v & 2) {
3729c912
MN
1386 LAST_SKIP_BITS(re, &s->gb, 2);
1387 level= (3*qscale*quant_matrix[0])>>5;
1388 if(v&1)
1389 level= -level;
1390 block[0] = level;
1391 mismatch ^= level;
1392 i++;
de6d9b64 1393 }
de6d9b64 1394
3729c912
MN
1395 /* now quantify & encode AC coefs */
1396 for(;;) {
1397 UPDATE_CACHE(re, &s->gb);
1398 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1399
1400 if(level == 127){
1401 break;
1402 } else if(level != 0) {
1403 i += run;
1404 j = scantable[i];
1405 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1406 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1407 LAST_SKIP_BITS(re, &s->gb, 1);
1408 } else {
1409 /* escape */
1410 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1411 UPDATE_CACHE(re, &s->gb);
1412 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1413
1414 i += run;
1415 j = scantable[i];
1416 if(level<0){
1417 level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
1418 level= -level;
1419 }else{
1420 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1421 }
1422 }
1423 if (i > 63){
1424 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1425 return -1;
1426 }
1427
1428 mismatch ^= level;
1429 block[j] = level;
25ed7f92 1430 }
3729c912 1431 CLOSE_READER(re, &s->gb);
de6d9b64
FB
1432 }
1433 block[63] ^= (mismatch & 1);
3729c912 1434
de6d9b64
FB
1435 s->block_last_index[n] = i;
1436 return 0;
1437}
1438
3729c912
MN
1439static inline int mpeg2_decode_block_intra(MpegEncContext *s,
1440 DCTELEM *block,
1441 int n)
de6d9b64
FB
1442{
1443 int level, dc, diff, i, j, run;
3729c912 1444 int component;
de6d9b64 1445 RLTable *rl;
0c1a9eda
ZK
1446 uint8_t * const scantable= s->intra_scantable.permutated;
1447 const uint16_t *quant_matrix;
3729c912 1448 const int qscale= s->qscale;
de6d9b64
FB
1449 int mismatch;
1450
de6d9b64 1451 /* DC coef */
3729c912
MN
1452 if (n < 4){
1453 quant_matrix = s->intra_matrix;
1454 component = 0;
1455 }else{
1456 quant_matrix = s->chroma_intra_matrix;
1457 component = n - 3;
1458 }
de6d9b64
FB
1459 diff = decode_dc(s, component);
1460 if (diff >= 0xffff)
1461 return -1;
1462 dc = s->last_dc[component];
1463 dc += diff;
1464 s->last_dc[component] = dc;
1465 block[0] = dc << (3 - s->intra_dc_precision);
1466 dprintf("dc=%d\n", block[0]);
d753173a 1467 mismatch = block[0] ^ 1;
3729c912 1468 i = 0;
de6d9b64
FB
1469 if (s->intra_vlc_format)
1470 rl = &rl_mpeg2;
1471 else
1472 rl = &rl_mpeg1;
25ed7f92 1473
3729c912
MN
1474 {
1475 OPEN_READER(re, &s->gb);
1476 /* now quantify & encode AC coefs */
1477 for(;;) {
1478 UPDATE_CACHE(re, &s->gb);
1479 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1480
1481 if(level == 127){
1482 break;
1483 } else if(level != 0) {
1484 i += run;
1485 j = scantable[i];
1486 level= (level*qscale*quant_matrix[j])>>4;
1487 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1488 LAST_SKIP_BITS(re, &s->gb, 1);
1489 } else {
1490 /* escape */
1491 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1492 UPDATE_CACHE(re, &s->gb);
1493 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1494 i += run;
1495 j = scantable[i];
1496 if(level<0){
1497 level= (-level*qscale*quant_matrix[j])>>4;
1498 level= -level;
1499 }else{
1500 level= (level*qscale*quant_matrix[j])>>4;
1501 }
1502 }
1503 if (i > 63){
1504 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1505 return -1;
1506 }
1507
1508 mismatch^= level;
1509 block[j] = level;
9ac7ecd6 1510 }
3729c912 1511 CLOSE_READER(re, &s->gb);
de6d9b64 1512 }
3729c912
MN
1513 block[63]^= mismatch&1;
1514
de6d9b64
FB
1515 s->block_last_index[n] = i;
1516 return 0;
1517}
1518
1519/* compressed picture size */
1520#define PICTURE_BUFFER_SIZE 100000
1521
1522typedef struct Mpeg1Context {
1523 MpegEncContext mpeg_enc_ctx;
0c1a9eda 1524 uint32_t header_state;
de6d9b64 1525 int start_code; /* current start code */
0c1a9eda
ZK
1526 uint8_t buffer[PICTURE_BUFFER_SIZE];
1527 uint8_t *buf_ptr;
de6d9b64
FB
1528 int buffer_size;
1529 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1cb0edb4 1530 int repeat_field; /* true if we must repeat the field */
de6d9b64
FB
1531} Mpeg1Context;
1532
1533static int mpeg_decode_init(AVCodecContext *avctx)
1534{
1535 Mpeg1Context *s = avctx->priv_data;
8f8402e4 1536
94aec31f 1537 s->mpeg_enc_ctx.flags= avctx->flags;
8f8402e4 1538 common_init(&s->mpeg_enc_ctx);
14207b27 1539 init_vlcs(&s->mpeg_enc_ctx);
de6d9b64
FB
1540
1541 s->header_state = 0xff;
1542 s->mpeg_enc_ctx_allocated = 0;
1543 s->buffer_size = PICTURE_BUFFER_SIZE;
1544 s->start_code = -1;
1545 s->buf_ptr = s->buffer;
1546 s->mpeg_enc_ctx.picture_number = 0;
1cb0edb4 1547 s->repeat_field = 0;
d7e9533a 1548 s->mpeg_enc_ctx.codec_id= avctx->codec->id;
de6d9b64
FB
1549 return 0;
1550}
1551
1552/* return the 8 bit start code value and update the search
1553 state. Return -1 if no start code found */
0c1a9eda
ZK
1554static int find_start_code(uint8_t **pbuf_ptr, uint8_t *buf_end,
1555 uint32_t *header_state)
de6d9b64 1556{
0c1a9eda 1557 uint8_t *buf_ptr;
de6d9b64
FB
1558 unsigned int state, v;
1559 int val;
1560
1561 state = *header_state;
1562 buf_ptr = *pbuf_ptr;
1563 while (buf_ptr < buf_end) {
1564 v = *buf_ptr++;
1565 if (state == 0x000001) {
1566 state = ((state << 8) | v) & 0xffffff;
1567 val = state;
1568 goto found;
1569 }
1570 state = ((state << 8) | v) & 0xffffff;
1571 }
1572 val = -1;
1573 found:
1574 *pbuf_ptr = buf_ptr;
1575 *header_state = state;
1576 return val;
1577}
1578
1579static int mpeg1_decode_picture(AVCodecContext *avctx,
0c1a9eda 1580 uint8_t *buf, int buf_size)
de6d9b64
FB
1581{
1582 Mpeg1Context *s1 = avctx->priv_data;
1583 MpegEncContext *s = &s1->mpeg_enc_ctx;
1584 int ref, f_code;
1585
68f593b4 1586 init_get_bits(&s->gb, buf, buf_size*8);
de6d9b64
FB
1587
1588 ref = get_bits(&s->gb, 10); /* temporal ref */
1589 s->pict_type = get_bits(&s->gb, 3);
b9ecd1ee 1590 dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
58bfafbe 1591
612476ef 1592 skip_bits(&s->gb, 16);
de6d9b64 1593 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
612476ef 1594 s->full_pel[0] = get_bits1(&s->gb);
de6d9b64
FB
1595 f_code = get_bits(&s->gb, 3);
1596 if (f_code == 0)
1597 return -1;
1598 s->mpeg_f_code[0][0] = f_code;
1599 s->mpeg_f_code[0][1] = f_code;
1600 }
1601 if (s->pict_type == B_TYPE) {
612476ef 1602 s->full_pel[1] = get_bits1(&s->gb);
de6d9b64
FB
1603 f_code = get_bits(&s->gb, 3);
1604 if (f_code == 0)
1605 return -1;
1606 s->mpeg_f_code[1][0] = f_code;
1607 s->mpeg_f_code[1][1] = f_code;
1608 }
1e491e29
MN
1609 s->current_picture.pict_type= s->pict_type;
1610 s->current_picture.key_frame= s->pict_type == I_TYPE;
d9cb5429 1611
de6d9b64
FB
1612 s->y_dc_scale = 8;
1613 s->c_dc_scale = 8;
1614 s->first_slice = 1;
1615 return 0;
1616}
1617
1618static void mpeg_decode_sequence_extension(MpegEncContext *s)
1619{
1620 int horiz_size_ext, vert_size_ext;
945f15b7 1621 int bit_rate_ext, vbv_buf_ext;
de6d9b64 1622 int frame_rate_ext_n, frame_rate_ext_d;
945f15b7 1623 float aspect;
de6d9b64 1624
612476ef 1625 skip_bits(&s->gb, 8); /* profil and level */
1cb0edb4 1626 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
612476ef 1627 skip_bits(&s->gb, 2); /* chroma_format */
de6d9b64
FB
1628 horiz_size_ext = get_bits(&s->gb, 2);
1629 vert_size_ext = get_bits(&s->gb, 2);
1630 s->width |= (horiz_size_ext << 12);
1631 s->height |= (vert_size_ext << 12);
1632 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1633 s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
612476ef 1634 skip_bits1(&s->gb); /* marker */
de6d9b64 1635 vbv_buf_ext = get_bits(&s->gb, 8);
945f15b7 1636 s->low_delay = get_bits1(&s->gb);
de6d9b64
FB
1637 frame_rate_ext_n = get_bits(&s->gb, 2);
1638 frame_rate_ext_d = get_bits(&s->gb, 5);
14bea432
MN
1639 av_reduce(
1640 &s->avctx->frame_rate,
1641 &s->avctx->frame_rate_base,
1642 frame_rate_tab[s->frame_rate_index] * (frame_rate_ext_n+1),
1643 MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d+1),
1644 1<<30);
1645
de6d9b64
FB
1646 dprintf("sequence extension\n");
1647 s->mpeg2 = 1;
fb4a4a56 1648 s->avctx->sub_id = 2; /* indicates mpeg2 found */
945f15b7
MN
1649
1650 aspect= mpeg2_aspect[s->aspect_ratio_info];
1651 if(aspect>0.0) s->avctx->aspect_ratio= s->width/(aspect*s->height);
1652 else if(aspect<0.0) s->avctx->aspect_ratio= -1.0/aspect;
de6d9b64
FB
1653}
1654
1655static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1656{
60832448 1657 int i, v, j;
de6d9b64 1658
25ed7f92
FB
1659 dprintf("matrix extension\n");
1660
612476ef 1661 if (get_bits1(&s->gb)) {
de6d9b64
FB
1662 for(i=0;i<64;i++) {
1663 v = get_bits(&s->gb, 8);
b0368839 1664 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
60832448
FB
1665 s->intra_matrix[j] = v;
1666 s->chroma_intra_matrix[j] = v;
de6d9b64
FB
1667 }
1668 }
612476ef 1669 if (get_bits1(&s->gb)) {
de6d9b64
FB
1670 for(i=0;i<64;i++) {
1671 v = get_bits(&s->gb, 8);
b0368839 1672 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
d7e9533a
MN
1673 s->inter_matrix[j] = v;
1674 s->chroma_inter_matrix[j] = v;
de6d9b64
FB
1675 }
1676 }
612476ef 1677 if (get_bits1(&s->gb)) {
de6d9b64
FB
1678 for(i=0;i<64;i++) {
1679 v = get_bits(&s->gb, 8);
b0368839 1680 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
60832448 1681 s->chroma_intra_matrix[j] = v;
de6d9b64
FB
1682 }
1683 }
612476ef 1684 if (get_bits1(&s->gb)) {
de6d9b64
FB
1685 for(i=0;i<64;i++) {
1686 v = get_bits(&s->gb, 8);
b0368839 1687 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
d7e9533a 1688 s->chroma_inter_matrix[j] = v;
de6d9b64
FB
1689 }
1690 }
1691}
1692
1693static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
1694{
1695 s->full_pel[0] = s->full_pel[1] = 0;
1696 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1697 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1698 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1699 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1700 s->intra_dc_precision = get_bits(&s->gb, 2);
1701 s->picture_structure = get_bits(&s->gb, 2);
612476ef
A
1702 s->top_field_first = get_bits1(&s->gb);
1703 s->frame_pred_frame_dct = get_bits1(&s->gb);
1704 s->concealment_motion_vectors = get_bits1(&s->gb);
1705 s->q_scale_type = get_bits1(&s->gb);
1706 s->intra_vlc_format = get_bits1(&s->gb);
1707 s->alternate_scan = get_bits1(&s->gb);
1708 s->repeat_first_field = get_bits1(&s->gb);
1709 s->chroma_420_type = get_bits1(&s->gb);
1710 s->progressive_frame = get_bits1(&s->gb);
acf44abb 1711
dfb476cb
MN
1712 if(s->picture_structure == PICT_FRAME)
1713 s->first_field=0;
1714 else{
1715 s->first_field ^= 1;
1716 memset(s->mbskip_table, 0, s->mb_width*s->mb_height);
1717 }
1718
acf44abb
MN
1719 if(s->alternate_scan){
1720 ff_init_scantable(s, &s->inter_scantable , ff_alternate_vertical_scan);
1721 ff_init_scantable(s, &s->intra_scantable , ff_alternate_vertical_scan);
1722 ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_vertical_scan);
1723 ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
1724 }else{
1725 ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct);
1726 ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct);
1727 ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
1728 ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
1729 }
1730
de6d9b64 1731 /* composite display not parsed */
1cb0edb4 1732 dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
25ed7f92 1733 dprintf("picture_structure=%d\n", s->picture_structure);
e0a3d744
J
1734 dprintf("top field first=%d\n", s->top_field_first);
1735 dprintf("repeat first field=%d\n", s->repeat_first_field);
de6d9b64 1736 dprintf("conceal=%d\n", s->concealment_motion_vectors);
25ed7f92
FB
1737 dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
1738 dprintf("alternate_scan=%d\n", s->alternate_scan);
de6d9b64 1739 dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1cb0edb4 1740 dprintf("progressive_frame=%d\n", s->progressive_frame);
de6d9b64
FB
1741}
1742
1743static void mpeg_decode_extension(AVCodecContext *avctx,
0c1a9eda 1744 uint8_t *buf, int buf_size)
de6d9b64
FB
1745{
1746 Mpeg1Context *s1 = avctx->priv_data;
1747 MpegEncContext *s = &s1->mpeg_enc_ctx;
1748 int ext_type;
1749
68f593b4 1750 init_get_bits(&s->gb, buf, buf_size*8);
de6d9b64
FB
1751
1752 ext_type = get_bits(&s->gb, 4);
1753 switch(ext_type) {
1754 case 0x1:
1755 /* sequence ext */
1756 mpeg_decode_sequence_extension(s);
1757 break;
1758 case 0x3:
1759 /* quant matrix extension */
1760 mpeg_decode_quant_matrix_extension(s);
1761 break;
1762 case 0x8:
1763 /* picture extension */
1764 mpeg_decode_picture_coding_extension(s);
1765 break;
1766 }
1767}
1768
86f85dca
MN
1769#define DECODE_SLICE_FATAL_ERROR -2
1770#define DECODE_SLICE_ERROR -1
1771#define DECODE_SLICE_OK 0
1772#define DECODE_SLICE_EOP 1
1773
1774/**
1775 * decodes a slice.
1776 * @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br>
1777 * DECODE_SLICE_ERROR if the slice is damaged<br>
1778 * DECODE_SLICE_OK if this slice is ok<br>
1779 * DECODE_SLICE_EOP if the end of the picture is reached
1780 */
de6d9b64 1781static int mpeg_decode_slice(AVCodecContext *avctx,
492cd3a9 1782 AVFrame *pict,
de6d9b64 1783 int start_code,
0c1a9eda 1784 uint8_t *buf, int buf_size)
de6d9b64
FB
1785{
1786 Mpeg1Context *s1 = avctx->priv_data;
1787 MpegEncContext *s = &s1->mpeg_enc_ctx;
1788 int ret;
dfb476cb 1789 const int field_pic= s->picture_structure != PICT_FRAME;
de6d9b64
FB
1790
1791 start_code = (start_code - 1) & 0xff;
9ac7ecd6 1792 if (start_code >= s->mb_height){
14207b27 1793 fprintf(stderr, "slice below image (%d >= %d)\n", start_code, s->mb_height);
86f85dca 1794 return DECODE_SLICE_ERROR;
9ac7ecd6 1795 }
9b8709d1
MN
1796
1797 ff_mpeg1_clean_buffers(s);
dfb476cb 1798
de6d9b64 1799 /* start frame decoding */
b536d0aa
MN
1800 if (s->first_slice) {
1801 if(s->first_field || s->picture_structure==PICT_FRAME){
d6db1c9c 1802 if(MPV_frame_start(s, avctx) < 0)
86f85dca 1803 return DECODE_SLICE_FATAL_ERROR;
2ec23b6d
MN
1804 /* first check if we must repeat the frame */
1805 s->current_picture.repeat_pict = 0;
1806
1807 if (s->repeat_first_field) {
1808 if (s->progressive_sequence) {
1809 if (s->top_field_first)
1810 s->current_picture.repeat_pict = 4;
1811 else
1812 s->current_picture.repeat_pict = 2;
1813 } else if (s->progressive_frame) {
1814 s->current_picture.repeat_pict = 1;
1815 }
1816 }
1817// printf("%d \n", s->current_picture.repeat_pict);
1818
59b571c1 1819 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
f943e138 1820 printf("qp:%d fc:%2d%2d%2d%2d %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
59b571c1
MN
1821 s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1],
1822 s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
1823 s->progressive_sequence ? "pro" :"", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
1824 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
1825 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
1826 }
b536d0aa
MN
1827 }else{ //second field
1828 int i;
1829 for(i=0; i<4; i++){
1830 s->current_picture.data[i] = s->current_picture_ptr->data[i];
1831 if(s->picture_structure == PICT_BOTTOM_FIELD){
1832 s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
1833 }
1834 }
1835 }
de6d9b64 1836 }
dfb476cb 1837 s->first_slice = 0;
de6d9b64 1838
68f593b4 1839 init_get_bits(&s->gb, buf, buf_size*8);
de6d9b64 1840
0ee50938 1841 s->qscale = get_qscale(s);
de6d9b64 1842 /* extra slice info */
612476ef
A
1843 while (get_bits1(&s->gb) != 0) {
1844 skip_bits(&s->gb, 8);
de6d9b64
FB
1845 }
1846
ce5b7c5e
MN
1847 s->mb_x=0;
1848 for(;;) {
1849 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1850 if (code < 0)
1851 return -1; /* error = end of slice, but empty slice is bad or?*/
1852 if (code >= 33) {
1853 if (code == 33) {
1854 s->mb_x += 33;
1855 }
1856 /* otherwise, stuffing, nothing to do */
1857 } else {
1858 s->mb_x += code;
1859 break;
1860 }
1861 }
9b8709d1 1862
ce5b7c5e 1863 s->mb_y = start_code;
9b8709d1 1864 s->mb_skip_run= 0;
ce5b7c5e 1865
de6d9b64 1866 for(;;) {
eb4b3dd3 1867 s->dsp.clear_blocks(s->block[0]);
ce5b7c5e 1868
b7ec19d3 1869 ret = mpeg_decode_mb(s, s->block);
de6d9b64
FB
1870 dprintf("ret=%d\n", ret);
1871 if (ret < 0)
1872 return -1;
b536d0aa 1873
ce5b7c5e
MN
1874 MPV_decode_mb(s, s->block);
1875
1876 if (++s->mb_x >= s->mb_width) {
640950c7
MN
1877 if(s->picture_structure==PICT_FRAME){
1878 ff_draw_horiz_band(s, 16*s->mb_y, 16);
1879 }else{
1880 if(!s->first_field){
1881 ff_draw_horiz_band(s, 32*s->mb_y, 32);
1882 }
1883 }
ce5b7c5e
MN
1884
1885 s->mb_x = 0;
1886 s->mb_y++;
d87c0267 1887 PRINT_QP("%s", "\n");
ce5b7c5e 1888 }
d87c0267 1889 PRINT_QP("%2d", s->qscale);
ce5b7c5e
MN
1890
1891 /* skip mb handling */
9b8709d1 1892 if (s->mb_skip_run == -1) {
ce5b7c5e 1893 /* read again increment */
9b8709d1 1894 s->mb_skip_run = 0;
ce5b7c5e
MN
1895 for(;;) {
1896 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1897 if (code < 0)
1898 goto eos; /* error = end of slice */
1899 if (code >= 33) {
1900 if (code == 33) {
9b8709d1 1901 s->mb_skip_run += 33;
ce5b7c5e
MN
1902 }
1903 /* otherwise, stuffing, nothing to do */
1904 } else {
9b8709d1 1905 s->mb_skip_run += code;
ce5b7c5e
MN
1906 break;
1907 }
1908 }
1909 }
dfb476cb 1910 if(s->mb_y<<field_pic >= s->mb_height){
ce5b7c5e 1911 fprintf(stderr, "slice too long\n");
86f85dca 1912 return DECODE_SLICE_ERROR;
ce5b7c5e 1913 }
de6d9b64 1914 }
ce5b7c5e
MN
1915eos: //end of slice
1916
5b403c40 1917 emms_c();
dfb476cb 1918//intf("%d %d %d %d\n", s->mb_y, s->mb_height, s->pict_type, s->picture_number);
de6d9b64 1919 /* end of slice reached */
dfb476cb 1920 if (s->mb_y<<field_pic == s->mb_height && !s->first_field) {
de6d9b64 1921 /* end of image */
1e491e29
MN
1922
1923 if(s->mpeg2)
1924 s->qscale >>=1;
de6d9b64
FB
1925
1926 MPV_frame_end(s);
1927
4e00e76b 1928 if (s->pict_type == B_TYPE || s->low_delay) {
492cd3a9 1929 *pict= *(AVFrame*)&s->current_picture;
de6d9b64 1930 } else {
1e491e29 1931 s->picture_number++;
de6d9b64
FB
1932 /* latency of 1 frame for I and P frames */
1933 /* XXX: use another variable than picture_number */
b536d0aa 1934 if (s->last_picture_ptr == NULL) {
1e491e29 1935 return DECODE_SLICE_OK;
de6d9b64 1936 } else {
492cd3a9 1937 *pict= *(AVFrame*)&s->last_picture;
de6d9b64 1938 }
de6d9b64 1939 }
1e491e29 1940 return DECODE_SLICE_EOP;
de6d9b64 1941 } else {
86f85dca 1942 return DECODE_SLICE_OK;
de6d9b64
FB
1943 }
1944}
1945
1946static int mpeg1_decode_sequence(AVCodecContext *avctx,
0c1a9eda 1947 uint8_t *buf, int buf_size)
de6d9b64
FB
1948{
1949 Mpeg1Context *s1 = avctx->priv_data;
1950 MpegEncContext *s = &s1->mpeg_enc_ctx;
60832448 1951 int width, height, i, v, j;
945f15b7 1952 float aspect;
fb4a4a56 1953
68f593b4 1954 init_get_bits(&s->gb, buf, buf_size*8);
de6d9b64
FB
1955
1956 width = get_bits(&s->gb, 12);
1957 height = get_bits(&s->gb, 12);
945f15b7
MN
1958 s->aspect_ratio_info= get_bits(&s->gb, 4);
1959 if(!s->mpeg2){
1960 aspect= mpeg1_aspect[s->aspect_ratio_info];
1961 if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height);
1962 }
1963
de6d9b64
FB
1964 s->frame_rate_index = get_bits(&s->gb, 4);
1965 if (s->frame_rate_index == 0)
1966 return -1;
1967 s->bit_rate = get_bits(&s->gb, 18) * 400;
612476ef 1968 if (get_bits1(&s->gb) == 0) /* marker */
de6d9b64
FB
1969 return -1;
1970 if (width <= 0 || height <= 0 ||
1971 (width % 2) != 0 || (height % 2) != 0)
1972 return -1;
1973 if (width != s->width ||
1974 height != s->height) {
1975 /* start new mpeg1 context decoding */
1976 s->out_format = FMT_MPEG1;
1977 if (s1->mpeg_enc_ctx_allocated) {
1978 MPV_common_end(s);
1979 }
1980 s->width = width;
1981 s->height = height;
4e00e76b 1982 avctx->has_b_frames= 1;
0c23ead1 1983 s->avctx = avctx;
de6d9b64
FB
1984 avctx->width = width;
1985 avctx->height = height;
14bea432
MN
1986 av_reduce(
1987 &avctx->frame_rate,
1988 &avctx->frame_rate_base,
1989 frame_rate_tab[s->frame_rate_index],
1990 MPEG1_FRAME_RATE_BASE, //FIXME store in allready reduced form
1991 1<<30
1992 );
de6d9b64
FB
1993 avctx->bit_rate = s->bit_rate;
1994
1995 if (MPV_common_init(s) < 0)
1996 return -1;
de6d9b64
FB
1997 s1->mpeg_enc_ctx_allocated = 1;
1998 }
1999
612476ef
A
2000 skip_bits(&s->gb, 10); /* vbv_buffer_size */
2001 skip_bits(&s->gb, 1);
de6d9b64
FB
2002
2003 /* get matrix */
612476ef 2004 if (get_bits1(&s->gb)) {
de6d9b64
FB
2005 for(i=0;i<64;i++) {
2006 v = get_bits(&s->gb, 8);
2ad1516a 2007 j = s->intra_scantable.permutated[i];
60832448
FB
2008 s->intra_matrix[j] = v;
2009 s->chroma_intra_matrix[j] = v;
de6d9b64 2010 }
25ed7f92
FB
2011#ifdef DEBUG
2012 dprintf("intra matrix present\n");
2013 for(i=0;i<64;i++)
8e1652dc 2014 dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]);
25ed7f92
FB
2015 printf("\n");
2016#endif
de6d9b64
FB
2017 } else {
2018 for(i=0;i<64;i++) {
b0368839 2019 int j= s->dsp.idct_permutation[i];
adc09b2e 2020 v = ff_mpeg1_default_intra_matrix[i];
2ad1516a
MN
2021 s->intra_matrix[j] = v;
2022 s->chroma_intra_matrix[j] = v;
de6d9b64
FB
2023 }
2024 }
612476ef 2025 if (get_bits1(&s->gb)) {
de6d9b64
FB
2026 for(i=0;i<64;i++) {
2027 v = get_bits(&s->gb, 8);
2ad1516a 2028 j = s->intra_scantable.permutated[i];
d7e9533a
MN
2029 s->inter_matrix[j] = v;
2030 s->chroma_inter_matrix[j] = v;
de6d9b64 2031 }
25ed7f92
FB
2032#ifdef DEBUG
2033 dprintf("non intra matrix present\n");
2034 for(i=0;i<64;i++)
8e1652dc 2035 dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]);
25ed7f92
FB
2036 printf("\n");
2037#endif
de6d9b64
FB
2038 } else {
2039 for(i=0;i<64;i++) {
b0368839 2040 int j= s->dsp.idct_permutation[i];
adc09b2e 2041 v = ff_mpeg1_default_non_intra_matrix[i];
2ad1516a
MN
2042 s->inter_matrix[j] = v;
2043 s->chroma_inter_matrix[j] = v;
de6d9b64
FB
2044 }
2045 }
2046
2047 /* we set mpeg2 parameters so that it emulates mpeg1 */
2048 s->progressive_sequence = 1;
2049 s->progressive_frame = 1;
2050 s->picture_structure = PICT_FRAME;
2051 s->frame_pred_frame_dct = 1;
2052 s->mpeg2 = 0;
fb4a4a56 2053 avctx->sub_id = 1; /* indicates mpeg1 */
de6d9b64
FB
2054 return 0;
2055}
2056
e2f9490e
FB
2057static void mpeg_decode_user_data(AVCodecContext *avctx,
2058 const uint8_t *buf, int buf_size)
2059{
2060 const uint8_t *p;
2061 int len, flags;
2062 p = buf;
2063 len = buf_size;
2064
2065 /* we parse the DTG active format information */
2066 if (len >= 5 &&
2067 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2068 flags = p[4];
2069 p += 5;
2070 len -= 5;
2071 if (flags & 0x80) {
2072 /* skip event id */
2073 if (len < 2)
2074 return;
2075 p += 2;
2076 len -= 2;
2077 }
2078 if (flags & 0x40) {
2079 if (len < 1)
2080 return;
2081 avctx->dtg_active_format = p[0] & 0x0f;
2082 }
2083 }
2084}
2085
de6d9b64
FB
2086/* handle buffering and image synchronisation */
2087static int mpeg_decode_frame(AVCodecContext *avctx,
2088 void *data, int *data_size,
0c1a9eda 2089 uint8_t *buf, int buf_size)
de6d9b64
FB
2090{
2091 Mpeg1Context *s = avctx->priv_data;
0c1a9eda 2092 uint8_t *buf_end, *buf_ptr, *buf_start;
de6d9b64 2093 int len, start_code_found, ret, code, start_code, input_size;
492cd3a9 2094 AVFrame *picture = data;
1cb0edb4
J
2095 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2096
de6d9b64
FB
2097 dprintf("fill_buffer\n");
2098
2099 *data_size = 0;
d7e9533a 2100
de6d9b64
FB
2101 /* special case for last picture */
2102 if (buf_size == 0) {
de6d9b64 2103 if (s2->picture_number > 0) {
492cd3a9 2104 *picture= *(AVFrame*)&s2->next_picture;
1e491e29 2105
492cd3a9 2106 *data_size = sizeof(AVFrame);
de6d9b64
FB
2107 }
2108 return 0;
2109 }
2110
2111 buf_ptr = buf;
2112 buf_end = buf + buf_size;
e0a3d744
J
2113
2114#if 0
2115 if (s->repeat_field % 2 == 1) {
1cb0edb4
J
2116 s->repeat_field++;
2117 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
e0a3d744
J
2118 // s2->picture_number, s->repeat_field);
2119 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
2120 *data_size = sizeof(AVPicture);
2121 goto the_end;
2122 }
1cb0edb4 2123 }
e0a3d744 2124#endif
de6d9b64
FB
2125 while (buf_ptr < buf_end) {
2126 buf_start = buf_ptr;
2127 /* find start next code */
2128 code = find_start_code(&buf_ptr, buf_end, &s->header_state);
2129 if (code >= 0) {
2130 start_code_found = 1;
2131 } else {
2132 start_code_found = 0;
2133 }
2134 /* copy to buffer */
2135 len = buf_ptr - buf_start;
2136 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
2137 /* data too big : flush */
2138 s->buf_ptr = s->buffer;
2139 if (start_code_found)
2140 s->start_code = code;
2141 } else {
2142 memcpy(s->buf_ptr, buf_start, len);
2143 s->buf_ptr += len;
d7425f59 2144 if( (!(s2->flags&CODEC_FLAG_TRUNCATED)) && (!start_code_found)
14207b27
MN
2145 && s->buf_ptr+4<s->buffer+s->buffer_size){
2146 start_code_found= 1;
2147 code= 0x1FF;
2148 s->header_state=0xFF;
2149 s->buf_ptr[0]=0;
2150 s->buf_ptr[1]=0;
2151 s->buf_ptr[2]=1;
2152 s->buf_ptr[3]=0xFF;
2153 s->buf_ptr+=4;
2154 }
de6d9b64
FB
2155 if (start_code_found) {
2156 /* prepare data for next start code */
2157 input_size = s->buf_ptr - s->buffer;
2158 start_code = s->start_code;
2159 s->buf_ptr = s->buffer;
2160 s->start_code = code;
2161 switch(start_code) {
2162 case SEQ_START_CODE:
2163 mpeg1_decode_sequence(avctx, s->buffer,
2164 input_size);
2165 break;
2166
2167 case PICTURE_START_CODE:
2168 /* we have a complete image : we try to decompress it */
2169 mpeg1_decode_picture(avctx,
2170 s->buffer, input_size);
2171 break;
2172 case EXT_START_CODE:
2173 mpeg_decode_extension(avctx,
2174 s->buffer, input_size);
e2f9490e
FB
2175 break;
2176 case USER_START_CODE:
2177 mpeg_decode_user_data(avctx,
2178 s->buffer, input_size);
de6d9b64
FB
2179 break;
2180 default:
2181 if (start_code >= SLICE_MIN_START_CODE &&
d9cb5429 2182 start_code <= SLICE_MAX_START_CODE) {
945f15b7
MN
2183
2184 /* skip b frames if we dont have reference frames */
b536d0aa 2185 if(s2->last_picture_ptr==NULL && s2->pict_type==B_TYPE) break;
945f15b7
MN
2186 /* skip b frames if we are in a hurry */
2187 if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
2188 /* skip everything if we are in a hurry>=5 */
2189 if(avctx->hurry_up>=5) break;
2190
de6d9b64
FB
2191 ret = mpeg_decode_slice(avctx, picture,
2192 start_code, s->buffer, input_size);
dfb476cb 2193
86f85dca 2194 if (ret == DECODE_SLICE_EOP) {
de6d9b64
FB
2195 *data_size = sizeof(AVPicture);
2196 goto the_end;
288daa84 2197 }else if(ret<0){
2aa29681 2198 fprintf(stderr,"Error while decoding slice\n");
86f85dca 2199 if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
de6d9b64
FB
2200 }
2201 }
2202 break;
2203 }
2204 }
2205 }
2206 }
2207 the_end:
2208 return buf_ptr - buf;
2209}
2210
2211static int mpeg_decode_end(AVCodecContext *avctx)
2212{
2213 Mpeg1Context *s = avctx->priv_data;
2214
2215 if (s->mpeg_enc_ctx_allocated)
2216 MPV_common_end(&s->mpeg_enc_ctx);
2217 return 0;
2218}
2219
2220AVCodec mpeg_decoder = {
2221 "mpegvideo",
2222 CODEC_TYPE_VIDEO,
2223 CODEC_ID_MPEG1VIDEO,
2224 sizeof(Mpeg1Context),
2225 mpeg_decode_init,
2226 NULL,
2227 mpeg_decode_end,
2228 mpeg_decode_frame,
d7425f59 2229 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
de6d9b64 2230};