100000l
[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
029911d1 22 * MPEG1/2 codec
983e3246
MN
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 32
de6d9b64
FB
33/* Start codes. */
34#define SEQ_END_CODE 0x000001b7
35#define SEQ_START_CODE 0x000001b3
36#define GOP_START_CODE 0x000001b8
37#define PICTURE_START_CODE 0x00000100
38#define SLICE_MIN_START_CODE 0x00000101
39#define SLICE_MAX_START_CODE 0x000001af
40#define EXT_START_CODE 0x000001b5
41#define USER_START_CODE 0x000001b2
42
4f68b084
MN
43#define DC_VLC_BITS 9
44#define MV_VLC_BITS 9
45#define MBINCR_VLC_BITS 9
46#define MB_PAT_VLC_BITS 9
47#define MB_PTYPE_VLC_BITS 6
48#define MB_BTYPE_VLC_BITS 6
49#define TEX_VLC_BITS 9
50
764ef400 51#ifdef CONFIG_ENCODERS
de6d9b64
FB
52static void mpeg1_encode_block(MpegEncContext *s,
53 DCTELEM *block,
54 int component);
63b15e55 55static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
764ef400 56#endif //CONFIG_ENCODERS
de6d9b64 57static void mpeg1_skip_picture(MpegEncContext *s, int pict_num);
a0201736
MN
58static inline int mpeg1_decode_block_inter(MpegEncContext *s,
59 DCTELEM *block,
60 int n);
61static inline int mpeg1_decode_block_intra(MpegEncContext *s,
de6d9b64
FB
62 DCTELEM *block,
63 int n);
3729c912 64static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
de6d9b64
FB
65 DCTELEM *block,
66 int n);
3729c912 67static inline int mpeg2_decode_block_intra(MpegEncContext *s,
de6d9b64
FB
68 DCTELEM *block,
69 int n);
70static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
57489bc5 71static void exchange_uv(MpegEncContext *s);
de6d9b64 72
2e7b4c84
IK
73#ifdef HAVE_XVMC
74extern int XVMC_field_start(MpegEncContext *s, AVCodecContext *avctx);
75extern int XVMC_field_end(MpegEncContext *s);
a579db0c 76extern void XVMC_pack_pblocks(MpegEncContext *s,int cbp);
57489bc5 77extern void XVMC_init_block(MpegEncContext *s);//set s->block
2e7b4c84
IK
78#endif
79
c442d75c 80#ifdef CONFIG_ENCODERS
30952237 81static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
0c1a9eda 82static uint8_t fcode_tab[MAX_MV*2+1];
11ce8834 83
c442d75c
MN
84static uint32_t uni_mpeg1_ac_vlc_bits[64*64*2];
85static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
2a250222
MN
86
87/* simple include everything table for dc, first byte is bits number next 3 are code*/
88static uint32_t mpeg1_lum_dc_uni[512];
89static uint32_t mpeg1_chr_dc_uni[512];
90
91static uint8_t mpeg1_index_run[2][64];
92static int8_t mpeg1_max_level[2][64];
764ef400 93#endif //CONFIG_ENCODERS
c442d75c 94
4f68b084
MN
95static void init_2d_vlc_rl(RLTable *rl)
96{
07787186 97 int i;
4f68b084
MN
98
99 init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2,
100 &rl->table_vlc[0][1], 4, 2,
101 &rl->table_vlc[0][0], 4, 2);
102
103
104 rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
105 for(i=0; i<rl->vlc.table_size; i++){
106 int code= rl->vlc.table[i][0];
107 int len = rl->vlc.table[i][1];
108 int level, run;
109
110 if(len==0){ // illegal code
111 run= 65;
112 level= MAX_LEVEL;
113 }else if(len<0){ //more bits needed
114 run= 0;
115 level= code;
116 }else{
117 if(code==rl->n){ //esc
118 run= 65;
119 level= 0;
120 }else if(code==rl->n+1){ //eob
a0201736
MN
121 run= 0;
122 level= 127;
4f68b084
MN
123 }else{
124 run= rl->table_run [code] + 1;
125 level= rl->table_level[code];
126 }
127 }
128 rl->rl_vlc[0][i].len= len;
129 rl->rl_vlc[0][i].level= level;
130 rl->rl_vlc[0][i].run= run;
131 }
132}
133
2a250222 134#ifdef CONFIG_ENCODERS
c442d75c
MN
135static void init_uni_ac_vlc(RLTable *rl, uint32_t *uni_ac_vlc_bits, uint8_t *uni_ac_vlc_len){
136 int i;
137
138 for(i=0; i<128; i++){
139 int level= i-64;
140 int run;
141 for(run=0; run<64; run++){
142 int len, bits, code;
143
144 int alevel= ABS(level);
145 int sign= (level>>31)&1;
146
147 if (alevel > rl->max_level[0][run])
148 code= 111; /*rl->n*/
149 else
150 code= rl->index_run[0][run] + alevel - 1;
151
152 if (code < 111 /* rl->n */) {
153 /* store the vlc & sign at once */
154 len= mpeg1_vlc[code][1]+1;
155 bits= (mpeg1_vlc[code][0]<<1) + sign;
156 } else {
157 len= mpeg1_vlc[111/*rl->n*/][1]+6;
158 bits= mpeg1_vlc[111/*rl->n*/][0]<<6;
159
160 bits|= run;
161 if (alevel < 128) {
162 bits<<=8; len+=8;
163 bits|= level & 0xff;
164 } else {
165 bits<<=16; len+=16;
166 bits|= level & 0xff;
167 if (level < 0) {
168 bits|= 0x8001 + level + 255;
169 } else {
170 bits|= level & 0xffff;
171 }
172 }
173 }
174
175 uni_ac_vlc_bits[UNI_AC_ENC_INDEX(run, i)]= bits;
176 uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
177 }
178 }
179}
4f68b084 180
de6d9b64
FB
181static void put_header(MpegEncContext *s, int header)
182{
183 align_put_bits(&s->pb);
80feb2a2
MN
184 put_bits(&s->pb, 16, header>>16);
185 put_bits(&s->pb, 16, header&0xFFFF);
de6d9b64
FB
186}
187
188/* put sequence header if needed */
189static void mpeg1_encode_sequence_header(MpegEncContext *s)
190{
191 unsigned int vbv_buffer_size;
d78647e8 192 unsigned int fps, v;
426b8061 193 int n, i;
0c1a9eda 194 uint64_t time_code;
426b8061 195 float best_aspect_error= 1E10;
5ff85f1d 196 float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
98b0858e 197 int constraint_parameter_flag;
426b8061 198
5ff85f1d 199 if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
de6d9b64 200
1e491e29 201 if (s->current_picture.key_frame) {
de6d9b64
FB
202 /* mpeg1 header repeated every gop */
203 put_header(s, SEQ_START_CODE);
204
205 /* search closest frame rate */
206 {
207 int i, dmin, d;
208 s->frame_rate_index = 0;
209 dmin = 0x7fffffff;
14bea432
MN
210 for(i=1;i<14;i++) {
211 if(s->avctx->strict_std_compliance >= 0 && i>=9) break;
212
213 d = abs(MPEG1_FRAME_RATE_BASE*(int64_t)s->avctx->frame_rate/s->avctx->frame_rate_base - frame_rate_tab[i]);
de6d9b64
FB
214 if (d < dmin) {
215 dmin = d;
216 s->frame_rate_index = i;
217 }
218 }
219 }
220
221 put_bits(&s->pb, 12, s->width);
222 put_bits(&s->pb, 12, s->height);
426b8061
MN
223
224 for(i=1; i<15; i++){
5c9e4723
MN
225 float error= aspect_ratio;
226 if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
f259747b 227 error-= 1.0/mpeg1_aspect[i];
5c9e4723
MN
228 else
229 error-= av_q2d(mpeg2_aspect[i])*s->height/s->width;
230
426b8061
MN
231 error= ABS(error);
232
233 if(error < best_aspect_error){
234 best_aspect_error= error;
235 s->aspect_ratio_info= i;
236 }
237 }
238
239 put_bits(&s->pb, 4, s->aspect_ratio_info);
de6d9b64 240 put_bits(&s->pb, 4, s->frame_rate_index);
12dccd4e
MN
241
242 if(s->avctx->rc_max_rate){
243 v = (s->avctx->rc_max_rate + 399) / 400;
244 if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
245 v = 0x3ffff;
246 }else{
247 v= 0x3FFFF;
248 }
ecfd40b3
HM
249
250 if(s->avctx->rc_buffer_size)
251 vbv_buffer_size = s->avctx->rc_buffer_size;
252 else
253 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
12dccd4e
MN
254 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
255 vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
256
257 put_bits(&s->pb, 18, v & 0x3FFFF);
258 put_bits(&s->pb, 1, 1); /* marker */
98b0858e
MN
259 put_bits(&s->pb, 10, vbv_buffer_size & 0x3FF);
260
261 constraint_parameter_flag=
262 s->width <= 768 && s->height <= 576 &&
263 s->mb_width * s->mb_height <= 396 &&
264 s->mb_width * s->mb_height * frame_rate_tab[s->frame_rate_index] <= MPEG1_FRAME_RATE_BASE*396*25 &&
265 frame_rate_tab[s->frame_rate_index] <= MPEG1_FRAME_RATE_BASE*30 &&
266 vbv_buffer_size <= 20 &&
267 v <= 1856000/400 &&
268 s->codec_id == CODEC_ID_MPEG1VIDEO;
269
270 put_bits(&s->pb, 1, constraint_parameter_flag);
d6eb3c50
MN
271
272 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
273 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
de6d9b64 274
029911d1
MN
275 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
276 put_header(s, EXT_START_CODE);
277 put_bits(&s->pb, 4, 1); //seq ext
278 put_bits(&s->pb, 1, 0); //esc
279 put_bits(&s->pb, 3, 4); //profile
280 put_bits(&s->pb, 4, 8); //level
3ab5b8cc 281 put_bits(&s->pb, 1, s->progressive_sequence);
029911d1
MN
282 put_bits(&s->pb, 2, 1); //chroma format 4:2:0
283 put_bits(&s->pb, 2, 0); //horizontal size ext
284 put_bits(&s->pb, 2, 0); //vertical size ext
285 put_bits(&s->pb, 12, v>>18); //bitrate ext
286 put_bits(&s->pb, 1, 1); //marker
287 put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
288 put_bits(&s->pb, 1, s->low_delay);
289 put_bits(&s->pb, 2, 0); // frame_rate_ext_n
290 put_bits(&s->pb, 5, 0); // frame_rate_ext_d
291 }
292
de6d9b64
FB
293 put_header(s, GOP_START_CODE);
294 put_bits(&s->pb, 1, 0); /* do drop frame */
295 /* time code : we must convert from the real frame rate to a
296 fake mpeg frame rate in case of low frame rate */
297 fps = frame_rate_tab[s->frame_rate_index];
14bea432 298 time_code = (int64_t)s->fake_picture_number * MPEG1_FRAME_RATE_BASE;
de6d9b64 299 s->gop_picture_number = s->fake_picture_number;
0c1a9eda
ZK
300 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
301 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
de6d9b64 302 put_bits(&s->pb, 1, 1);
0c1a9eda 303 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
14bea432 304 put_bits(&s->pb, 6, (uint32_t)((time_code % fps) / MPEG1_FRAME_RATE_BASE));
69b0ff3a 305 put_bits(&s->pb, 1, 0); /* closed gop */
de6d9b64
FB
306 put_bits(&s->pb, 1, 0); /* broken link */
307 }
308
14bea432 309 if (s->avctx->frame_rate < (24 * s->avctx->frame_rate_base) && s->picture_number > 0) {
de6d9b64
FB
310 /* insert empty P pictures to slow down to the desired
311 frame rate. Each fake pictures takes about 20 bytes */
312 fps = frame_rate_tab[s->frame_rate_index];
14bea432 313 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
314 while (s->fake_picture_number < n) {
315 mpeg1_skip_picture(s, s->fake_picture_number -
316 s->gop_picture_number);
317 s->fake_picture_number++;
318 }
319
320 }
de6d9b64
FB
321}
322
9b8709d1
MN
323static inline void encode_mb_skip_run(MpegEncContext *s, int run){
324 while (run >= 33) {
325 put_bits(&s->pb, 11, 0x008);
326 run -= 33;
327 }
328 put_bits(&s->pb, mbAddrIncrTable[run][1],
329 mbAddrIncrTable[run][0]);
330}
de6d9b64
FB
331
332/* insert a fake P picture */
333static void mpeg1_skip_picture(MpegEncContext *s, int pict_num)
334{
029911d1
MN
335 assert(s->codec_id == CODEC_ID_MPEG1VIDEO); // mpeg2 can do these repeat things
336
de6d9b64
FB
337 /* mpeg1 picture header */
338 put_header(s, PICTURE_START_CODE);
339 /* temporal reference */
340 put_bits(&s->pb, 10, pict_num & 0x3ff);
341
342 put_bits(&s->pb, 3, P_TYPE);
343 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
344
345 put_bits(&s->pb, 1, 1); /* integer coordinates */
346 put_bits(&s->pb, 3, 1); /* forward_f_code */
347
348 put_bits(&s->pb, 1, 0); /* extra bit picture */
349
350 /* only one slice */
351 put_header(s, SLICE_MIN_START_CODE);
352 put_bits(&s->pb, 5, 1); /* quantizer scale */
353 put_bits(&s->pb, 1, 0); /* slice extra information */
354
9b8709d1 355 encode_mb_skip_run(s, 0);
de6d9b64
FB
356
357 /* empty macroblock */
358 put_bits(&s->pb, 3, 1); /* motion only */
359
360 /* zero motion x & y */
361 put_bits(&s->pb, 1, 1);
362 put_bits(&s->pb, 1, 1);
363
364 /* output a number of empty slice */
9b8709d1 365 encode_mb_skip_run(s, s->mb_width * s->mb_height - 2);
de6d9b64
FB
366
367 /* empty macroblock */
368 put_bits(&s->pb, 3, 1); /* motion only */
369
370 /* zero motion x & y */
371 put_bits(&s->pb, 1, 1);
372 put_bits(&s->pb, 1, 1);
373}
764ef400 374#endif //CONFIG_ENCODERS
de6d9b64 375
8f8402e4 376static void common_init(MpegEncContext *s)
de6d9b64 377{
8f8402e4
MN
378 s->y_dc_scale_table=
379 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
380}
f004ca1c 381
2a250222
MN
382void ff_mpeg1_clean_buffers(MpegEncContext *s){
383 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
384 s->last_dc[1] = s->last_dc[0];
385 s->last_dc[2] = s->last_dc[0];
386 memset(s->last_mv, 0, sizeof(s->last_mv));
387}
388
7604246d 389#ifdef CONFIG_ENCODERS
9b8709d1
MN
390
391void ff_mpeg1_encode_slice_header(MpegEncContext *s){
392 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
393 put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
394 put_bits(&s->pb, 1, 0); /* slice extra information */
395}
396
8f8402e4
MN
397void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
398{
de6d9b64
FB
399 mpeg1_encode_sequence_header(s);
400
401 /* mpeg1 picture header */
402 put_header(s, PICTURE_START_CODE);
403 /* temporal reference */
63b15e55
MN
404
405 // RAL: s->picture_number instead of s->fake_picture_number
406 put_bits(&s->pb, 10, (s->picture_number -
de6d9b64 407 s->gop_picture_number) & 0x3ff);
af469427 408 s->fake_picture_number++;
de6d9b64
FB
409
410 put_bits(&s->pb, 3, s->pict_type);
d60a8f85
MN
411
412 s->vbv_delay_ptr= s->pb.buf + get_bit_count(&s->pb)/8;
413 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
de6d9b64 414
63b15e55
MN
415 // RAL: Forward f_code also needed for B frames
416 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
de6d9b64 417 put_bits(&s->pb, 1, 0); /* half pel coordinates */
cb231c48
MN
418 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
419 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
420 else
421 put_bits(&s->pb, 3, 7); /* forward_f_code */
de6d9b64
FB
422 }
423
63b15e55
MN
424 // RAL: Backward f_code necessary for B frames
425 if (s->pict_type == B_TYPE) {
426 put_bits(&s->pb, 1, 0); /* half pel coordinates */
cb231c48
MN
427 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
428 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
429 else
430 put_bits(&s->pb, 3, 7); /* backward_f_code */
029911d1 431 }
63b15e55 432
de6d9b64 433 put_bits(&s->pb, 1, 0); /* extra bit picture */
3ab5b8cc
FB
434
435 s->frame_pred_frame_dct = 1;
029911d1
MN
436 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
437 put_header(s, EXT_START_CODE);
438 put_bits(&s->pb, 4, 8); //pic ext
cb231c48
MN
439 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
440 put_bits(&s->pb, 4, s->f_code);
441 put_bits(&s->pb, 4, s->f_code);
442 }else{
443 put_bits(&s->pb, 8, 255);
444 }
445 if (s->pict_type == B_TYPE) {
446 put_bits(&s->pb, 4, s->b_code);
447 put_bits(&s->pb, 4, s->b_code);
448 }else{
449 put_bits(&s->pb, 8, 255);
450 }
029911d1
MN
451 put_bits(&s->pb, 2, s->intra_dc_precision);
452 put_bits(&s->pb, 2, s->picture_structure= PICT_FRAME);
7a0f9d7e
FB
453 if (s->progressive_sequence) {
454 put_bits(&s->pb, 1, 0); /* no repeat */
455 } else {
456 put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
457 }
3ab5b8cc
FB
458 /* XXX: optimize the generation of this flag with entropy
459 measures */
460 s->frame_pred_frame_dct = s->progressive_sequence;
461
462 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
029911d1
MN
463 put_bits(&s->pb, 1, s->concealment_motion_vectors);
464 put_bits(&s->pb, 1, s->q_scale_type);
465 put_bits(&s->pb, 1, s->intra_vlc_format);
466 put_bits(&s->pb, 1, s->alternate_scan);
467 put_bits(&s->pb, 1, s->repeat_first_field);
468 put_bits(&s->pb, 1, s->chroma_420_type=1);
3ab5b8cc
FB
469 s->progressive_frame = s->progressive_sequence;
470 put_bits(&s->pb, 1, s->progressive_frame);
029911d1
MN
471 put_bits(&s->pb, 1, 0); //composite_display_flag
472 }
473
9b8709d1
MN
474 s->mb_y=0;
475 ff_mpeg1_encode_slice_header(s);
de6d9b64
FB
476}
477
3ab5b8cc
FB
478static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
479 int has_mv)
480{
481 put_bits(&s->pb, n, bits);
482 if (!s->frame_pred_frame_dct) {
483 if (has_mv)
484 put_bits(&s->pb, 2, 2); /* motion_type: frame */
485 put_bits(&s->pb, 1, s->interlaced_dct);
486 }
487}
488
de6d9b64
FB
489void mpeg1_encode_mb(MpegEncContext *s,
490 DCTELEM block[6][64],
491 int motion_x, int motion_y)
492{
9b8709d1
MN
493 int i, cbp;
494 const int mb_x = s->mb_x;
495 const int mb_y = s->mb_y;
496 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
de6d9b64
FB
497
498 /* compute cbp */
499 cbp = 0;
500 for(i=0;i<6;i++) {
501 if (s->block_last_index[i] >= 0)
502 cbp |= 1 << (5 - i);
503 }
504
029911d1 505 if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
63b15e55
MN
506 ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) ||
507 (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) |
508 ((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 509 s->mb_skip_run++;
d87c0267 510 s->qscale -= s->dquant;
49733979
MN
511 s->skip_count++;
512 s->misc_bits++;
513 s->last_bits++;
de6d9b64 514 } else {
9b8709d1
MN
515 if(first_mb){
516 assert(s->mb_skip_run == 0);
517 encode_mb_skip_run(s, s->mb_x);
518 }else{
519 encode_mb_skip_run(s, s->mb_skip_run);
de6d9b64 520 }
de6d9b64
FB
521
522 if (s->pict_type == I_TYPE) {
d87c0267 523 if(s->dquant && cbp){
3ab5b8cc 524 put_mb_modes(s, 2, 1, 0); /* macroblock_type : macroblock_quant = 1 */
d87c0267
MN
525 put_bits(&s->pb, 5, s->qscale);
526 }else{
3ab5b8cc 527 put_mb_modes(s, 1, 1, 0); /* macroblock_type : macroblock_quant = 0 */
d87c0267
MN
528 s->qscale -= s->dquant;
529 }
49733979
MN
530 s->misc_bits+= get_bits_diff(s);
531 s->i_count++;
63b15e55
MN
532 } else if (s->mb_intra) {
533 if(s->dquant && cbp){
3ab5b8cc 534 put_mb_modes(s, 6, 0x01, 0);
63b15e55
MN
535 put_bits(&s->pb, 5, s->qscale);
536 }else{
3ab5b8cc 537 put_mb_modes(s, 5, 0x03, 0);
63b15e55
MN
538 s->qscale -= s->dquant;
539 }
540 s->misc_bits+= get_bits_diff(s);
541 s->i_count++;
542 s->last_mv[0][0][0] =
543 s->last_mv[0][0][1] = 0;
544 } else if (s->pict_type == P_TYPE) {
de6d9b64
FB
545 if (cbp != 0) {
546 if (motion_x == 0 && motion_y == 0) {
d87c0267 547 if(s->dquant){
3ab5b8cc 548 put_mb_modes(s, 5, 1, 0); /* macroblock_pattern & quant */
d87c0267
MN
549 put_bits(&s->pb, 5, s->qscale);
550 }else{
3ab5b8cc 551 put_mb_modes(s, 2, 1, 0); /* macroblock_pattern only */
d87c0267 552 }
49733979 553 s->misc_bits+= get_bits_diff(s);
de6d9b64
FB
554 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
555 } else {
d87c0267 556 if(s->dquant){
3ab5b8cc 557 put_mb_modes(s, 5, 2, 1); /* motion + cbp */
d87c0267
MN
558 put_bits(&s->pb, 5, s->qscale);
559 }else{
3ab5b8cc 560 put_mb_modes(s, 1, 1, 1); /* motion + cbp */
d87c0267 561 }
49733979 562 s->misc_bits+= get_bits_diff(s);
63b15e55
MN
563 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
564 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
49733979 565 s->mv_bits+= get_bits_diff(s);
de6d9b64
FB
566 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
567 }
568 } else {
569 put_bits(&s->pb, 3, 1); /* motion only */
3ab5b8cc
FB
570 if (!s->frame_pred_frame_dct)
571 put_bits(&s->pb, 2, 2); /* motion_type: frame */
63b15e55
MN
572 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
573 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
d87c0267 574 s->qscale -= s->dquant;
49733979 575 s->mv_bits+= get_bits_diff(s);
de6d9b64 576 }
49733979 577 s->f_count++;
63b15e55
MN
578 } else
579 { // RAL: All the following bloc added for B frames:
580 if (cbp != 0)
581 { // With coded bloc pattern
582 if (s->mv_dir == (MV_DIR_FORWARD | MV_DIR_BACKWARD))
583 { // Bi-directional motion
3ab5b8cc
FB
584 if (s->dquant) {
585 put_mb_modes(s, 5, 2, 1);
63b15e55 586 put_bits(&s->pb, 5, s->qscale);
3ab5b8cc
FB
587 } else {
588 put_mb_modes(s, 2, 3, 1);
589 }
63b15e55
MN
590 s->misc_bits += get_bits_diff(s);
591 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
592 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
593 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
594 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
595 s->b_count++;
596 s->f_count++;
597 s->mv_bits += get_bits_diff(s);
598 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
599 }
600 else if (s->mv_dir == MV_DIR_BACKWARD)
601 { // Backward motion
3ab5b8cc
FB
602 if (s->dquant) {
603 put_mb_modes(s, 6, 2, 1);
63b15e55 604 put_bits(&s->pb, 5, s->qscale);
3ab5b8cc
FB
605 } else {
606 put_mb_modes(s, 3, 3, 1);
607 }
63b15e55
MN
608 s->misc_bits += get_bits_diff(s);
609 mpeg1_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code);
610 mpeg1_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code);
611 s->b_count++;
612 s->mv_bits += get_bits_diff(s);
613 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
614 }
615 else if (s->mv_dir == MV_DIR_FORWARD)
616 { // Forward motion
3ab5b8cc
FB
617 if (s->dquant) {
618 put_mb_modes(s, 6, 3, 1);
63b15e55 619 put_bits(&s->pb, 5, s->qscale);
3ab5b8cc
FB
620 } else {
621 put_mb_modes(s, 4, 3, 1);
622 }
63b15e55
MN
623 s->misc_bits += get_bits_diff(s);
624 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);
625 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);
626 s->f_count++;
627 s->mv_bits += get_bits_diff(s);
628 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
629 }
630 }
631 else
632 { // No coded bloc pattern
633 if (s->mv_dir == (MV_DIR_FORWARD | MV_DIR_BACKWARD))
7604246d 634 { // Bi-directional motion
63b15e55 635 put_bits(&s->pb, 2, 2); /* backward & forward motion */
3ab5b8cc
FB
636 if (!s->frame_pred_frame_dct)
637 put_bits(&s->pb, 2, 2); /* motion_type: frame */
63b15e55
MN
638 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
639 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
640 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
641 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
642 s->b_count++;
643 s->f_count++;
644 }
645 else if (s->mv_dir == MV_DIR_BACKWARD)
646 { // Backward motion
647 put_bits(&s->pb, 3, 2); /* backward motion only */
3ab5b8cc
FB
648 if (!s->frame_pred_frame_dct)
649 put_bits(&s->pb, 2, 2); /* motion_type: frame */
63b15e55
MN
650 mpeg1_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code);
651 mpeg1_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code);
652 s->b_count++;
653 }
654 else if (s->mv_dir == MV_DIR_FORWARD)
655 { // Forward motion
656 put_bits(&s->pb, 4, 2); /* forward motion only */
3ab5b8cc
FB
657 if (!s->frame_pred_frame_dct)
658 put_bits(&s->pb, 2, 2); /* motion_type: frame */
63b15e55
MN
659 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);
660 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);
661 s->f_count++;
662 }
663 s->qscale -= s->dquant;
664 s->mv_bits += get_bits_diff(s);
665 }
666 // End of bloc from RAL
de6d9b64 667 }
de6d9b64
FB
668 for(i=0;i<6;i++) {
669 if (cbp & (1 << (5 - i))) {
670 mpeg1_encode_block(s, block[i], i);
671 }
672 }
9b8709d1 673 s->mb_skip_run = 0;
49733979
MN
674 if(s->mb_intra)
675 s->i_tex_bits+= get_bits_diff(s);
676 else
677 s->p_tex_bits+= get_bits_diff(s);
de6d9b64 678 }
63b15e55
MN
679
680 // RAL: By this:
681 if (s->mv_dir & MV_DIR_FORWARD)
682 {
683 s->last_mv[0][0][0]= s->mv[0][0][0];
684 s->last_mv[0][0][1]= s->mv[0][0][1];
685 }
686 if (s->mv_dir & MV_DIR_BACKWARD)
687 {
688 s->last_mv[1][0][0]= s->mv[1][0][0];
689 s->last_mv[1][0][1]= s->mv[1][0][1];
690 }
de6d9b64
FB
691}
692
63b15e55
MN
693// RAL: Parameter added: f_or_b_code
694static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
de6d9b64
FB
695{
696 int code, bit_size, l, m, bits, range, sign;
697
698 if (val == 0) {
699 /* zero vector */
700 code = 0;
701 put_bits(&s->pb,
702 mbMotionVectorTable[0][1],
703 mbMotionVectorTable[0][0]);
704 } else {
63b15e55 705 bit_size = f_or_b_code - 1;
de6d9b64
FB
706 range = 1 << bit_size;
707 /* modulo encoding */
708 l = 16 * range;
709 m = 2 * l;
710 if (val < -l) {
711 val += m;
712 } else if (val >= l) {
713 val -= m;
714 }
715
716 if (val >= 0) {
717 val--;
718 code = (val >> bit_size) + 1;
719 bits = val & (range - 1);
720 sign = 0;
721 } else {
722 val = -val;
723 val--;
724 code = (val >> bit_size) + 1;
725 bits = val & (range - 1);
726 sign = 1;
727 }
63b15e55
MN
728
729 assert(code > 0 && code <= 16);
730
de6d9b64
FB
731 put_bits(&s->pb,
732 mbMotionVectorTable[code][1],
733 mbMotionVectorTable[code][0]);
63b15e55 734
de6d9b64
FB
735 put_bits(&s->pb, 1, sign);
736 if (bit_size > 0) {
737 put_bits(&s->pb, bit_size, bits);
738 }
739 }
740}
741
8f8402e4 742void ff_mpeg1_encode_init(MpegEncContext *s)
11ce8834
MN
743{
744 static int done=0;
8f8402e4
MN
745
746 common_init(s);
747
11ce8834
MN
748 if(!done){
749 int f_code;
750 int mv;
8f8402e4 751 int i;
11ce8834
MN
752
753 done=1;
8f8402e4 754 init_rl(&rl_mpeg1);
c442d75c 755
8f8402e4
MN
756 for(i=0; i<64; i++)
757 {
758 mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
759 mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
760 }
c442d75c
MN
761
762 init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_bits, uni_mpeg1_ac_vlc_len);
8f8402e4
MN
763
764 /* build unified dc encoding tables */
765 for(i=-255; i<256; i++)
766 {
767 int adiff, index;
768 int bits, code;
769 int diff=i;
770
771 adiff = ABS(diff);
772 if(diff<0) diff--;
773 index = vlc_dc_table[adiff];
774
775 bits= vlc_dc_lum_bits[index] + index;
776 code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
777 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
778
779 bits= vlc_dc_chroma_bits[index] + index;
780 code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
781 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
782 }
783
30952237
MN
784 mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
785
11ce8834
MN
786 for(f_code=1; f_code<=MAX_FCODE; f_code++){
787 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
788 int len;
789
790 if(mv==0) len= mbMotionVectorTable[0][1];
791 else{
792 int val, bit_size, range, code;
793
794 bit_size = s->f_code - 1;
795 range = 1 << bit_size;
796
797 val=mv;
798 if (val < 0)
799 val = -val;
800 val--;
801 code = (val >> bit_size) + 1;
802 if(code<17){
803 len= mbMotionVectorTable[code][1] + 1 + bit_size;
804 }else{
805 len= mbMotionVectorTable[16][1] + 2 + bit_size;
806 }
807 }
808
809 mv_penalty[f_code][mv+MAX_MV]= len;
810 }
811 }
812
813
814 for(f_code=MAX_FCODE; f_code>0; f_code--){
815 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
816 fcode_tab[mv+MAX_MV]= f_code;
817 }
818 }
819 }
1457ab52 820 s->me.mv_penalty= mv_penalty;
11ce8834 821 s->fcode_tab= fcode_tab;
029911d1
MN
822 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
823 s->min_qcoeff=-255;
824 s->max_qcoeff= 255;
825 }else{
826 s->min_qcoeff=-2047;
827 s->max_qcoeff= 2047;
828 }
c442d75c 829 s->intra_ac_vlc_length=
e5021fff
MN
830 s->inter_ac_vlc_length=
831 s->intra_ac_vlc_last_length=
832 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
11ce8834 833}
8f8402e4 834
de6d9b64
FB
835static inline void encode_dc(MpegEncContext *s, int diff, int component)
836{
de6d9b64 837 if (component == 0) {
f004ca1c
MN
838 put_bits(
839 &s->pb,
840 mpeg1_lum_dc_uni[diff+255]&0xFF,
841 mpeg1_lum_dc_uni[diff+255]>>8);
de6d9b64 842 } else {
f004ca1c
MN
843 put_bits(
844 &s->pb,
845 mpeg1_chr_dc_uni[diff+255]&0xFF,
846 mpeg1_chr_dc_uni[diff+255]>>8);
de6d9b64
FB
847 }
848}
849
850static void mpeg1_encode_block(MpegEncContext *s,
851 DCTELEM *block,
852 int n)
853{
854 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
855 int code, component;
80feb2a2 856// RLTable *rl = &rl_mpeg1;
de6d9b64
FB
857
858 last_index = s->block_last_index[n];
859
860 /* DC coef */
861 if (s->mb_intra) {
862 component = (n <= 3 ? 0 : n - 4 + 1);
863 dc = block[0]; /* overflow is impossible */
864 diff = dc - s->last_dc[component];
865 encode_dc(s, diff, component);
866 s->last_dc[component] = dc;
867 i = 1;
029911d1
MN
868/*
869 if (s->intra_vlc_format)
870 rl = &rl_mpeg2;
871 else
872 rl = &rl_mpeg1;
873*/
de6d9b64
FB
874 } else {
875 /* encode the first coefficient : needs to be done here because
876 it is handled slightly differently */
877 level = block[0];
878 if (abs(level) == 1) {
0c1a9eda 879 code = ((uint32_t)level >> 31); /* the sign bit */
de6d9b64
FB
880 put_bits(&s->pb, 2, code | 0x02);
881 i = 1;
882 } else {
883 i = 0;
884 last_non_zero = -1;
885 goto next_coef;
886 }
887 }
888
889 /* now quantify & encode AC coefs */
890 last_non_zero = i - 1;
80feb2a2 891
de6d9b64 892 for(;i<=last_index;i++) {
2ad1516a 893 j = s->intra_scantable.permutated[i];
de6d9b64
FB
894 level = block[j];
895 next_coef:
896#if 0
897 if (level != 0)
898 dprintf("level[%d]=%d\n", i, level);
899#endif
900 /* encode using VLC */
901 if (level != 0) {
902 run = i - last_non_zero - 1;
2ad1516a
MN
903
904 alevel= level;
905 MASK_ABS(sign, alevel)
906 sign&=1;
907
80feb2a2 908// code = get_rl_index(rl, 0, run, alevel);
c442d75c 909 if (alevel <= mpeg1_max_level[0][run]){
80feb2a2 910 code= mpeg1_index_run[0][run] + alevel - 1;
80feb2a2
MN
911 /* store the vlc & sign at once */
912 put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign);
de6d9b64 913 } else {
80feb2a2
MN
914 /* escape seems to be pretty rare <5% so i dont optimize it */
915 put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]);
de6d9b64
FB
916 /* escape: only clip in this case */
917 put_bits(&s->pb, 6, run);
029911d1
MN
918 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
919 if (alevel < 128) {
920 put_bits(&s->pb, 8, level & 0xff);
de6d9b64 921 } else {
029911d1
MN
922 if (level < 0) {
923 put_bits(&s->pb, 16, 0x8001 + level + 255);
924 } else {
925 put_bits(&s->pb, 16, level & 0xffff);
926 }
de6d9b64 927 }
029911d1
MN
928 }else{
929 put_bits(&s->pb, 12, level & 0xfff);
de6d9b64
FB
930 }
931 }
932 last_non_zero = i;
933 }
934 }
935 /* end of block */
936 put_bits(&s->pb, 2, 0x2);
937}
7604246d 938#endif //CONFIG_ENCODERS
de6d9b64
FB
939
940/******************************************/
941/* decoding */
942
943static VLC dc_lum_vlc;
944static VLC dc_chroma_vlc;
945static VLC mv_vlc;
946static VLC mbincr_vlc;
947static VLC mb_ptype_vlc;
948static VLC mb_btype_vlc;
949static VLC mb_pat_vlc;
950
c3bf0288 951static void init_vlcs()
de6d9b64
FB
952{
953 static int done = 0;
954
955 if (!done) {
915bbac6 956 done = 1;
de6d9b64 957
9ac7ecd6 958 init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
de6d9b64
FB
959 vlc_dc_lum_bits, 1, 1,
960 vlc_dc_lum_code, 2, 2);
9ac7ecd6 961 init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12,
de6d9b64
FB
962 vlc_dc_chroma_bits, 1, 1,
963 vlc_dc_chroma_code, 2, 2);
8ed2ddb2 964 init_vlc(&mv_vlc, MV_VLC_BITS, 17,
de6d9b64
FB
965 &mbMotionVectorTable[0][1], 2, 1,
966 &mbMotionVectorTable[0][0], 2, 1);
9c00c3af 967 init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36,
de6d9b64
FB
968 &mbAddrIncrTable[0][1], 2, 1,
969 &mbAddrIncrTable[0][0], 2, 1);
8ed2ddb2 970 init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 63,
de6d9b64
FB
971 &mbPatTable[0][1], 2, 1,
972 &mbPatTable[0][0], 2, 1);
973
7bc9090a 974 init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
de6d9b64
FB
975 &table_mb_ptype[0][1], 2, 1,
976 &table_mb_ptype[0][0], 2, 1);
7bc9090a 977 init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
de6d9b64
FB
978 &table_mb_btype[0][1], 2, 1,
979 &table_mb_btype[0][0], 2, 1);
980 init_rl(&rl_mpeg1);
981 init_rl(&rl_mpeg2);
4f68b084
MN
982
983 init_2d_vlc_rl(&rl_mpeg1);
984 init_2d_vlc_rl(&rl_mpeg2);
de6d9b64
FB
985 }
986}
987
988static inline int get_dmv(MpegEncContext *s)
989{
612476ef
A
990 if(get_bits1(&s->gb))
991 return 1 - (get_bits1(&s->gb) << 1);
de6d9b64
FB
992 else
993 return 0;
994}
995
0ee50938
FB
996static inline int get_qscale(MpegEncContext *s)
997{
7062fad6 998 int qscale = get_bits(&s->gb, 5);
029911d1 999 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
0ee50938 1000 if (s->q_scale_type) {
7062fad6 1001 return non_linear_qscale[qscale];
0ee50938 1002 } else {
7062fad6 1003 return qscale << 1;
0ee50938 1004 }
0ee50938
FB
1005 }
1006 return qscale;
1007}
1008
de6d9b64
FB
1009/* motion type (for mpeg2) */
1010#define MT_FIELD 1
1011#define MT_FRAME 2
1012#define MT_16X8 2
1013#define MT_DMV 3
1014
1015static int mpeg_decode_mb(MpegEncContext *s,
1016 DCTELEM block[6][64])
1017{
d2975f8d 1018 int i, j, k, cbp, val, mb_type, motion_type;
de6d9b64 1019
de6d9b64
FB
1020 dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
1021
f943e138
MN
1022 assert(s->mb_skiped==0);
1023
9b8709d1 1024 if (s->mb_skip_run-- != 0) {
7bc9090a 1025 if(s->pict_type == I_TYPE){
9b879566 1026 av_log(s->avctx, AV_LOG_ERROR, "skiped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
7bc9090a
MN
1027 return -1;
1028 }
1029
de6d9b64
FB
1030 /* skip mb */
1031 s->mb_intra = 0;
1032 for(i=0;i<6;i++)
1033 s->block_last_index[i] = -1;
1034 s->mv_type = MV_TYPE_16X16;
1035 if (s->pict_type == P_TYPE) {
1036 /* if P type, zero motion vector is implied */
1037 s->mv_dir = MV_DIR_FORWARD;
1038 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1039 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
25ed7f92 1040 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
f943e138 1041 s->mb_skiped = 1;
7bc9090a 1042 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
de6d9b64
FB
1043 } else {
1044 /* if B type, reuse previous vectors and directions */
1045 s->mv[0][0][0] = s->last_mv[0][0][0];
1046 s->mv[0][0][1] = s->last_mv[0][0][1];
1047 s->mv[1][0][0] = s->last_mv[1][0][0];
1048 s->mv[1][0][1] = s->last_mv[1][0][1];
f943e138 1049
7bc9090a
MN
1050 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
1051 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1] | MB_TYPE_SKIP;
1052// assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
1053
f943e138
MN
1054 if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
1055 s->mb_skiped = 1;
de6d9b64 1056 }
59b571c1 1057
de6d9b64
FB
1058 return 0;
1059 }
1060
1061 switch(s->pict_type) {
1062 default:
1063 case I_TYPE:
612476ef 1064 if (get_bits1(&s->gb) == 0) {
e94bc100 1065 if (get_bits1(&s->gb) == 0){
9b879566 1066 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
de6d9b64 1067 return -1;
e94bc100 1068 }
7bc9090a 1069 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
de6d9b64 1070 } else {
7bc9090a 1071 mb_type = MB_TYPE_INTRA;
de6d9b64
FB
1072 }
1073 break;
1074 case P_TYPE:
8ed2ddb2 1075 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
9ac7ecd6 1076 if (mb_type < 0){
9b879566 1077 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
de6d9b64 1078 return -1;
9ac7ecd6 1079 }
7bc9090a 1080 mb_type = ptype2mb_type[ mb_type ];
de6d9b64
FB
1081 break;
1082 case B_TYPE:
8ed2ddb2 1083 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
9ac7ecd6 1084 if (mb_type < 0){
9b879566 1085 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
de6d9b64 1086 return -1;
9ac7ecd6 1087 }
7bc9090a 1088 mb_type = btype2mb_type[ mb_type ];
de6d9b64
FB
1089 break;
1090 }
1091 dprintf("mb_type=%x\n", mb_type);
7bc9090a
MN
1092// motion_type = 0; /* avoid warning */
1093 if (IS_INTRA(mb_type)) {
1094 /* compute dct type */
1095 if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1096 !s->frame_pred_frame_dct) {
1097 s->interlaced_dct = get_bits1(&s->gb);
1098 }
de6d9b64 1099
7bc9090a
MN
1100 if (IS_QUANT(mb_type))
1101 s->qscale = get_qscale(s);
1102
de6d9b64
FB
1103 if (s->concealment_motion_vectors) {
1104 /* just parse them */
1105 if (s->picture_structure != PICT_FRAME)
612476ef 1106 skip_bits1(&s->gb); /* field select */
daab3296
MN
1107
1108 s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
1109 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
1110 s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
1111 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
1112
7bc9090a 1113 skip_bits1(&s->gb); /* marker */
daab3296
MN
1114 }else
1115 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
de6d9b64 1116 s->mb_intra = 1;
a579db0c
IK
1117#ifdef HAVE_XVMC
1118 //one 1 we memcpy blocks in xvmcvideo
1119 if(s->avctx->xvmc_acceleration > 1){
1120 XVMC_pack_pblocks(s,-1);//inter are always full blocks
1121 if(s->swap_uv){
1122 exchange_uv(s);
1123 }
1124 }
1125#endif
7bc9090a 1126
029911d1 1127 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
7bc9090a 1128 for(i=0;i<6;i++) {
a579db0c 1129 if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
7bc9090a
MN
1130 return -1;
1131 }
1132 } else {
1133 for(i=0;i<6;i++) {
a579db0c 1134 if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
7bc9090a
MN
1135 return -1;
1136 }
1137 }
de6d9b64 1138 } else {
7bc9090a 1139 if (mb_type & MB_TYPE_ZERO_MV){
b40cd4e0 1140 assert(mb_type & MB_TYPE_CBP);
7bc9090a
MN
1141
1142 /* compute dct type */
1143 if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1144 !s->frame_pred_frame_dct) {
1145 s->interlaced_dct = get_bits1(&s->gb);
1146 }
1147
1148 if (IS_QUANT(mb_type))
1149 s->qscale = get_qscale(s);
1150
1151 s->mv_dir = MV_DIR_FORWARD;
1152 s->mv_type = MV_TYPE_16X16;
1153 s->last_mv[0][0][0] = 0;
1154 s->last_mv[0][0][1] = 0;
1155 s->last_mv[0][1][0] = 0;
1156 s->last_mv[0][1][1] = 0;
1157 s->mv[0][0][0] = 0;
1158 s->mv[0][0][1] = 0;
1159 }else{
1160 assert(mb_type & MB_TYPE_L0L1);
1161//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
1162 /* get additionnal motion vector type */
1163 if (s->frame_pred_frame_dct)
1164 motion_type = MT_FRAME;
1165 else{
1166 motion_type = get_bits(&s->gb, 2);
1167 }
1168
1169 /* compute dct type */
1170 if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
b40cd4e0 1171 !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
7bc9090a
MN
1172 s->interlaced_dct = get_bits1(&s->gb);
1173 }
1174
1175 if (IS_QUANT(mb_type))
1176 s->qscale = get_qscale(s);
1177
1178 /* motion vectors */
1179 s->mv_dir = 0;
1180 for(i=0;i<2;i++) {
1181 if (USES_LIST(mb_type, i)) {
1182 s->mv_dir |= (MV_DIR_FORWARD >> i);
1183 dprintf("motion_type=%d\n", motion_type);
1184 switch(motion_type) {
1185 case MT_FRAME: /* or MT_16X8 */
1186 if (s->picture_structure == PICT_FRAME) {
1187 /* MT_FRAME */
1188 mb_type |= MB_TYPE_16x16;
1189 s->mv_type = MV_TYPE_16X16;
1190 s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
1191 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
1192 s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
1193 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
de6d9b64 1194 /* full_pel: only for mpeg1 */
7bc9090a
MN
1195 if (s->full_pel[i]){
1196 s->mv[i][0][0] <<= 1;
1197 s->mv[i][0][1] <<= 1;
1198 }
1199 } else {
1200 /* MT_16X8 */
26f548a7 1201 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
7bc9090a
MN
1202 s->mv_type = MV_TYPE_16X8;
1203 for(j=0;j<2;j++) {
1204 s->field_select[i][j] = get_bits1(&s->gb);
1205 for(k=0;k<2;k++) {
1206 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1207 s->last_mv[i][j][k]);
1208 s->last_mv[i][j][k] = val;
1209 s->mv[i][j][k] = val;
1210 }
1211 }
de6d9b64 1212 }
7bc9090a
MN
1213 break;
1214 case MT_FIELD:
1215 s->mv_type = MV_TYPE_FIELD;
1216 if (s->picture_structure == PICT_FRAME) {
1217 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1218 for(j=0;j<2;j++) {
1219 s->field_select[i][j] = get_bits1(&s->gb);
1220 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1221 s->last_mv[i][j][0]);
1222 s->last_mv[i][j][0] = val;
1223 s->mv[i][j][0] = val;
1224 dprintf("fmx=%d\n", val);
1225 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1226 s->last_mv[i][j][1] >> 1);
1227 s->last_mv[i][j][1] = val << 1;
1228 s->mv[i][j][1] = val;
1229 dprintf("fmy=%d\n", val);
1230 }
1231 } else {
26f548a7 1232 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
7bc9090a 1233 s->field_select[i][0] = get_bits1(&s->gb);
de6d9b64
FB
1234 for(k=0;k<2;k++) {
1235 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
7bc9090a
MN
1236 s->last_mv[i][0][k]);
1237 s->last_mv[i][0][k] = val;
1238 s->last_mv[i][1][k] = val;
1239 s->mv[i][0][k] = val;
de6d9b64
FB
1240 }
1241 }
7bc9090a
MN
1242 break;
1243 case MT_DMV:
1244 {
1245 int dmx, dmy, mx, my, m;
1246
1247 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1248 s->last_mv[i][0][0]);
1249 s->last_mv[i][0][0] = mx;
1250 s->last_mv[i][1][0] = mx;
1251 dmx = get_dmv(s);
1252 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1253 s->last_mv[i][0][1] >> 1);
1254 dmy = get_dmv(s);
1255 s->mv_type = MV_TYPE_DMV;
1dff7d56
IK
1256
1257
1258 s->last_mv[i][0][1] = my<<1;
1259 s->last_mv[i][1][1] = my<<1;
1260
1261 s->mv[i][0][0] = mx;
1262 s->mv[i][0][1] = my;
1263 s->mv[i][1][0] = mx;//not used
1264 s->mv[i][1][1] = my;//not used
1265
7bc9090a
MN
1266 if (s->picture_structure == PICT_FRAME) {
1267 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1268
1dff7d56 1269 //m = 1 + 2 * s->top_field_first;
7bc9090a 1270 m = s->top_field_first ? 1 : 3;
1dff7d56 1271
7bc9090a 1272 /* top -> top pred */
1dff7d56
IK
1273 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1274 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
7bc9090a 1275 m = 4 - m;
7bc9090a
MN
1276 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1277 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1278 } else {
1279 mb_type |= MB_TYPE_16x16;
1280
1dff7d56
IK
1281 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1282 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1283 if(s->picture_structure == PICT_TOP_FIELD)
1284 s->mv[i][2][1]--;
1285 else
1286 s->mv[i][2][1]++;
7bc9090a 1287 }
de6d9b64 1288 }
7bc9090a 1289 break;
26f548a7
MN
1290 default:
1291 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1292 return -1;
de6d9b64 1293 }
de6d9b64
FB
1294 }
1295 }
1296 }
7bc9090a
MN
1297
1298 s->mb_intra = 0;
de6d9b64 1299
b40cd4e0 1300 if (HAS_CBP(mb_type)) {
7bc9090a
MN
1301 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1302 if (cbp < 0){
9b879566 1303 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
7bc9090a 1304 return -1;
de6d9b64 1305 }
7bc9090a
MN
1306 cbp++;
1307
a579db0c
IK
1308#ifdef HAVE_XVMC
1309 //on 1 we memcpy blocks in xvmcvideo
1310 if(s->avctx->xvmc_acceleration > 1){
1311 XVMC_pack_pblocks(s,cbp);
1312 if(s->swap_uv){
1313 exchange_uv(s);
1314 }
1315 }
1316#endif
1317
029911d1 1318 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
7bc9090a
MN
1319 for(i=0;i<6;i++) {
1320 if (cbp & 32) {
a579db0c 1321 if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
7bc9090a
MN
1322 return -1;
1323 } else {
1324 s->block_last_index[i] = -1;
1325 }
1326 cbp+=cbp;
de6d9b64 1327 }
7bc9090a
MN
1328 } else {
1329 for(i=0;i<6;i++) {
1330 if (cbp & 32) {
a579db0c 1331 if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
7bc9090a
MN
1332 return -1;
1333 } else {
1334 s->block_last_index[i] = -1;
1335 }
1336 cbp+=cbp;
a0201736 1337 }
de6d9b64 1338 }
7bc9090a
MN
1339 }else{
1340 for(i=0;i<6;i++)
1341 s->block_last_index[i] = -1;
de6d9b64
FB
1342 }
1343 }
7bc9090a
MN
1344
1345 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
1346
de6d9b64
FB
1347 return 0;
1348}
1349
1350/* as h263, but only 17 codes */
1351static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
1352{
05858889 1353 int code, sign, val, l, shift;
de6d9b64 1354
8ed2ddb2 1355 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
de6d9b64
FB
1356 if (code == 0) {
1357 return pred;
1358 }
7bc9090a
MN
1359 if (code < 0) {
1360 return 0xffff;
1361 }
1362
612476ef 1363 sign = get_bits1(&s->gb);
de6d9b64 1364 shift = fcode - 1;
05858889
B
1365 val = code;
1366 if (shift) {
1367 val = (val - 1) << shift;
de6d9b64 1368 val |= get_bits(&s->gb, shift);
05858889
B
1369 val++;
1370 }
de6d9b64
FB
1371 if (sign)
1372 val = -val;
1373 val += pred;
1374
1375 /* modulo decoding */
7bc9090a 1376 l = 1 << (shift+4);
05858889 1377 val = ((val + l)&(l*2-1)) - l;
de6d9b64
FB
1378 return val;
1379}
1380
c3bf0288 1381static inline int decode_dc(GetBitContext *gb, int component)
de6d9b64
FB
1382{
1383 int code, diff;
1384
1385 if (component == 0) {
c3bf0288 1386 code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
de6d9b64 1387 } else {
c3bf0288 1388 code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
de6d9b64 1389 }
9ac7ecd6 1390 if (code < 0){
9b879566 1391 av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
de6d9b64 1392 return 0xffff;
9ac7ecd6 1393 }
de6d9b64
FB
1394 if (code == 0) {
1395 diff = 0;
1396 } else {
c3bf0288 1397 diff = get_xbits(gb, code);
de6d9b64
FB
1398 }
1399 return diff;
1400}
1401
a0201736 1402static inline int mpeg1_decode_block_intra(MpegEncContext *s,
de6d9b64
FB
1403 DCTELEM *block,
1404 int n)
1405{
1406 int level, dc, diff, i, j, run;
a0201736 1407 int component;
de6d9b64 1408 RLTable *rl = &rl_mpeg1;
0c1a9eda
ZK
1409 uint8_t * const scantable= s->intra_scantable.permutated;
1410 const uint16_t *quant_matrix= s->intra_matrix;
a0201736 1411 const int qscale= s->qscale;
de6d9b64 1412
a0201736
MN
1413 /* DC coef */
1414 component = (n <= 3 ? 0 : n - 4 + 1);
c3bf0288 1415 diff = decode_dc(&s->gb, component);
a0201736
MN
1416 if (diff >= 0xffff)
1417 return -1;
1418 dc = s->last_dc[component];
1419 dc += diff;
1420 s->last_dc[component] = dc;
1421 block[0] = dc<<3;
1422 dprintf("dc=%d diff=%d\n", dc, diff);
1423 i = 0;
1424 {
1425 OPEN_READER(re, &s->gb);
1426 /* now quantify & encode AC coefs */
1427 for(;;) {
1428 UPDATE_CACHE(re, &s->gb);
1429 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1430
1431 if(level == 127){
1432 break;
1433 } else if(level != 0) {
1434 i += run;
1435 j = scantable[i];
1436 level= (level*qscale*quant_matrix[j])>>3;
1437 level= (level-1)|1;
1438 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1439 LAST_SKIP_BITS(re, &s->gb, 1);
1440 } else {
1441 /* escape */
1442 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1443 UPDATE_CACHE(re, &s->gb);
1444 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1445 if (level == -128) {
1446 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1447 } else if (level == 0) {
1448 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
1449 }
1450 i += run;
1451 j = scantable[i];
1452 if(level<0){
1453 level= -level;
1454 level= (level*qscale*quant_matrix[j])>>3;
1455 level= (level-1)|1;
1456 level= -level;
1457 }else{
1458 level= (level*qscale*quant_matrix[j])>>3;
1459 level= (level-1)|1;
1460 }
1461 }
1462 if (i > 63){
9b879566 1463 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
a0201736
MN
1464 return -1;
1465 }
1466
1467 block[j] = level;
1468 }
1469 CLOSE_READER(re, &s->gb);
1470 }
1471 s->block_last_index[n] = i;
1472 return 0;
1473}
1474
1475static inline int mpeg1_decode_block_inter(MpegEncContext *s,
1476 DCTELEM *block,
1477 int n)
1478{
1479 int level, i, j, run;
1480 RLTable *rl = &rl_mpeg1;
0c1a9eda
ZK
1481 uint8_t * const scantable= s->intra_scantable.permutated;
1482 const uint16_t *quant_matrix= s->inter_matrix;
a0201736
MN
1483 const int qscale= s->qscale;
1484
1485 {
8db1a1dd
MN
1486 int v;
1487 OPEN_READER(re, &s->gb);
a0201736 1488 i = -1;
de6d9b64 1489 /* special case for the first coef. no need to add a second vlc table */
8db1a1dd
MN
1490 UPDATE_CACHE(re, &s->gb);
1491 v= SHOW_UBITS(re, &s->gb, 2);
de6d9b64 1492 if (v & 2) {
3729c912 1493 LAST_SKIP_BITS(re, &s->gb, 2);
a0201736
MN
1494 level= (3*qscale*quant_matrix[0])>>4;
1495 level= (level-1)|1;
1496 if(v&1)
1497 level= -level;
3729c912 1498 block[0] = level;
a0201736 1499 i++;
de6d9b64 1500 }
3729c912 1501
a0201736
MN
1502 /* now quantify & encode AC coefs */
1503 for(;;) {
1504 UPDATE_CACHE(re, &s->gb);
1505 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1506
1507 if(level == 127){
1508 break;
1509 } else if(level != 0) {
1510 i += run;
1511 j = scantable[i];
1512 level= ((level*2+1)*qscale*quant_matrix[j])>>4;
1513 level= (level-1)|1;
1514 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1515 LAST_SKIP_BITS(re, &s->gb, 1);
1516 } else {
1517 /* escape */
1518 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1519 UPDATE_CACHE(re, &s->gb);
1520 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1521 if (level == -128) {
1522 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1523 } else if (level == 0) {
1524 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
1525 }
1526 i += run;
1527 j = scantable[i];
1528 if(level<0){
1529 level= -level;
1530 level= ((level*2+1)*qscale*quant_matrix[j])>>4;
1531 level= (level-1)|1;
1532 level= -level;
1533 }else{
1534 level= ((level*2+1)*qscale*quant_matrix[j])>>4;
1535 level= (level-1)|1;
1536 }
de6d9b64 1537 }
a0201736 1538 if (i > 63){
9b879566 1539 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
a0201736
MN
1540 return -1;
1541 }
1542
1543 block[j] = level;
de6d9b64 1544 }
a0201736 1545 CLOSE_READER(re, &s->gb);
de6d9b64 1546 }
a0201736 1547 s->block_last_index[n] = i;
de6d9b64
FB
1548 return 0;
1549}
1550
1551/* Also does unquantization here, since I will never support mpeg2
1552 encoding */
3729c912
MN
1553static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
1554 DCTELEM *block,
1555 int n)
de6d9b64
FB
1556{
1557 int level, i, j, run;
de6d9b64 1558 RLTable *rl = &rl_mpeg1;
0c1a9eda
ZK
1559 uint8_t * const scantable= s->intra_scantable.permutated;
1560 const uint16_t *quant_matrix;
3729c912 1561 const int qscale= s->qscale;
de6d9b64
FB
1562 int mismatch;
1563
de6d9b64
FB
1564 mismatch = 1;
1565
1566 {
8db1a1dd
MN
1567 int v;
1568 OPEN_READER(re, &s->gb);
3729c912 1569 i = -1;
8db1a1dd 1570 if (n < 4)
3729c912 1571 quant_matrix = s->inter_matrix;
de6d9b64 1572 else
3729c912 1573 quant_matrix = s->chroma_inter_matrix;
8db1a1dd 1574
de6d9b64 1575 /* special case for the first coef. no need to add a second vlc table */
8db1a1dd
MN
1576 UPDATE_CACHE(re, &s->gb);
1577 v= SHOW_UBITS(re, &s->gb, 2);
de6d9b64 1578 if (v & 2) {
3729c912
MN
1579 LAST_SKIP_BITS(re, &s->gb, 2);
1580 level= (3*qscale*quant_matrix[0])>>5;
1581 if(v&1)
1582 level= -level;
1583 block[0] = level;
1584 mismatch ^= level;
1585 i++;
de6d9b64 1586 }
de6d9b64 1587
3729c912
MN
1588 /* now quantify & encode AC coefs */
1589 for(;;) {
1590 UPDATE_CACHE(re, &s->gb);
1591 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1592
1593 if(level == 127){
1594 break;
1595 } else if(level != 0) {
1596 i += run;
1597 j = scantable[i];
1598 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1599 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1600 LAST_SKIP_BITS(re, &s->gb, 1);
1601 } else {
1602 /* escape */
1603 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1604 UPDATE_CACHE(re, &s->gb);
1605 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1606
1607 i += run;
1608 j = scantable[i];
1609 if(level<0){
1610 level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
1611 level= -level;
1612 }else{
1613 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1614 }
1615 }
1616 if (i > 63){
9b879566 1617 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
3729c912
MN
1618 return -1;
1619 }
1620
1621 mismatch ^= level;
1622 block[j] = level;
25ed7f92 1623 }
3729c912 1624 CLOSE_READER(re, &s->gb);
de6d9b64
FB
1625 }
1626 block[63] ^= (mismatch & 1);
3729c912 1627
de6d9b64
FB
1628 s->block_last_index[n] = i;
1629 return 0;
1630}
1631
3729c912
MN
1632static inline int mpeg2_decode_block_intra(MpegEncContext *s,
1633 DCTELEM *block,
1634 int n)
de6d9b64
FB
1635{
1636 int level, dc, diff, i, j, run;
3729c912 1637 int component;
de6d9b64 1638 RLTable *rl;
0c1a9eda
ZK
1639 uint8_t * const scantable= s->intra_scantable.permutated;
1640 const uint16_t *quant_matrix;
3729c912 1641 const int qscale= s->qscale;
de6d9b64
FB
1642 int mismatch;
1643
de6d9b64 1644 /* DC coef */
3729c912
MN
1645 if (n < 4){
1646 quant_matrix = s->intra_matrix;
1647 component = 0;
1648 }else{
1649 quant_matrix = s->chroma_intra_matrix;
1650 component = n - 3;
1651 }
c3bf0288 1652 diff = decode_dc(&s->gb, component);
de6d9b64
FB
1653 if (diff >= 0xffff)
1654 return -1;
1655 dc = s->last_dc[component];
1656 dc += diff;
1657 s->last_dc[component] = dc;
1658 block[0] = dc << (3 - s->intra_dc_precision);
1659 dprintf("dc=%d\n", block[0]);
d753173a 1660 mismatch = block[0] ^ 1;
3729c912 1661 i = 0;
de6d9b64
FB
1662 if (s->intra_vlc_format)
1663 rl = &rl_mpeg2;
1664 else
1665 rl = &rl_mpeg1;
25ed7f92 1666
3729c912
MN
1667 {
1668 OPEN_READER(re, &s->gb);
1669 /* now quantify & encode AC coefs */
1670 for(;;) {
1671 UPDATE_CACHE(re, &s->gb);
1672 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1673
1674 if(level == 127){
1675 break;
1676 } else if(level != 0) {
1677 i += run;
1678 j = scantable[i];
1679 level= (level*qscale*quant_matrix[j])>>4;
1680 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1681 LAST_SKIP_BITS(re, &s->gb, 1);
1682 } else {
1683 /* escape */
1684 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1685 UPDATE_CACHE(re, &s->gb);
1686 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1687 i += run;
1688 j = scantable[i];
1689 if(level<0){
1690 level= (-level*qscale*quant_matrix[j])>>4;
1691 level= -level;
1692 }else{
1693 level= (level*qscale*quant_matrix[j])>>4;
1694 }
1695 }
1696 if (i > 63){
9b879566 1697 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
3729c912
MN
1698 return -1;
1699 }
1700
1701 mismatch^= level;
1702 block[j] = level;
9ac7ecd6 1703 }
3729c912 1704 CLOSE_READER(re, &s->gb);
de6d9b64 1705 }
3729c912
MN
1706 block[63]^= mismatch&1;
1707
de6d9b64
FB
1708 s->block_last_index[n] = i;
1709 return 0;
1710}
1711
de6d9b64
FB
1712typedef struct Mpeg1Context {
1713 MpegEncContext mpeg_enc_ctx;
de6d9b64 1714 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1cb0edb4 1715 int repeat_field; /* true if we must repeat the field */
fa384dcc 1716 AVPanScan pan_scan; /** some temporary storage for the panscan */
de6d9b64
FB
1717} Mpeg1Context;
1718
1719static int mpeg_decode_init(AVCodecContext *avctx)
1720{
1721 Mpeg1Context *s = avctx->priv_data;
8f8402e4 1722
94aec31f 1723 s->mpeg_enc_ctx.flags= avctx->flags;
8f8402e4 1724 common_init(&s->mpeg_enc_ctx);
c3bf0288 1725 init_vlcs();
de6d9b64 1726
de6d9b64 1727 s->mpeg_enc_ctx_allocated = 0;
de6d9b64 1728 s->mpeg_enc_ctx.picture_number = 0;
1cb0edb4 1729 s->repeat_field = 0;
d7e9533a 1730 s->mpeg_enc_ctx.codec_id= avctx->codec->id;
de6d9b64
FB
1731 return 0;
1732}
1733
1734/* return the 8 bit start code value and update the search
1735 state. Return -1 if no start code found */
80097bbf 1736static int find_start_code(uint8_t **pbuf_ptr, uint8_t *buf_end)
de6d9b64 1737{
0c1a9eda 1738 uint8_t *buf_ptr;
80097bbf 1739 unsigned int state=0xFFFFFFFF, v;
de6d9b64
FB
1740 int val;
1741
de6d9b64
FB
1742 buf_ptr = *pbuf_ptr;
1743 while (buf_ptr < buf_end) {
1744 v = *buf_ptr++;
1745 if (state == 0x000001) {
1746 state = ((state << 8) | v) & 0xffffff;
1747 val = state;
1748 goto found;
1749 }
1750 state = ((state << 8) | v) & 0xffffff;
1751 }
1752 val = -1;
1753 found:
1754 *pbuf_ptr = buf_ptr;
de6d9b64
FB
1755 return val;
1756}
1757
1758static int mpeg1_decode_picture(AVCodecContext *avctx,
0c1a9eda 1759 uint8_t *buf, int buf_size)
de6d9b64
FB
1760{
1761 Mpeg1Context *s1 = avctx->priv_data;
1762 MpegEncContext *s = &s1->mpeg_enc_ctx;
d60a8f85 1763 int ref, f_code, vbv_delay;
de6d9b64 1764
68f593b4 1765 init_get_bits(&s->gb, buf, buf_size*8);
de6d9b64
FB
1766
1767 ref = get_bits(&s->gb, 10); /* temporal ref */
1768 s->pict_type = get_bits(&s->gb, 3);
b9ecd1ee 1769 dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
58bfafbe 1770
d60a8f85 1771 vbv_delay= get_bits(&s->gb, 16);
de6d9b64 1772 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
612476ef 1773 s->full_pel[0] = get_bits1(&s->gb);
de6d9b64
FB
1774 f_code = get_bits(&s->gb, 3);
1775 if (f_code == 0)
1776 return -1;
1777 s->mpeg_f_code[0][0] = f_code;
1778 s->mpeg_f_code[0][1] = f_code;
1779 }
1780 if (s->pict_type == B_TYPE) {
612476ef 1781 s->full_pel[1] = get_bits1(&s->gb);
de6d9b64
FB
1782 f_code = get_bits(&s->gb, 3);
1783 if (f_code == 0)
1784 return -1;
1785 s->mpeg_f_code[1][0] = f_code;
1786 s->mpeg_f_code[1][1] = f_code;
1787 }
1e491e29
MN
1788 s->current_picture.pict_type= s->pict_type;
1789 s->current_picture.key_frame= s->pict_type == I_TYPE;
d9cb5429 1790
de6d9b64
FB
1791 s->y_dc_scale = 8;
1792 s->c_dc_scale = 8;
1793 s->first_slice = 1;
1794 return 0;
1795}
1796
1797static void mpeg_decode_sequence_extension(MpegEncContext *s)
1798{
1799 int horiz_size_ext, vert_size_ext;
945f15b7 1800 int bit_rate_ext, vbv_buf_ext;
de6d9b64 1801 int frame_rate_ext_n, frame_rate_ext_d;
029911d1 1802 int level, profile;
de6d9b64 1803
029911d1
MN
1804 skip_bits(&s->gb, 1); /* profil and level esc*/
1805 profile= get_bits(&s->gb, 3);
1806 level= get_bits(&s->gb, 4);
1cb0edb4 1807 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
612476ef 1808 skip_bits(&s->gb, 2); /* chroma_format */
de6d9b64
FB
1809 horiz_size_ext = get_bits(&s->gb, 2);
1810 vert_size_ext = get_bits(&s->gb, 2);
1811 s->width |= (horiz_size_ext << 12);
1812 s->height |= (vert_size_ext << 12);
1813 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1814 s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
612476ef 1815 skip_bits1(&s->gb); /* marker */
de6d9b64 1816 vbv_buf_ext = get_bits(&s->gb, 8);
e0560448 1817
945f15b7 1818 s->low_delay = get_bits1(&s->gb);
e0560448
MN
1819 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
1820
de6d9b64
FB
1821 frame_rate_ext_n = get_bits(&s->gb, 2);
1822 frame_rate_ext_d = get_bits(&s->gb, 5);
14bea432
MN
1823 av_reduce(
1824 &s->avctx->frame_rate,
1825 &s->avctx->frame_rate_base,
1826 frame_rate_tab[s->frame_rate_index] * (frame_rate_ext_n+1),
1827 MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d+1),
1828 1<<30);
1829
de6d9b64 1830 dprintf("sequence extension\n");
029911d1 1831 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
fb4a4a56 1832 s->avctx->sub_id = 2; /* indicates mpeg2 found */
945f15b7 1833
5ff85f1d
MN
1834 if(s->aspect_ratio_info <= 1)
1835 s->avctx->sample_aspect_ratio= mpeg2_aspect[s->aspect_ratio_info];
1836 else{
1837 s->avctx->sample_aspect_ratio=
1838 av_div_q(
1839 mpeg2_aspect[s->aspect_ratio_info],
1840 (AVRational){s->width, s->height}
1841 );
1842 }
029911d1
MN
1843
1844 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
9b879566 1845 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d \n", profile, level);
de6d9b64
FB
1846}
1847
fa384dcc
MN
1848static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1849{
1850 MpegEncContext *s= &s1->mpeg_enc_ctx;
1851 int color_description, w, h;
1852
1853 skip_bits(&s->gb, 3); /* video format */
1854 color_description= get_bits1(&s->gb);
1855 if(color_description){
1856 skip_bits(&s->gb, 8); /* color primaries */
1857 skip_bits(&s->gb, 8); /* transfer_characteristics */
1858 skip_bits(&s->gb, 8); /* matrix_coefficients */
1859 }
1860 w= get_bits(&s->gb, 14);
1861 skip_bits(&s->gb, 1); //marker
1862 h= get_bits(&s->gb, 14);
1863 skip_bits(&s->gb, 1); //marker
1864
1865 s1->pan_scan.width= 16*w;
1866 s1->pan_scan.height=16*h;
1867
5ff85f1d
MN
1868 if(s->aspect_ratio_info > 1)
1869 s->avctx->sample_aspect_ratio=
1870 av_div_q(
1871 mpeg2_aspect[s->aspect_ratio_info],
1872 (AVRational){w, h}
1873 );
fa384dcc
MN
1874
1875 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
9b879566 1876 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
fa384dcc
MN
1877}
1878
1879static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1880{
1881 MpegEncContext *s= &s1->mpeg_enc_ctx;
1882 int i;
1883
1884 for(i=0; i<1; i++){ //FIXME count
1885 s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
1886 skip_bits(&s->gb, 1); //marker
1887 s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
1888 skip_bits(&s->gb, 1); //marker
1889 }
1890
1891 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
9b879566 1892 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
fa384dcc
MN
1893 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1894 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1895 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
1896 );
1897}
1898
de6d9b64
FB
1899static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1900{
60832448 1901 int i, v, j;
de6d9b64 1902
25ed7f92
FB
1903 dprintf("matrix extension\n");
1904
612476ef 1905 if (get_bits1(&s->gb)) {
de6d9b64
FB
1906 for(i=0;i<64;i++) {
1907 v = get_bits(&s->gb, 8);
b0368839 1908 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
60832448
FB
1909 s->intra_matrix[j] = v;
1910 s->chroma_intra_matrix[j] = v;
de6d9b64
FB
1911 }
1912 }
612476ef 1913 if (get_bits1(&s->gb)) {
de6d9b64
FB
1914 for(i=0;i<64;i++) {
1915 v = get_bits(&s->gb, 8);
b0368839 1916 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
d7e9533a
MN
1917 s->inter_matrix[j] = v;
1918 s->chroma_inter_matrix[j] = v;
de6d9b64
FB
1919 }
1920 }
612476ef 1921 if (get_bits1(&s->gb)) {
de6d9b64
FB
1922 for(i=0;i<64;i++) {
1923 v = get_bits(&s->gb, 8);
b0368839 1924 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
60832448 1925 s->chroma_intra_matrix[j] = v;
de6d9b64
FB
1926 }
1927 }
612476ef 1928 if (get_bits1(&s->gb)) {
de6d9b64
FB
1929 for(i=0;i<64;i++) {
1930 v = get_bits(&s->gb, 8);
b0368839 1931 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
d7e9533a 1932 s->chroma_inter_matrix[j] = v;
de6d9b64
FB
1933 }
1934 }
1935}
1936
1937static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
1938{
1939 s->full_pel[0] = s->full_pel[1] = 0;
1940 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1941 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1942 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1943 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1944 s->intra_dc_precision = get_bits(&s->gb, 2);
1945 s->picture_structure = get_bits(&s->gb, 2);
612476ef
A
1946 s->top_field_first = get_bits1(&s->gb);
1947 s->frame_pred_frame_dct = get_bits1(&s->gb);
1948 s->concealment_motion_vectors = get_bits1(&s->gb);
1949 s->q_scale_type = get_bits1(&s->gb);
1950 s->intra_vlc_format = get_bits1(&s->gb);
1951 s->alternate_scan = get_bits1(&s->gb);
1952 s->repeat_first_field = get_bits1(&s->gb);
1953 s->chroma_420_type = get_bits1(&s->gb);
1954 s->progressive_frame = get_bits1(&s->gb);
acf44abb 1955
dfb476cb
MN
1956 if(s->picture_structure == PICT_FRAME)
1957 s->first_field=0;
1958 else{
1959 s->first_field ^= 1;
7bc9090a 1960 memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
dfb476cb
MN
1961 }
1962
acf44abb 1963 if(s->alternate_scan){
3d2e8cce
MN
1964 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
1965 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
1966 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
1967 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
acf44abb 1968 }else{
3d2e8cce
MN
1969 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
1970 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
1971 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
1972 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
acf44abb
MN
1973 }
1974
de6d9b64 1975 /* composite display not parsed */
1cb0edb4 1976 dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
25ed7f92 1977 dprintf("picture_structure=%d\n", s->picture_structure);
e0a3d744
J
1978 dprintf("top field first=%d\n", s->top_field_first);
1979 dprintf("repeat first field=%d\n", s->repeat_first_field);
de6d9b64 1980 dprintf("conceal=%d\n", s->concealment_motion_vectors);
25ed7f92
FB
1981 dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
1982 dprintf("alternate_scan=%d\n", s->alternate_scan);
de6d9b64 1983 dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1cb0edb4 1984 dprintf("progressive_frame=%d\n", s->progressive_frame);
de6d9b64
FB
1985}
1986
1987static void mpeg_decode_extension(AVCodecContext *avctx,
0c1a9eda 1988 uint8_t *buf, int buf_size)
de6d9b64
FB
1989{
1990 Mpeg1Context *s1 = avctx->priv_data;
1991 MpegEncContext *s = &s1->mpeg_enc_ctx;
1992 int ext_type;
1993
68f593b4 1994 init_get_bits(&s->gb, buf, buf_size*8);
de6d9b64
FB
1995
1996 ext_type = get_bits(&s->gb, 4);
1997 switch(ext_type) {
1998 case 0x1:
de6d9b64
FB
1999 mpeg_decode_sequence_extension(s);
2000 break;
fa384dcc
MN
2001 case 0x2:
2002 mpeg_decode_sequence_display_extension(s1);
2003 break;
de6d9b64 2004 case 0x3:
de6d9b64
FB
2005 mpeg_decode_quant_matrix_extension(s);
2006 break;
fa384dcc
MN
2007 case 0x7:
2008 mpeg_decode_picture_display_extension(s1);
2009 break;
de6d9b64 2010 case 0x8:
de6d9b64
FB
2011 mpeg_decode_picture_coding_extension(s);
2012 break;
2013 }
2014}
2015
a579db0c
IK
2016static void exchange_uv(MpegEncContext *s){
2017short * tmp;
2018
2019 tmp = s->pblocks[4];
2020 s->pblocks[4] = s->pblocks[5];
2021 s->pblocks[5] = tmp;
ff862be5
MN
2022}
2023
86f85dca
MN
2024#define DECODE_SLICE_FATAL_ERROR -2
2025#define DECODE_SLICE_ERROR -1
2026#define DECODE_SLICE_OK 0
86f85dca
MN
2027
2028/**
2029 * decodes a slice.
2030 * @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br>
2031 * DECODE_SLICE_ERROR if the slice is damaged<br>
2032 * DECODE_SLICE_OK if this slice is ok<br>
86f85dca 2033 */
de6d9b64 2034static int mpeg_decode_slice(AVCodecContext *avctx,
492cd3a9 2035 AVFrame *pict,
de6d9b64 2036 int start_code,
80097bbf 2037 uint8_t **buf, int buf_size)
de6d9b64
FB
2038{
2039 Mpeg1Context *s1 = avctx->priv_data;
2040 MpegEncContext *s = &s1->mpeg_enc_ctx;
2041 int ret;
dfb476cb 2042 const int field_pic= s->picture_structure != PICT_FRAME;
de6d9b64 2043
db6e7795
MN
2044 s->resync_mb_x= s->mb_x =
2045 s->resync_mb_y= s->mb_y = -1;
2046
de6d9b64 2047 start_code = (start_code - 1) & 0xff;
9ac7ecd6 2048 if (start_code >= s->mb_height){
9b879566 2049 av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", start_code, s->mb_height);
db6e7795 2050 return -1;
9ac7ecd6 2051 }
9b8709d1
MN
2052
2053 ff_mpeg1_clean_buffers(s);
7bc9090a 2054 s->interlaced_dct = 0;
dfb476cb 2055
de6d9b64 2056 /* start frame decoding */
b536d0aa
MN
2057 if (s->first_slice) {
2058 if(s->first_field || s->picture_structure==PICT_FRAME){
d6db1c9c 2059 if(MPV_frame_start(s, avctx) < 0)
86f85dca 2060 return DECODE_SLICE_FATAL_ERROR;
7bc9090a
MN
2061
2062 ff_er_frame_start(s);
2063
2ec23b6d 2064 /* first check if we must repeat the frame */
9ee2c20e 2065 s->current_picture_ptr->repeat_pict = 0;
2ec23b6d
MN
2066 if (s->repeat_first_field) {
2067 if (s->progressive_sequence) {
2068 if (s->top_field_first)
9ee2c20e 2069 s->current_picture_ptr->repeat_pict = 4;
2ec23b6d 2070 else
9ee2c20e 2071 s->current_picture_ptr->repeat_pict = 2;
2ec23b6d 2072 } else if (s->progressive_frame) {
9ee2c20e 2073 s->current_picture_ptr->repeat_pict = 1;
2ec23b6d
MN
2074 }
2075 }
fa384dcc
MN
2076
2077 *s->current_picture_ptr->pan_scan= s1->pan_scan;
b536d0aa
MN
2078 }else{ //second field
2079 int i;
37b787f1
MN
2080
2081 if(!s->current_picture_ptr){
9b879566 2082 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
37b787f1
MN
2083 return -1;
2084 }
2085
b536d0aa
MN
2086 for(i=0; i<4; i++){
2087 s->current_picture.data[i] = s->current_picture_ptr->data[i];
2088 if(s->picture_structure == PICT_BOTTOM_FIELD){
2089 s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
2090 }
2091 }
2092 }
2e7b4c84
IK
2093#ifdef HAVE_XVMC
2094// MPV_frame_start will call this function too,
2095// but we need to call it on every field
2096 if(s->avctx->xvmc_acceleration)
2097 XVMC_field_start(s,avctx);
2098#endif
2099 }//fi(s->first_slice)
de6d9b64 2100
80097bbf 2101 init_get_bits(&s->gb, *buf, buf_size*8);
de6d9b64 2102
0ee50938 2103 s->qscale = get_qscale(s);
a4337a51
MN
2104 if (s->first_slice && (s->first_field || s->picture_structure==PICT_FRAME)) {
2105 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
2106 av_log(s->avctx, AV_LOG_DEBUG, "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",
2107 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],
2108 s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
2109 s->progressive_sequence ? "pro" :"", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
2110 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
2111 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
2112 }
2113 }
2114
2115 s->first_slice = 0;
7bc9090a 2116 if(s->qscale == 0){
9b879566 2117 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
db6e7795 2118 return -1;
7bc9090a
MN
2119 }
2120
de6d9b64 2121 /* extra slice info */
612476ef
A
2122 while (get_bits1(&s->gb) != 0) {
2123 skip_bits(&s->gb, 8);
de6d9b64 2124 }
7bc9090a 2125
ce5b7c5e 2126 s->mb_x=0;
7bc9090a 2127
ce5b7c5e
MN
2128 for(;;) {
2129 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
7bc9090a 2130 if (code < 0){
9b879566 2131 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
db6e7795 2132 return -1;
7bc9090a 2133 }
ce5b7c5e
MN
2134 if (code >= 33) {
2135 if (code == 33) {
2136 s->mb_x += 33;
2137 }
2138 /* otherwise, stuffing, nothing to do */
2139 } else {
2140 s->mb_x += code;
2141 break;
2142 }
2143 }
9b8709d1 2144
7bc9090a
MN
2145 s->resync_mb_x= s->mb_x;
2146 s->resync_mb_y= s->mb_y = start_code;
9b8709d1 2147 s->mb_skip_run= 0;
7d1c3fc1 2148 ff_init_block_index(s);
ce5b7c5e 2149
de6d9b64 2150 for(;;) {
a579db0c
IK
2151#ifdef HAVE_XVMC
2152 //one 1 we memcpy blocks in xvmcvideo
2153 if(s->avctx->xvmc_acceleration > 1)
2154 XVMC_init_block(s);//set s->block
2155#endif
2156
eb4b3dd3 2157 s->dsp.clear_blocks(s->block[0]);
7bc9090a 2158
b7ec19d3 2159 ret = mpeg_decode_mb(s, s->block);
332f9ac4 2160 s->chroma_qscale= s->qscale;
7bc9090a 2161
de6d9b64
FB
2162 dprintf("ret=%d\n", ret);
2163 if (ret < 0)
2164 return -1;
8d7ec294 2165
31b1ec5d
MN
2166 if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
2167 const int wrap = field_pic ? 2*s->block_wrap[0] : s->block_wrap[0];
2168 int xy = s->mb_x*2 + 1 + (s->mb_y*2 +1)*wrap;
fad05f52
WH
2169 int motion_for_top_x, motion_for_top_y, motion_back_top_x, motion_back_top_y;
2170 int motion_for_bottom_x, motion_for_bottom_y, motion_back_bottom_x, motion_back_bottom_y;
31b1ec5d
MN
2171 if(field_pic && !s->first_field)
2172 xy += wrap/2;
2173
db6e7795 2174 if (s->mb_intra) {
fad05f52
WH
2175 motion_for_top_x = motion_for_top_y = motion_back_top_x = motion_back_top_y =
2176 motion_for_bottom_x = motion_for_bottom_y = motion_back_bottom_x = motion_back_bottom_y = 0;
8d7ec294 2177 }else if (s->mv_type == MV_TYPE_16X16){
fad05f52
WH
2178 motion_for_top_x = motion_for_bottom_x = s->mv[0][0][0];
2179 motion_for_top_y = motion_for_bottom_y = s->mv[0][0][1];
2180 motion_back_top_x = motion_back_bottom_x = s->mv[1][0][0];
2181 motion_back_top_y = motion_back_bottom_y = s->mv[1][0][1];
8d7ec294 2182 } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
fad05f52
WH
2183 motion_for_top_x = s->mv[0][0][0];
2184 motion_for_top_y = s->mv[0][0][1];
2185 motion_for_bottom_x = s->mv[0][1][0];
2186 motion_for_bottom_y = s->mv[0][1][1];
2187 motion_back_top_x = s->mv[1][0][0];
2188 motion_back_top_y = s->mv[1][0][1];
2189 motion_back_bottom_x = s->mv[1][1][0];
2190 motion_back_bottom_y = s->mv[1][1][1];
8d7ec294
WH
2191 }
2192
fad05f52
WH
2193 s->current_picture.motion_val[0][xy][0] = motion_for_top_x;
2194 s->current_picture.motion_val[0][xy][1] = motion_for_top_y;
2195 s->current_picture.motion_val[0][xy + 1][0] = motion_for_top_x;
2196 s->current_picture.motion_val[0][xy + 1][1] = motion_for_top_y;
2197 s->current_picture.motion_val[0][xy + wrap][0] = motion_for_bottom_x;
2198 s->current_picture.motion_val[0][xy + wrap][1] = motion_for_bottom_y;
2199 s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_for_bottom_x;
2200 s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_for_bottom_y;
8d7ec294
WH
2201
2202 if(s->pict_type != B_TYPE){
fad05f52 2203 motion_back_top_x = motion_back_top_y = motion_back_bottom_x = motion_back_bottom_y = 0;
7bc9090a 2204 }
8d7ec294 2205
fad05f52
WH
2206 s->current_picture.motion_val[1][xy][0] = motion_back_top_x;
2207 s->current_picture.motion_val[1][xy][1] = motion_back_top_y;
2208 s->current_picture.motion_val[1][xy + 1][0] = motion_back_top_x;
2209 s->current_picture.motion_val[1][xy + 1][1] = motion_back_top_y;
2210 s->current_picture.motion_val[1][xy + wrap][0] = motion_back_bottom_x;
2211 s->current_picture.motion_val[1][xy + wrap][1] = motion_back_bottom_y;
2212 s->current_picture.motion_val[1][xy + 1 + wrap][0] = motion_back_bottom_x;
2213 s->current_picture.motion_val[1][xy + 1 + wrap][1] = motion_back_bottom_y;
7bc9090a 2214 }
8d7ec294 2215
7d1c3fc1
MN
2216 s->dest[0] += 16;
2217 s->dest[1] += 8;
2218 s->dest[2] += 8;
ce5b7c5e 2219
7d1c3fc1
MN
2220 MPV_decode_mb(s, s->block);
2221
ce5b7c5e 2222 if (++s->mb_x >= s->mb_width) {
ff862be5 2223
3bb07d61 2224 ff_draw_horiz_band(s, 16*s->mb_y, 16);
ce5b7c5e
MN
2225
2226 s->mb_x = 0;
2227 s->mb_y++;
cf713bb8
MN
2228
2229 if(s->mb_y<<field_pic >= s->mb_height){
2230 int left= s->gb.size_in_bits - get_bits_count(&s->gb);
2231
2232 if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)))
2233 || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
9b879566 2234 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d\n", left);
cf713bb8
MN
2235 return -1;
2236 }else
2237 goto eos;
2238 }
7d1c3fc1
MN
2239
2240 ff_init_block_index(s);
ce5b7c5e 2241 }
ce5b7c5e
MN
2242
2243 /* skip mb handling */
9b8709d1 2244 if (s->mb_skip_run == -1) {
ce5b7c5e 2245 /* read again increment */
9b8709d1 2246 s->mb_skip_run = 0;
ce5b7c5e
MN
2247 for(;;) {
2248 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
7bc9090a 2249 if (code < 0){
9b879566 2250 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
9c00c3af 2251 return -1;
7bc9090a 2252 }
ce5b7c5e
MN
2253 if (code >= 33) {
2254 if (code == 33) {
9b8709d1 2255 s->mb_skip_run += 33;
9c00c3af
MN
2256 }else if(code == 35){
2257 if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
9b879566 2258 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
9c00c3af
MN
2259 return -1;
2260 }
2261 goto eos; /* end of slice */
ce5b7c5e
MN
2262 }
2263 /* otherwise, stuffing, nothing to do */
2264 } else {
9b8709d1 2265 s->mb_skip_run += code;
ce5b7c5e
MN
2266 break;
2267 }
2268 }
2269 }
de6d9b64 2270 }
db6e7795
MN
2271eos: // end of slice
2272 *buf += get_bits_count(&s->gb)/8 - 1;
7bc9090a 2273//printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
db6e7795
MN
2274 return 0;
2275}
7bc9090a 2276
db6e7795
MN
2277/**
2278 * handles slice ends.
2279 * @return 1 if it seems to be the last slice of
2280 */
2281static int slice_end(AVCodecContext *avctx, AVFrame *pict)
2282{
2283 Mpeg1Context *s1 = avctx->priv_data;
2284 MpegEncContext *s = &s1->mpeg_enc_ctx;
2285
36b58e85 2286 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
855127bc
FB
2287 return 0;
2288
2e7b4c84
IK
2289#ifdef HAVE_XVMC
2290 if(s->avctx->xvmc_acceleration)
2291 XVMC_field_end(s);
2292#endif
de6d9b64 2293 /* end of slice reached */
db6e7795 2294 if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
de6d9b64 2295 /* end of image */
1e491e29 2296
029911d1 2297 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
0426af31
MN
2298 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
2299 }else
2300 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG1;
2301
7bc9090a 2302 ff_er_frame_end(s);
de6d9b64
FB
2303
2304 MPV_frame_end(s);
2305
4e00e76b 2306 if (s->pict_type == B_TYPE || s->low_delay) {
8e937a4a 2307 *pict= *(AVFrame*)s->current_picture_ptr;
0c9bbaec 2308 ff_print_debug_info(s, pict);
de6d9b64 2309 } else {
1e491e29 2310 s->picture_number++;
de6d9b64
FB
2311 /* latency of 1 frame for I and P frames */
2312 /* XXX: use another variable than picture_number */
80097bbf 2313 if (s->last_picture_ptr != NULL) {
8e937a4a 2314 *pict= *(AVFrame*)s->last_picture_ptr;
0c9bbaec 2315 ff_print_debug_info(s, pict);
de6d9b64 2316 }
de6d9b64 2317 }
ff862be5 2318
db6e7795 2319 return 1;
de6d9b64 2320 } else {
db6e7795 2321 return 0;
de6d9b64
FB
2322 }
2323}
2324
2325static int mpeg1_decode_sequence(AVCodecContext *avctx,
0c1a9eda 2326 uint8_t *buf, int buf_size)
de6d9b64
FB
2327{
2328 Mpeg1Context *s1 = avctx->priv_data;
2329 MpegEncContext *s = &s1->mpeg_enc_ctx;
60832448 2330 int width, height, i, v, j;
945f15b7 2331 float aspect;
fb4a4a56 2332
68f593b4 2333 init_get_bits(&s->gb, buf, buf_size*8);
de6d9b64
FB
2334
2335 width = get_bits(&s->gb, 12);
2336 height = get_bits(&s->gb, 12);
945f15b7 2337 s->aspect_ratio_info= get_bits(&s->gb, 4);
32e7b91a
FB
2338 if (s->aspect_ratio_info == 0)
2339 return -1;
2340 aspect= 1.0/mpeg1_aspect[s->aspect_ratio_info];
2341 avctx->sample_aspect_ratio= av_d2q(aspect, 255);
945f15b7 2342
de6d9b64
FB
2343 s->frame_rate_index = get_bits(&s->gb, 4);
2344 if (s->frame_rate_index == 0)
2345 return -1;
2346 s->bit_rate = get_bits(&s->gb, 18) * 400;
612476ef 2347 if (get_bits1(&s->gb) == 0) /* marker */
de6d9b64
FB
2348 return -1;
2349 if (width <= 0 || height <= 0 ||
2350 (width % 2) != 0 || (height % 2) != 0)
2351 return -1;
2352 if (width != s->width ||
2353 height != s->height) {
2354 /* start new mpeg1 context decoding */
2355 s->out_format = FMT_MPEG1;
2356 if (s1->mpeg_enc_ctx_allocated) {
2357 MPV_common_end(s);
2358 }
2359 s->width = width;
2360 s->height = height;
4e00e76b 2361 avctx->has_b_frames= 1;
0c23ead1 2362 s->avctx = avctx;
de6d9b64
FB
2363 avctx->width = width;
2364 avctx->height = height;
14bea432
MN
2365 av_reduce(
2366 &avctx->frame_rate,
2367 &avctx->frame_rate_base,
2368 frame_rate_tab[s->frame_rate_index],
2369 MPEG1_FRAME_RATE_BASE, //FIXME store in allready reduced form
2370 1<<30
2371 );
de6d9b64
FB
2372 avctx->bit_rate = s->bit_rate;
2373
2e7b4c84
IK
2374 //get_format() or set_video(width,height,aspect,pix_fmt);
2375 //until then pix_fmt may be changed right after codec init
2376 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
a579db0c
IK
2377 if( avctx->idct_algo == FF_IDCT_AUTO )
2378 avctx->idct_algo = FF_IDCT_SIMPLE;
2e7b4c84 2379
de6d9b64
FB
2380 if (MPV_common_init(s) < 0)
2381 return -1;
de6d9b64 2382 s1->mpeg_enc_ctx_allocated = 1;
a579db0c 2383 s->swap_uv = 0;//just in case vcr2 and mpeg2 stream have been concatinated
de6d9b64
FB
2384 }
2385
612476ef
A
2386 skip_bits(&s->gb, 10); /* vbv_buffer_size */
2387 skip_bits(&s->gb, 1);
de6d9b64
FB
2388
2389 /* get matrix */
612476ef 2390 if (get_bits1(&s->gb)) {
de6d9b64
FB
2391 for(i=0;i<64;i++) {
2392 v = get_bits(&s->gb, 8);
2ad1516a 2393 j = s->intra_scantable.permutated[i];
60832448
FB
2394 s->intra_matrix[j] = v;
2395 s->chroma_intra_matrix[j] = v;
de6d9b64 2396 }
25ed7f92
FB
2397#ifdef DEBUG
2398 dprintf("intra matrix present\n");
2399 for(i=0;i<64;i++)
8e1652dc 2400 dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]);
25ed7f92
FB
2401 printf("\n");
2402#endif
de6d9b64
FB
2403 } else {
2404 for(i=0;i<64;i++) {
b0368839 2405 int j= s->dsp.idct_permutation[i];
adc09b2e 2406 v = ff_mpeg1_default_intra_matrix[i];
2ad1516a
MN
2407 s->intra_matrix[j] = v;
2408 s->chroma_intra_matrix[j] = v;
de6d9b64
FB
2409 }
2410 }
612476ef 2411 if (get_bits1(&s->gb)) {
de6d9b64
FB
2412 for(i=0;i<64;i++) {
2413 v = get_bits(&s->gb, 8);
2ad1516a 2414 j = s->intra_scantable.permutated[i];
d7e9533a
MN
2415 s->inter_matrix[j] = v;
2416 s->chroma_inter_matrix[j] = v;
de6d9b64 2417 }
25ed7f92
FB
2418#ifdef DEBUG
2419 dprintf("non intra matrix present\n");
2420 for(i=0;i<64;i++)
8e1652dc 2421 dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]);
25ed7f92
FB
2422 printf("\n");
2423#endif
de6d9b64
FB
2424 } else {
2425 for(i=0;i<64;i++) {
b0368839 2426 int j= s->dsp.idct_permutation[i];
adc09b2e 2427 v = ff_mpeg1_default_non_intra_matrix[i];
2ad1516a
MN
2428 s->inter_matrix[j] = v;
2429 s->chroma_inter_matrix[j] = v;
de6d9b64
FB
2430 }
2431 }
2432
2433 /* we set mpeg2 parameters so that it emulates mpeg1 */
2434 s->progressive_sequence = 1;
2435 s->progressive_frame = 1;
2436 s->picture_structure = PICT_FRAME;
2437 s->frame_pred_frame_dct = 1;
029911d1 2438 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
fb4a4a56 2439 avctx->sub_id = 1; /* indicates mpeg1 */
248a189a 2440 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
de6d9b64
FB
2441 return 0;
2442}
2443
e94bc100
MN
2444static int vcr2_init_sequence(AVCodecContext *avctx)
2445{
2446 Mpeg1Context *s1 = avctx->priv_data;
2447 MpegEncContext *s = &s1->mpeg_enc_ctx;
008f0851 2448 int i, v;
e94bc100
MN
2449
2450 /* start new mpeg1 context decoding */
2451 s->out_format = FMT_MPEG1;
2452 if (s1->mpeg_enc_ctx_allocated) {
2453 MPV_common_end(s);
2454 }
2455 s->width = avctx->width;
2456 s->height = avctx->height;
2457 avctx->has_b_frames= 0; //true?
ff862be5 2458 s->low_delay= 1;
e94bc100 2459 s->avctx = avctx;
2e7b4c84
IK
2460
2461 //get_format() or set_video(width,height,aspect,pix_fmt);
2462 //until then pix_fmt may be changed right after codec init
2463 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
a579db0c
IK
2464 if( avctx->idct_algo == FF_IDCT_AUTO )
2465 avctx->idct_algo = FF_IDCT_SIMPLE;
e94bc100
MN
2466
2467 if (MPV_common_init(s) < 0)
2468 return -1;
a579db0c
IK
2469 exchange_uv(s);//common init reset pblocks, so we swap them here
2470 s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
e94bc100
MN
2471 s1->mpeg_enc_ctx_allocated = 1;
2472
2473 for(i=0;i<64;i++) {
2474 int j= s->dsp.idct_permutation[i];
2475 v = ff_mpeg1_default_intra_matrix[i];
2476 s->intra_matrix[j] = v;
2477 s->chroma_intra_matrix[j] = v;
2478
2479 v = ff_mpeg1_default_non_intra_matrix[i];
2480 s->inter_matrix[j] = v;
2481 s->chroma_inter_matrix[j] = v;
2482 }
2483
e94bc100
MN
2484 s->progressive_sequence = 1;
2485 s->progressive_frame = 1;
2486 s->picture_structure = PICT_FRAME;
2487 s->frame_pred_frame_dct = 1;
029911d1 2488 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
008f0851 2489 avctx->sub_id = 2; /* indicates mpeg2 */
e94bc100
MN
2490 return 0;
2491}
2492
2493
e2f9490e
FB
2494static void mpeg_decode_user_data(AVCodecContext *avctx,
2495 const uint8_t *buf, int buf_size)
2496{
2497 const uint8_t *p;
2498 int len, flags;
2499 p = buf;
2500 len = buf_size;
2501
2502 /* we parse the DTG active format information */
2503 if (len >= 5 &&
2504 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2505 flags = p[4];
2506 p += 5;
2507 len -= 5;
2508 if (flags & 0x80) {
2509 /* skip event id */
2510 if (len < 2)
2511 return;
2512 p += 2;
2513 len -= 2;
2514 }
2515 if (flags & 0x40) {
2516 if (len < 1)
2517 return;
2518 avctx->dtg_active_format = p[0] & 0x0f;
2519 }
2520 }
2521}
2522
80097bbf
MN
2523/**
2524 * finds the end of the current frame in the bitstream.
2525 * @return the position of the first byte of the next frame, or -1
2526 */
2527static int mpeg1_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
2528 ParseContext *pc= &s->parse_context;
2529 int i;
2530 uint32_t state;
2531
2532 state= pc->state;
2533
2534 i=0;
2535 if(!pc->frame_start_found){
2536 for(i=0; i<buf_size; i++){
2537 state= (state<<8) | buf[i];
2538 if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
2539 i++;
2540 pc->frame_start_found=1;
2541 break;
2542 }
2543 }
2544 }
2545
2546 if(pc->frame_start_found){
2547 for(; i<buf_size; i++){
2548 state= (state<<8) | buf[i];
2549 if((state&0xFFFFFF00) == 0x100){
2550 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
2551 pc->frame_start_found=0;
2552 pc->state=-1;
2553 return i-3;
2554 }
2555 }
2556 }
2557 }
2558 pc->state= state;
bb463d81 2559 return END_NOT_FOUND;
80097bbf
MN
2560}
2561
de6d9b64
FB
2562/* handle buffering and image synchronisation */
2563static int mpeg_decode_frame(AVCodecContext *avctx,
2564 void *data, int *data_size,
0c1a9eda 2565 uint8_t *buf, int buf_size)
de6d9b64
FB
2566{
2567 Mpeg1Context *s = avctx->priv_data;
80097bbf
MN
2568 uint8_t *buf_end, *buf_ptr;
2569 int ret, start_code, input_size;
492cd3a9 2570 AVFrame *picture = data;
1cb0edb4 2571 MpegEncContext *s2 = &s->mpeg_enc_ctx;
de6d9b64
FB
2572 dprintf("fill_buffer\n");
2573
2574 *data_size = 0;
d7e9533a 2575
de6d9b64 2576 /* special case for last picture */
e9174ba4
MN
2577 if (buf_size == 0 && s2->low_delay==0 && s2->next_picture_ptr) {
2578 *picture= *(AVFrame*)s2->next_picture_ptr;
2579 s2->next_picture_ptr= NULL;
1e491e29 2580
e9174ba4 2581 *data_size = sizeof(AVFrame);
de6d9b64
FB
2582 return 0;
2583 }
2584
80097bbf 2585 if(s2->flags&CODEC_FLAG_TRUNCATED){
bb463d81 2586 int next= mpeg1_find_frame_end(s2, buf, buf_size);
80097bbf
MN
2587
2588 if( ff_combine_frame(s2, next, &buf, &buf_size) < 0 )
2589 return buf_size;
2590 }
2591
de6d9b64
FB
2592 buf_ptr = buf;
2593 buf_end = buf + buf_size;
e0a3d744
J
2594
2595#if 0
2596 if (s->repeat_field % 2 == 1) {
1cb0edb4
J
2597 s->repeat_field++;
2598 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
e0a3d744
J
2599 // s2->picture_number, s->repeat_field);
2600 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
2601 *data_size = sizeof(AVPicture);
2602 goto the_end;
2603 }
1cb0edb4 2604 }
e0a3d744 2605#endif
e94bc100
MN
2606
2607 if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
2608 vcr2_init_sequence(avctx);
2609
80097bbf 2610 for(;;) {
de6d9b64 2611 /* find start next code */
80097bbf 2612 start_code = find_start_code(&buf_ptr, buf_end);
cfcff636 2613 if (start_code < 0){
80de6a80
MN
2614 if(s2->pict_type != B_TYPE || avctx->hurry_up==0){
2615 if (slice_end(avctx, picture)) {
248a189a 2616 if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
80de6a80
MN
2617 *data_size = sizeof(AVPicture);
2618 }
db6e7795 2619 }
cfcff636 2620 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
de6d9b64 2621 }
cf713bb8
MN
2622
2623 input_size = buf_end - buf_ptr;
2624
2625 if(avctx->debug & FF_DEBUG_STARTCODE){
9b879566 2626 av_log(avctx, AV_LOG_DEBUG, "%3X at %d left %d\n", start_code, buf_ptr-buf, input_size);
cf713bb8 2627 }
80097bbf 2628
de6d9b64 2629 /* prepare data for next start code */
de6d9b64
FB
2630 switch(start_code) {
2631 case SEQ_START_CODE:
80097bbf 2632 mpeg1_decode_sequence(avctx, buf_ptr,
de6d9b64
FB
2633 input_size);
2634 break;
2635
2636 case PICTURE_START_CODE:
2637 /* we have a complete image : we try to decompress it */
2638 mpeg1_decode_picture(avctx,
80097bbf 2639 buf_ptr, input_size);
de6d9b64
FB
2640 break;
2641 case EXT_START_CODE:
2642 mpeg_decode_extension(avctx,
80097bbf 2643 buf_ptr, input_size);
e2f9490e
FB
2644 break;
2645 case USER_START_CODE:
2646 mpeg_decode_user_data(avctx,
80097bbf 2647 buf_ptr, input_size);
de6d9b64 2648 break;
05fd1577
MN
2649 case GOP_START_CODE:
2650 s2->first_field=0;
2651 break;
de6d9b64
FB
2652 default:
2653 if (start_code >= SLICE_MIN_START_CODE &&
d9cb5429 2654 start_code <= SLICE_MAX_START_CODE) {
945f15b7
MN
2655
2656 /* skip b frames if we dont have reference frames */
b536d0aa 2657 if(s2->last_picture_ptr==NULL && s2->pict_type==B_TYPE) break;
945f15b7
MN
2658 /* skip b frames if we are in a hurry */
2659 if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
2660 /* skip everything if we are in a hurry>=5 */
2661 if(avctx->hurry_up>=5) break;
80097bbf 2662
37b787f1
MN
2663 if (!s->mpeg_enc_ctx_allocated) break;
2664
de6d9b64 2665 ret = mpeg_decode_slice(avctx, picture,
80097bbf 2666 start_code, &buf_ptr, input_size);
db6e7795 2667 emms_c();
dfb476cb 2668
db6e7795
MN
2669 if(ret < 0){
2670 if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
7bc9090a 2671 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
db6e7795
MN
2672 if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
2673 }else{
2674 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END|DC_END|MV_END);
de6d9b64
FB
2675 }
2676 }
2677 break;
2678 }
de6d9b64 2679 }
de6d9b64
FB
2680}
2681
2682static int mpeg_decode_end(AVCodecContext *avctx)
2683{
2684 Mpeg1Context *s = avctx->priv_data;
2685
2686 if (s->mpeg_enc_ctx_allocated)
2687 MPV_common_end(&s->mpeg_enc_ctx);
2688 return 0;
2689}
2690
922bc38d
MN
2691AVCodec mpeg1video_decoder = {
2692 "mpeg1video",
de6d9b64
FB
2693 CODEC_TYPE_VIDEO,
2694 CODEC_ID_MPEG1VIDEO,
2695 sizeof(Mpeg1Context),
2696 mpeg_decode_init,
2697 NULL,
2698 mpeg_decode_end,
2699 mpeg_decode_frame,
d7425f59 2700 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
922bc38d
MN
2701 .flush= ff_mpeg_flush,
2702};
2703
2704AVCodec mpeg2video_decoder = {
2705 "mpeg2video",
2706 CODEC_TYPE_VIDEO,
2707 CODEC_ID_MPEG2VIDEO,
2708 sizeof(Mpeg1Context),
2709 mpeg_decode_init,
2710 NULL,
2711 mpeg_decode_end,
2712 mpeg_decode_frame,
2713 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
c512b303
IK
2714 .flush= ff_mpeg_flush,
2715};
2716
2717//legacy decoder
2718AVCodec mpegvideo_decoder = {
2719 "mpegvideo",
2720 CODEC_TYPE_VIDEO,
2721 CODEC_ID_MPEG2VIDEO,
2722 sizeof(Mpeg1Context),
2723 mpeg_decode_init,
2724 NULL,
2725 mpeg_decode_end,
2726 mpeg_decode_frame,
2727 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
7a06ff14 2728 .flush= ff_mpeg_flush,
de6d9b64 2729};
2e7b4c84
IK
2730
2731#ifdef HAVE_XVMC
2732static int mpeg_mc_decode_init(AVCodecContext *avctx){
2733 Mpeg1Context *s;
2734
2735 if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
2736 return -1;
a579db0c 2737 if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
2e7b4c84 2738 dprintf("mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
a579db0c 2739 }
2e7b4c84
IK
2740 mpeg_decode_init(avctx);
2741 s = avctx->priv_data;
2742
2743 avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
a579db0c 2744 avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
2e7b4c84
IK
2745
2746 return 0;
2747}
2748
2749AVCodec mpeg_xvmc_decoder = {
2750 "mpegvideo_xvmc",
2751 CODEC_TYPE_VIDEO,
2752 CODEC_ID_MPEG2VIDEO_XVMC,
2753 sizeof(Mpeg1Context),
2754 mpeg_mc_decode_init,
2755 NULL,
2756 mpeg_decode_end,
2757 mpeg_decode_frame,
2758 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
2759};
2760
2761#endif
c3bf0288
MN
2762
2763/* this is ugly i know, but the alternative is too make
2764 hundreds of vars global and prefix them with ff_mpeg1_
2765 which is far uglier. */
2766#include "mdec.c"