ffmpeg / libavcodec / mpeg12.c @ cb231c48
History  View  Annotate  Download (86.5 KB)
1 
/*


2 
* MPEG1 codec / MPEG2 decoder

3 
* Copyright (c) 2000,2001 Fabrice Bellard.

4 
*

5 
* This library is free software; you can redistribute it and/or

6 
* modify it under the terms of the GNU Lesser General Public

7 
* License as published by the Free Software Foundation; either

8 
* version 2 of the License, or (at your option) any later version.

9 
*

10 
* This library is distributed in the hope that it will be useful,

11 
* but WITHOUT ANY WARRANTY; without even the implied warranty of

12 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

13 
* Lesser General Public License for more details.

14 
*

15 
* You should have received a copy of the GNU Lesser General Public

16 
* License along with this library; if not, write to the Free Software

17 
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA

18 
*/

19 

20 
/**

21 
* @file mpeg12.c

22 
* MPEG1/2 codec

23 
*/

24 

25 
//#define DEBUG

26 
#include "avcodec.h" 
27 
#include "dsputil.h" 
28 
#include "mpegvideo.h" 
29  
30 
#include "mpeg12data.h" 
31  
32  
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  
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  
51 
static void mpeg1_encode_block(MpegEncContext *s, 
52 
DCTELEM *block, 
53 
int component);

54 
static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added 
55 
static void mpeg1_skip_picture(MpegEncContext *s, int pict_num); 
56 
static inline int mpeg1_decode_block_inter(MpegEncContext *s, 
57 
DCTELEM *block, 
58 
int n);

59 
static inline int mpeg1_decode_block_intra(MpegEncContext *s, 
60 
DCTELEM *block, 
61 
int n);

62 
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
63 
DCTELEM *block, 
64 
int n);

65 
static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
66 
DCTELEM *block, 
67 
int n);

68 
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred); 
69  
70 
#ifdef HAVE_XVMC

71 
extern int XVMC_field_start(MpegEncContext *s, AVCodecContext *avctx); 
72 
extern int XVMC_field_end(MpegEncContext *s); 
73 
#endif

74  
75 
#ifdef CONFIG_ENCODERS

76 
static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL; 
77 
static uint8_t fcode_tab[MAX_MV*2+1]; 
78  
79 
static uint32_t uni_mpeg1_ac_vlc_bits[64*64*2]; 
80 
static uint8_t uni_mpeg1_ac_vlc_len [64*64*2]; 
81  
82 
/* simple include everything table for dc, first byte is bits number next 3 are code*/

83 
static uint32_t mpeg1_lum_dc_uni[512]; 
84 
static uint32_t mpeg1_chr_dc_uni[512]; 
85  
86 
static uint8_t mpeg1_index_run[2][64]; 
87 
static int8_t mpeg1_max_level[2][64]; 
88 
#endif

89  
90 
static void init_2d_vlc_rl(RLTable *rl) 
91 
{ 
92 
int i;

93 

94 
init_vlc(&rl>vlc, TEX_VLC_BITS, rl>n + 2,

95 
&rl>table_vlc[0][1], 4, 2, 
96 
&rl>table_vlc[0][0], 4, 2); 
97  
98 

99 
rl>rl_vlc[0]= av_malloc(rl>vlc.table_size*sizeof(RL_VLC_ELEM)); 
100 
for(i=0; i<rl>vlc.table_size; i++){ 
101 
int code= rl>vlc.table[i][0]; 
102 
int len = rl>vlc.table[i][1]; 
103 
int level, run;

104 

105 
if(len==0){ // illegal code 
106 
run= 65;

107 
level= MAX_LEVEL; 
108 
}else if(len<0){ //more bits needed 
109 
run= 0;

110 
level= code; 
111 
}else{

112 
if(code==rl>n){ //esc 
113 
run= 65;

114 
level= 0;

115 
}else if(code==rl>n+1){ //eob 
116 
run= 0;

117 
level= 127;

118 
}else{

119 
run= rl>table_run [code] + 1;

120 
level= rl>table_level[code]; 
121 
} 
122 
} 
123 
rl>rl_vlc[0][i].len= len;

124 
rl>rl_vlc[0][i].level= level;

125 
rl>rl_vlc[0][i].run= run;

126 
} 
127 
} 
128  
129 
#ifdef CONFIG_ENCODERS

130 
static void init_uni_ac_vlc(RLTable *rl, uint32_t *uni_ac_vlc_bits, uint8_t *uni_ac_vlc_len){ 
131 
int i;

132  
133 
for(i=0; i<128; i++){ 
134 
int level= i64; 
135 
int run;

136 
for(run=0; run<64; run++){ 
137 
int len, bits, code;

138 

139 
int alevel= ABS(level);

140 
int sign= (level>>31)&1; 
141  
142 
if (alevel > rl>max_level[0][run]) 
143 
code= 111; /*rl>n*/ 
144 
else

145 
code= rl>index_run[0][run] + alevel  1; 
146  
147 
if (code < 111 /* rl>n */) { 
148 
/* store the vlc & sign at once */

149 
len= mpeg1_vlc[code][1]+1; 
150 
bits= (mpeg1_vlc[code][0]<<1) + sign; 
151 
} else {

152 
len= mpeg1_vlc[111/*rl>n*/][1]+6; 
153 
bits= mpeg1_vlc[111/*rl>n*/][0]<<6; 
154  
155 
bits= run; 
156 
if (alevel < 128) { 
157 
bits<<=8; len+=8; 
158 
bits= level & 0xff;

159 
} else {

160 
bits<<=16; len+=16; 
161 
bits= level & 0xff;

162 
if (level < 0) { 
163 
bits= 0x8001 + level + 255; 
164 
} else {

165 
bits= level & 0xffff;

166 
} 
167 
} 
168 
} 
169  
170 
uni_ac_vlc_bits[UNI_AC_ENC_INDEX(run, i)]= bits; 
171 
uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len; 
172 
} 
173 
} 
174 
} 
175  
176 
static void put_header(MpegEncContext *s, int header) 
177 
{ 
178 
align_put_bits(&s>pb); 
179 
put_bits(&s>pb, 16, header>>16); 
180 
put_bits(&s>pb, 16, header&0xFFFF); 
181 
} 
182  
183 
/* put sequence header if needed */

184 
static void mpeg1_encode_sequence_header(MpegEncContext *s) 
185 
{ 
186 
unsigned int vbv_buffer_size; 
187 
unsigned int fps, v; 
188 
int n, i;

189 
uint64_t time_code; 
190 
float best_aspect_error= 1E10; 
191 
float aspect_ratio= s>avctx>aspect_ratio;

192 

193 
if(aspect_ratio==0.0) aspect_ratio= s>width / (float)s>height; //pixel aspect 1:1 (VGA) 
194 

195 
if (s>current_picture.key_frame) {

196 
/* mpeg1 header repeated every gop */

197 
put_header(s, SEQ_START_CODE); 
198 

199 
/* search closest frame rate */

200 
{ 
201 
int i, dmin, d;

202 
s>frame_rate_index = 0;

203 
dmin = 0x7fffffff;

204 
for(i=1;i<14;i++) { 
205 
if(s>avctx>strict_std_compliance >= 0 && i>=9) break; 
206 

207 
d = abs(MPEG1_FRAME_RATE_BASE*(int64_t)s>avctx>frame_rate/s>avctx>frame_rate_base  frame_rate_tab[i]); 
208 
if (d < dmin) {

209 
dmin = d; 
210 
s>frame_rate_index = i; 
211 
} 
212 
} 
213 
} 
214 

215 
put_bits(&s>pb, 12, s>width);

216 
put_bits(&s>pb, 12, s>height);

217 

218 
for(i=1; i<15; i++){ 
219 
float error= mpeg1_aspect[i]  s>width/(s>height*aspect_ratio);

220 
error= ABS(error); 
221 

222 
if(error < best_aspect_error){

223 
best_aspect_error= error; 
224 
s>aspect_ratio_info= i; 
225 
} 
226 
} 
227 

228 
put_bits(&s>pb, 4, s>aspect_ratio_info);

229 
put_bits(&s>pb, 4, s>frame_rate_index);

230 

231 
if(s>avctx>rc_max_rate){

232 
v = (s>avctx>rc_max_rate + 399) / 400; 
233 
if (v > 0x3ffff && s>codec_id == CODEC_ID_MPEG1VIDEO) 
234 
v = 0x3ffff;

235 
}else{

236 
v= 0x3FFFF;

237 
} 
238  
239 
if(s>avctx>rc_buffer_size)

240 
vbv_buffer_size = s>avctx>rc_buffer_size; 
241 
else

242 
/* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */

243 
vbv_buffer_size = (( 20 * s>bit_rate) / (1151929 / 2)) * 8 * 1024; 
244 
vbv_buffer_size= (vbv_buffer_size + 16383) / 16384; 
245  
246 
put_bits(&s>pb, 18, v & 0x3FFFF); 
247 
put_bits(&s>pb, 1, 1); /* marker */ 
248 
put_bits(&s>pb, 10, vbv_buffer_size & 0x3FF); 
249 
put_bits(&s>pb, 1, 1); /* constrained parameter flag */ 
250 

251 
ff_write_quant_matrix(&s>pb, s>avctx>intra_matrix); 
252 
ff_write_quant_matrix(&s>pb, s>avctx>inter_matrix); 
253  
254 
if(s>codec_id == CODEC_ID_MPEG2VIDEO){

255 
put_header(s, EXT_START_CODE); 
256 
put_bits(&s>pb, 4, 1); //seq ext 
257 
put_bits(&s>pb, 1, 0); //esc 
258 
put_bits(&s>pb, 3, 4); //profile 
259 
put_bits(&s>pb, 4, 8); //level 
260 
put_bits(&s>pb, 1, s>progressive_sequence=1); 
261 
put_bits(&s>pb, 2, 1); //chroma format 4:2:0 
262 
put_bits(&s>pb, 2, 0); //horizontal size ext 
263 
put_bits(&s>pb, 2, 0); //vertical size ext 
264 
put_bits(&s>pb, 12, v>>18); //bitrate ext 
265 
put_bits(&s>pb, 1, 1); //marker 
266 
put_bits(&s>pb, 8, vbv_buffer_size >>10); //vbv buffer ext 
267 
put_bits(&s>pb, 1, s>low_delay);

268 
put_bits(&s>pb, 2, 0); // frame_rate_ext_n 
269 
put_bits(&s>pb, 5, 0); // frame_rate_ext_d 
270 
} 
271 

272 
put_header(s, GOP_START_CODE); 
273 
put_bits(&s>pb, 1, 0); /* do drop frame */ 
274 
/* time code : we must convert from the real frame rate to a

275 
fake mpeg frame rate in case of low frame rate */

276 
fps = frame_rate_tab[s>frame_rate_index]; 
277 
time_code = (int64_t)s>fake_picture_number * MPEG1_FRAME_RATE_BASE; 
278 
s>gop_picture_number = s>fake_picture_number; 
279 
put_bits(&s>pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24)); 
280 
put_bits(&s>pb, 6, (uint32_t)((time_code / (fps * 60)) % 60)); 
281 
put_bits(&s>pb, 1, 1); 
282 
put_bits(&s>pb, 6, (uint32_t)((time_code / fps) % 60)); 
283 
put_bits(&s>pb, 6, (uint32_t)((time_code % fps) / MPEG1_FRAME_RATE_BASE));

284 
put_bits(&s>pb, 1, 0); /* closed gop */ 
285 
put_bits(&s>pb, 1, 0); /* broken link */ 
286 
} 
287  
288 
if (s>avctx>frame_rate < (24 * s>avctx>frame_rate_base) && s>picture_number > 0) { 
289 
/* insert empty P pictures to slow down to the desired

290 
frame rate. Each fake pictures takes about 20 bytes */

291 
fps = frame_rate_tab[s>frame_rate_index]; 
292 
n = av_rescale((int64_t)s>picture_number * s>avctx>frame_rate_base, fps, s>avctx>frame_rate) / MPEG1_FRAME_RATE_BASE  1;

293 
while (s>fake_picture_number < n) {

294 
mpeg1_skip_picture(s, s>fake_picture_number  
295 
s>gop_picture_number); 
296 
s>fake_picture_number++; 
297 
} 
298  
299 
} 
300 
} 
301  
302 
static inline void encode_mb_skip_run(MpegEncContext *s, int run){ 
303 
while (run >= 33) { 
304 
put_bits(&s>pb, 11, 0x008); 
305 
run = 33;

306 
} 
307 
put_bits(&s>pb, mbAddrIncrTable[run][1],

308 
mbAddrIncrTable[run][0]);

309 
} 
310  
311 
/* insert a fake P picture */

312 
static void mpeg1_skip_picture(MpegEncContext *s, int pict_num) 
313 
{ 
314 
assert(s>codec_id == CODEC_ID_MPEG1VIDEO); // mpeg2 can do these repeat things

315  
316 
/* mpeg1 picture header */

317 
put_header(s, PICTURE_START_CODE); 
318 
/* temporal reference */

319 
put_bits(&s>pb, 10, pict_num & 0x3ff); 
320 

321 
put_bits(&s>pb, 3, P_TYPE);

322 
put_bits(&s>pb, 16, 0xffff); /* non constant bit rate */ 
323 

324 
put_bits(&s>pb, 1, 1); /* integer coordinates */ 
325 
put_bits(&s>pb, 3, 1); /* forward_f_code */ 
326 

327 
put_bits(&s>pb, 1, 0); /* extra bit picture */ 
328 

329 
/* only one slice */

330 
put_header(s, SLICE_MIN_START_CODE); 
331 
put_bits(&s>pb, 5, 1); /* quantizer scale */ 
332 
put_bits(&s>pb, 1, 0); /* slice extra information */ 
333 

334 
encode_mb_skip_run(s, 0);

335 

336 
/* empty macroblock */

337 
put_bits(&s>pb, 3, 1); /* motion only */ 
338 

339 
/* zero motion x & y */

340 
put_bits(&s>pb, 1, 1); 
341 
put_bits(&s>pb, 1, 1); 
342  
343 
/* output a number of empty slice */

344 
encode_mb_skip_run(s, s>mb_width * s>mb_height  2);

345 

346 
/* empty macroblock */

347 
put_bits(&s>pb, 3, 1); /* motion only */ 
348 

349 
/* zero motion x & y */

350 
put_bits(&s>pb, 1, 1); 
351 
put_bits(&s>pb, 1, 1); 
352 
} 
353 
#endif

354  
355 
static void common_init(MpegEncContext *s) 
356 
{ 
357 
s>y_dc_scale_table= 
358 
s>c_dc_scale_table= ff_mpeg1_dc_scale_table; 
359 
} 
360  
361 
void ff_mpeg1_clean_buffers(MpegEncContext *s){

362 
s>last_dc[0] = 1 << (7 + s>intra_dc_precision); 
363 
s>last_dc[1] = s>last_dc[0]; 
364 
s>last_dc[2] = s>last_dc[0]; 
365 
memset(s>last_mv, 0, sizeof(s>last_mv)); 
366 
} 
367  
368 
#ifdef CONFIG_ENCODERS

369  
370 
void ff_mpeg1_encode_slice_header(MpegEncContext *s){

371 
put_header(s, SLICE_MIN_START_CODE + s>mb_y); 
372 
put_bits(&s>pb, 5, s>qscale); /* quantizer scale */ 
373 
put_bits(&s>pb, 1, 0); /* slice extra information */ 
374 
} 
375  
376 
void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number) 
377 
{ 
378 
mpeg1_encode_sequence_header(s); 
379  
380 
/* mpeg1 picture header */

381 
put_header(s, PICTURE_START_CODE); 
382 
/* temporal reference */

383  
384 
// RAL: s>picture_number instead of s>fake_picture_number

385 
put_bits(&s>pb, 10, (s>picture_number 

386 
s>gop_picture_number) & 0x3ff);

387 
s>fake_picture_number++; 
388 

389 
put_bits(&s>pb, 3, s>pict_type);

390 
put_bits(&s>pb, 16, 0xffff); /* non constant bit rate */ 
391 

392 
// RAL: Forward f_code also needed for B frames

393 
if (s>pict_type == P_TYPE  s>pict_type == B_TYPE) {

394 
put_bits(&s>pb, 1, 0); /* half pel coordinates */ 
395 
if(s>codec_id == CODEC_ID_MPEG1VIDEO)

396 
put_bits(&s>pb, 3, s>f_code); /* forward_f_code */ 
397 
else

398 
put_bits(&s>pb, 3, 7); /* forward_f_code */ 
399 
} 
400 

401 
// RAL: Backward f_code necessary for B frames

402 
if (s>pict_type == B_TYPE) {

403 
put_bits(&s>pb, 1, 0); /* half pel coordinates */ 
404 
if(s>codec_id == CODEC_ID_MPEG1VIDEO)

405 
put_bits(&s>pb, 3, s>b_code); /* backward_f_code */ 
406 
else

407 
put_bits(&s>pb, 3, 7); /* backward_f_code */ 
408 
} 
409  
410 
put_bits(&s>pb, 1, 0); /* extra bit picture */ 
411 

412 
if(s>codec_id == CODEC_ID_MPEG2VIDEO){

413 
put_header(s, EXT_START_CODE); 
414 
put_bits(&s>pb, 4, 8); //pic ext 
415 
if (s>pict_type == P_TYPE  s>pict_type == B_TYPE) {

416 
put_bits(&s>pb, 4, s>f_code);

417 
put_bits(&s>pb, 4, s>f_code);

418 
}else{

419 
put_bits(&s>pb, 8, 255); 
420 
} 
421 
if (s>pict_type == B_TYPE) {

422 
put_bits(&s>pb, 4, s>b_code);

423 
put_bits(&s>pb, 4, s>b_code);

424 
}else{

425 
put_bits(&s>pb, 8, 255); 
426 
} 
427 
put_bits(&s>pb, 2, s>intra_dc_precision);

428 
put_bits(&s>pb, 2, s>picture_structure= PICT_FRAME);

429 
put_bits(&s>pb, 1, s>top_field_first);

430 
put_bits(&s>pb, 1, s>frame_pred_frame_dct= 1); 
431 
put_bits(&s>pb, 1, s>concealment_motion_vectors);

432 
put_bits(&s>pb, 1, s>q_scale_type);

433 
put_bits(&s>pb, 1, s>intra_vlc_format);

434 
put_bits(&s>pb, 1, s>alternate_scan);

435 
put_bits(&s>pb, 1, s>repeat_first_field);

436 
put_bits(&s>pb, 1, s>chroma_420_type=1); 
437 
put_bits(&s>pb, 1, s>progressive_frame=1); 
438 
put_bits(&s>pb, 1, 0); //composite_display_flag 
439 
} 
440 

441 
s>mb_y=0;

442 
ff_mpeg1_encode_slice_header(s); 
443 
} 
444  
445 
void mpeg1_encode_mb(MpegEncContext *s,

446 
DCTELEM block[6][64], 
447 
int motion_x, int motion_y) 
448 
{ 
449 
int i, cbp;

450 
const int mb_x = s>mb_x; 
451 
const int mb_y = s>mb_y; 
452 
const int first_mb= mb_x == s>resync_mb_x && mb_y == s>resync_mb_y; 
453  
454 
/* compute cbp */

455 
cbp = 0;

456 
for(i=0;i<6;i++) { 
457 
if (s>block_last_index[i] >= 0) 
458 
cbp = 1 << (5  i); 
459 
} 
460  
461 
if (cbp == 0 && !first_mb && (mb_x != s>mb_width  1  (mb_y != s>mb_height  1 && s>codec_id == CODEC_ID_MPEG1VIDEO)) && 
462 
((s>pict_type == P_TYPE && (motion_x  motion_y) == 0) 

463 
(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)  
464 
((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))) { 
465 
s>mb_skip_run++; 
466 
s>qscale = s>dquant; 
467 
s>skip_count++; 
468 
s>misc_bits++; 
469 
s>last_bits++; 
470 
} else {

471 
if(first_mb){

472 
assert(s>mb_skip_run == 0);

473 
encode_mb_skip_run(s, s>mb_x); 
474 
}else{

475 
encode_mb_skip_run(s, s>mb_skip_run); 
476 
} 
477 

478 
if (s>pict_type == I_TYPE) {

479 
if(s>dquant && cbp){

480 
put_bits(&s>pb, 2, 1); /* macroblock_type : macroblock_quant = 1 */ 
481 
put_bits(&s>pb, 5, s>qscale);

482 
}else{

483 
put_bits(&s>pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */ 
484 
s>qscale = s>dquant; 
485 
} 
486 
s>misc_bits+= get_bits_diff(s); 
487 
s>i_count++; 
488 
} else if (s>mb_intra) { 
489 
if(s>dquant && cbp){

490 
put_bits(&s>pb, 6, 0x01); 
491 
put_bits(&s>pb, 5, s>qscale);

492 
}else{

493 
put_bits(&s>pb, 5, 0x03); 
494 
s>qscale = s>dquant; 
495 
} 
496 
s>misc_bits+= get_bits_diff(s); 
497 
s>i_count++; 
498 
s>last_mv[0][0][0] = 
499 
s>last_mv[0][0][1] = 0; 
500 
} else if (s>pict_type == P_TYPE) { 
501 
if (cbp != 0) { 
502 
if (motion_x == 0 && motion_y == 0) { 
503 
if(s>dquant){

504 
put_bits(&s>pb, 5, 1); /* macroblock_pattern & quant */ 
505 
put_bits(&s>pb, 5, s>qscale);

506 
}else{

507 
put_bits(&s>pb, 2, 1); /* macroblock_pattern only */ 
508 
} 
509 
s>misc_bits+= get_bits_diff(s); 
510 
put_bits(&s>pb, mbPatTable[cbp  1][1], mbPatTable[cbp  1][0]); 
511 
} else {

512 
if(s>dquant){

513 
put_bits(&s>pb, 5, 2); /* motion + cbp */ 
514 
put_bits(&s>pb, 5, s>qscale);

515 
}else{

516 
put_bits(&s>pb, 1, 1); /* motion + cbp */ 
517 
} 
518 
s>misc_bits+= get_bits_diff(s); 
519 
mpeg1_encode_motion(s, motion_x  s>last_mv[0][0][0], s>f_code); // RAL: f_code parameter added 
520 
mpeg1_encode_motion(s, motion_y  s>last_mv[0][0][1], s>f_code); // RAL: f_code parameter added 
521 
s>mv_bits+= get_bits_diff(s); 
522 
put_bits(&s>pb, mbPatTable[cbp  1][1], mbPatTable[cbp  1][0]); 
523 
} 
524 
} else {

525 
put_bits(&s>pb, 3, 1); /* motion only */ 
526 
mpeg1_encode_motion(s, motion_x  s>last_mv[0][0][0], s>f_code); // RAL: f_code parameter added 
527 
mpeg1_encode_motion(s, motion_y  s>last_mv[0][0][1], s>f_code); // RAL: f_code parameter added 
528 
s>qscale = s>dquant; 
529 
s>mv_bits+= get_bits_diff(s); 
530 
} 
531 
s>f_count++; 
532 
} else

533 
{ // RAL: All the following bloc added for B frames:

534 
if (cbp != 0) 
535 
{ // With coded bloc pattern

536 
if (s>mv_dir == (MV_DIR_FORWARD  MV_DIR_BACKWARD))

537 
{ // Bidirectional motion

538 
if (s>dquant)

539 
{ // With QScale

540 
put_bits(&s>pb, 5, 2); 
541 
put_bits(&s>pb, 5, s>qscale);

542 
} 
543 
else // Without QScale 
544 
put_bits(&s>pb, 2, 3); 
545 
s>misc_bits += get_bits_diff(s); 
546 
mpeg1_encode_motion(s, s>mv[0][0][0]  s>last_mv[0][0][0], s>f_code); 
547 
mpeg1_encode_motion(s, s>mv[0][0][1]  s>last_mv[0][0][1], s>f_code); 
548 
mpeg1_encode_motion(s, s>mv[1][0][0]  s>last_mv[1][0][0], s>b_code); 
549 
mpeg1_encode_motion(s, s>mv[1][0][1]  s>last_mv[1][0][1], s>b_code); 
550 
s>b_count++; 
551 
s>f_count++; 
552 
s>mv_bits += get_bits_diff(s); 
553 
put_bits(&s>pb, mbPatTable[cbp  1][1], mbPatTable[cbp  1][0]); 
554 
} 
555 
else if (s>mv_dir == MV_DIR_BACKWARD) 
556 
{ // Backward motion

557 
if (s>dquant)

558 
{ // With QScale

559 
put_bits(&s>pb, 6, 2); 
560 
put_bits(&s>pb, 5, s>qscale);

561 
} 
562 
else // Without QScale 
563 
put_bits(&s>pb, 3, 3); 
564 
s>misc_bits += get_bits_diff(s); 
565 
mpeg1_encode_motion(s, motion_x  s>last_mv[1][0][0], s>b_code); 
566 
mpeg1_encode_motion(s, motion_y  s>last_mv[1][0][1], s>b_code); 
567 
s>b_count++; 
568 
s>mv_bits += get_bits_diff(s); 
569 
put_bits(&s>pb, mbPatTable[cbp  1][1], mbPatTable[cbp  1][0]); 
570 
} 
571 
else if (s>mv_dir == MV_DIR_FORWARD) 
572 
{ // Forward motion

573 
if (s>dquant)

574 
{ // With QScale

575 
put_bits(&s>pb, 6, 3); 
576 
put_bits(&s>pb, 5, s>qscale);

577 
} 
578 
else // Without QScale 
579 
put_bits(&s>pb, 4, 3); 
580 
s>misc_bits += get_bits_diff(s); 
581 
mpeg1_encode_motion(s, motion_x  s>last_mv[0][0][0], s>f_code); 
582 
mpeg1_encode_motion(s, motion_y  s>last_mv[0][0][1], s>f_code); 
583 
s>f_count++; 
584 
s>mv_bits += get_bits_diff(s); 
585 
put_bits(&s>pb, mbPatTable[cbp  1][1], mbPatTable[cbp  1][0]); 
586 
} 
587 
} 
588 
else

589 
{ // No coded bloc pattern

590 
if (s>mv_dir == (MV_DIR_FORWARD  MV_DIR_BACKWARD))

591 
{ // Bidirectional motion

592 
put_bits(&s>pb, 2, 2); /* backward & forward motion */ 
593 
mpeg1_encode_motion(s, s>mv[0][0][0]  s>last_mv[0][0][0], s>f_code); 
594 
mpeg1_encode_motion(s, s>mv[0][0][1]  s>last_mv[0][0][1], s>f_code); 
595 
mpeg1_encode_motion(s, s>mv[1][0][0]  s>last_mv[1][0][0], s>b_code); 
596 
mpeg1_encode_motion(s, s>mv[1][0][1]  s>last_mv[1][0][1], s>b_code); 
597 
s>b_count++; 
598 
s>f_count++; 
599 
} 
600 
else if (s>mv_dir == MV_DIR_BACKWARD) 
601 
{ // Backward motion

602 
put_bits(&s>pb, 3, 2); /* backward motion only */ 
603 
mpeg1_encode_motion(s, motion_x  s>last_mv[1][0][0], s>b_code); 
604 
mpeg1_encode_motion(s, motion_y  s>last_mv[1][0][1], s>b_code); 
605 
s>b_count++; 
606 
} 
607 
else if (s>mv_dir == MV_DIR_FORWARD) 
608 
{ // Forward motion

609 
put_bits(&s>pb, 4, 2); /* forward motion only */ 
610 
mpeg1_encode_motion(s, motion_x  s>last_mv[0][0][0], s>f_code); 
611 
mpeg1_encode_motion(s, motion_y  s>last_mv[0][0][1], s>f_code); 
612 
s>f_count++; 
613 
} 
614 
s>qscale = s>dquant; 
615 
s>mv_bits += get_bits_diff(s); 
616 
} 
617 
// End of bloc from RAL

618 
} 
619 
for(i=0;i<6;i++) { 
620 
if (cbp & (1 << (5  i))) { 
621 
mpeg1_encode_block(s, block[i], i); 
622 
} 
623 
} 
624 
s>mb_skip_run = 0;

625 
if(s>mb_intra)

626 
s>i_tex_bits+= get_bits_diff(s); 
627 
else

628 
s>p_tex_bits+= get_bits_diff(s); 
629 
} 
630  
631 
// RAL: By this:

632 
if (s>mv_dir & MV_DIR_FORWARD)

633 
{ 
634 
s>last_mv[0][0][0]= s>mv[0][0][0]; 
635 
s>last_mv[0][0][1]= s>mv[0][0][1]; 
636 
} 
637 
if (s>mv_dir & MV_DIR_BACKWARD)

638 
{ 
639 
s>last_mv[1][0][0]= s>mv[1][0][0]; 
640 
s>last_mv[1][0][1]= s>mv[1][0][1]; 
641 
} 
642 
} 
643  
644 
// RAL: Parameter added: f_or_b_code

645 
static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code) 
646 
{ 
647 
int code, bit_size, l, m, bits, range, sign;

648  
649 
if (val == 0) { 
650 
/* zero vector */

651 
code = 0;

652 
put_bits(&s>pb, 
653 
mbMotionVectorTable[0][1], 
654 
mbMotionVectorTable[0][0]); 
655 
} else {

656 
bit_size = f_or_b_code  1;

657 
range = 1 << bit_size;

658 
/* modulo encoding */

659 
l = 16 * range;

660 
m = 2 * l;

661 
if (val < l) {

662 
val += m; 
663 
} else if (val >= l) { 
664 
val = m; 
665 
} 
666  
667 
if (val >= 0) { 
668 
val; 
669 
code = (val >> bit_size) + 1;

670 
bits = val & (range  1);

671 
sign = 0;

672 
} else {

673 
val = val; 
674 
val; 
675 
code = (val >> bit_size) + 1;

676 
bits = val & (range  1);

677 
sign = 1;

678 
} 
679  
680 
assert(code > 0 && code <= 16); 
681  
682 
put_bits(&s>pb, 
683 
mbMotionVectorTable[code][1],

684 
mbMotionVectorTable[code][0]);

685  
686 
put_bits(&s>pb, 1, sign);

687 
if (bit_size > 0) { 
688 
put_bits(&s>pb, bit_size, bits); 
689 
} 
690 
} 
691 
} 
692  
693 
void ff_mpeg1_encode_init(MpegEncContext *s)

694 
{ 
695 
static int done=0; 
696  
697 
common_init(s); 
698  
699 
if(!done){

700 
int f_code;

701 
int mv;

702 
int i;

703  
704 
done=1;

705 
init_rl(&rl_mpeg1); 
706  
707 
for(i=0; i<64; i++) 
708 
{ 
709 
mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i]; 
710 
mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i]; 
711 
} 
712 

713 
init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_bits, uni_mpeg1_ac_vlc_len); 
714  
715 
/* build unified dc encoding tables */

716 
for(i=255; i<256; i++) 
717 
{ 
718 
int adiff, index;

719 
int bits, code;

720 
int diff=i;

721  
722 
adiff = ABS(diff); 
723 
if(diff<0) diff; 
724 
index = vlc_dc_table[adiff]; 
725  
726 
bits= vlc_dc_lum_bits[index] + index; 
727 
code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index)  1)); 
728 
mpeg1_lum_dc_uni[i+255]= bits + (code<<8); 
729 

730 
bits= vlc_dc_chroma_bits[index] + index; 
731 
code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index)  1)); 
732 
mpeg1_chr_dc_uni[i+255]= bits + (code<<8); 
733 
} 
734  
735 
mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) ); 
736  
737 
for(f_code=1; f_code<=MAX_FCODE; f_code++){ 
738 
for(mv=MAX_MV; mv<=MAX_MV; mv++){

739 
int len;

740  
741 
if(mv==0) len= mbMotionVectorTable[0][1]; 
742 
else{

743 
int val, bit_size, range, code;

744  
745 
bit_size = s>f_code  1;

746 
range = 1 << bit_size;

747  
748 
val=mv; 
749 
if (val < 0) 
750 
val = val; 
751 
val; 
752 
code = (val >> bit_size) + 1;

753 
if(code<17){ 
754 
len= mbMotionVectorTable[code][1] + 1 + bit_size; 
755 
}else{

756 
len= mbMotionVectorTable[16][1] + 2 + bit_size; 
757 
} 
758 
} 
759  
760 
mv_penalty[f_code][mv+MAX_MV]= len; 
761 
} 
762 
} 
763 

764  
765 
for(f_code=MAX_FCODE; f_code>0; f_code){ 
766 
for(mv=(8<<f_code); mv<(8<<f_code); mv++){ 
767 
fcode_tab[mv+MAX_MV]= f_code; 
768 
} 
769 
} 
770 
} 
771 
s>me.mv_penalty= mv_penalty; 
772 
s>fcode_tab= fcode_tab; 
773 
if(s>codec_id == CODEC_ID_MPEG1VIDEO){

774 
s>min_qcoeff=255;

775 
s>max_qcoeff= 255;

776 
}else{

777 
s>min_qcoeff=2047;

778 
s>max_qcoeff= 2047;

779 
} 
780 
s>intra_ac_vlc_length= 
781 
s>inter_ac_vlc_length= uni_mpeg1_ac_vlc_len; 
782 
} 
783  
784 
static inline void encode_dc(MpegEncContext *s, int diff, int component) 
785 
{ 
786 
if (component == 0) { 
787 
put_bits( 
788 
&s>pb, 
789 
mpeg1_lum_dc_uni[diff+255]&0xFF, 
790 
mpeg1_lum_dc_uni[diff+255]>>8); 
791 
} else {

792 
put_bits( 
793 
&s>pb, 
794 
mpeg1_chr_dc_uni[diff+255]&0xFF, 
795 
mpeg1_chr_dc_uni[diff+255]>>8); 
796 
} 
797 
} 
798  
799 
static void mpeg1_encode_block(MpegEncContext *s, 
800 
DCTELEM *block, 
801 
int n)

802 
{ 
803 
int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;

804 
int code, component;

805 
// RLTable *rl = &rl_mpeg1;

806  
807 
last_index = s>block_last_index[n]; 
808  
809 
/* DC coef */

810 
if (s>mb_intra) {

811 
component = (n <= 3 ? 0 : n  4 + 1); 
812 
dc = block[0]; /* overflow is impossible */ 
813 
diff = dc  s>last_dc[component]; 
814 
encode_dc(s, diff, component); 
815 
s>last_dc[component] = dc; 
816 
i = 1;

817 
/*

818 
if (s>intra_vlc_format)

819 
rl = &rl_mpeg2;

820 
else

821 
rl = &rl_mpeg1;

822 
*/

823 
} else {

824 
/* encode the first coefficient : needs to be done here because

825 
it is handled slightly differently */

826 
level = block[0];

827 
if (abs(level) == 1) { 
828 
code = ((uint32_t)level >> 31); /* the sign bit */ 
829 
put_bits(&s>pb, 2, code  0x02); 
830 
i = 1;

831 
} else {

832 
i = 0;

833 
last_non_zero = 1;

834 
goto next_coef;

835 
} 
836 
} 
837  
838 
/* now quantify & encode AC coefs */

839 
last_non_zero = i  1;

840  
841 
for(;i<=last_index;i++) {

842 
j = s>intra_scantable.permutated[i]; 
843 
level = block[j]; 
844 
next_coef:

845 
#if 0

846 
if (level != 0)

847 
dprintf("level[%d]=%d\n", i, level);

848 
#endif

849 
/* encode using VLC */

850 
if (level != 0) { 
851 
run = i  last_non_zero  1;

852 

853 
alevel= level; 
854 
MASK_ABS(sign, alevel) 
855 
sign&=1;

856  
857 
// code = get_rl_index(rl, 0, run, alevel);

858 
if (alevel <= mpeg1_max_level[0][run]){ 
859 
code= mpeg1_index_run[0][run] + alevel  1; 
860 
/* store the vlc & sign at once */

861 
put_bits(&s>pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign); 
862 
} else {

863 
/* escape seems to be pretty rare <5% so i dont optimize it */

864 
put_bits(&s>pb, mpeg1_vlc[111/*rl>n*/][1], mpeg1_vlc[111/*rl>n*/][0]); 
865 
/* escape: only clip in this case */

866 
put_bits(&s>pb, 6, run);

867 
if(s>codec_id == CODEC_ID_MPEG1VIDEO){

868 
if (alevel < 128) { 
869 
put_bits(&s>pb, 8, level & 0xff); 
870 
} else {

871 
if (level < 0) { 
872 
put_bits(&s>pb, 16, 0x8001 + level + 255); 
873 
} else {

874 
put_bits(&s>pb, 16, level & 0xffff); 
875 
} 
876 
} 
877 
}else{

878 
put_bits(&s>pb, 12, level & 0xfff); 
879 
} 
880 
} 
881 
last_non_zero = i; 
882 
} 
883 
} 
884 
/* end of block */

885 
put_bits(&s>pb, 2, 0x2); 
886 
} 
887 
#endif //CONFIG_ENCODERS 
888  
889 
/******************************************/

890 
/* decoding */

891  
892 
static VLC dc_lum_vlc;

893 
static VLC dc_chroma_vlc;

894 
static VLC mv_vlc;

895 
static VLC mbincr_vlc;

896 
static VLC mb_ptype_vlc;

897 
static VLC mb_btype_vlc;

898 
static VLC mb_pat_vlc;

899  
900 
static void init_vlcs() 
901 
{ 
902 
static int done = 0; 
903  
904 
if (!done) {

905 
done = 1;

906  
907 
init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,

908 
vlc_dc_lum_bits, 1, 1, 
909 
vlc_dc_lum_code, 2, 2); 
910 
init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12,

911 
vlc_dc_chroma_bits, 1, 1, 
912 
vlc_dc_chroma_code, 2, 2); 
913 
init_vlc(&mv_vlc, MV_VLC_BITS, 17,

914 
&mbMotionVectorTable[0][1], 2, 1, 
915 
&mbMotionVectorTable[0][0], 2, 1); 
916 
init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36,

917 
&mbAddrIncrTable[0][1], 2, 1, 
918 
&mbAddrIncrTable[0][0], 2, 1); 
919 
init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 63,

920 
&mbPatTable[0][1], 2, 1, 
921 
&mbPatTable[0][0], 2, 1); 
922 

923 
init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,

924 
&table_mb_ptype[0][1], 2, 1, 
925 
&table_mb_ptype[0][0], 2, 1); 
926 
init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,

927 
&table_mb_btype[0][1], 2, 1, 
928 
&table_mb_btype[0][0], 2, 1); 
929 
init_rl(&rl_mpeg1); 
930 
init_rl(&rl_mpeg2); 
931  
932 
init_2d_vlc_rl(&rl_mpeg1); 
933 
init_2d_vlc_rl(&rl_mpeg2); 
934 
} 
935 
} 
936  
937 
static inline int get_dmv(MpegEncContext *s) 
938 
{ 
939 
if(get_bits1(&s>gb))

940 
return 1  (get_bits1(&s>gb) << 1); 
941 
else

942 
return 0; 
943 
} 
944  
945 
static inline int get_qscale(MpegEncContext *s) 
946 
{ 
947 
int qscale = get_bits(&s>gb, 5); 
948 
if (s>codec_id == CODEC_ID_MPEG2VIDEO) {

949 
if (s>q_scale_type) {

950 
return non_linear_qscale[qscale];

951 
} else {

952 
return qscale << 1; 
953 
} 
954 
} 
955 
return qscale;

956 
} 
957  
958 
/* motion type (for mpeg2) */

959 
#define MT_FIELD 1 
960 
#define MT_FRAME 2 
961 
#define MT_16X8 2 
962 
#define MT_DMV 3 
963  
964 
static int mpeg_decode_mb(MpegEncContext *s, 
965 
DCTELEM block[6][64]) 
966 
{ 
967 
int i, j, k, cbp, val, mb_type, motion_type;

968 

969 
dprintf("decode_mb: x=%d y=%d\n", s>mb_x, s>mb_y);

970  
971 
assert(s>mb_skiped==0);

972  
973 
if (s>mb_skip_run != 0) { 
974 
if(s>pict_type == I_TYPE){

975 
fprintf(stderr, "skiped MB in I frame at %d %d\n", s>mb_x, s>mb_y);

976 
return 1; 
977 
} 
978 

979 
/* skip mb */

980 
s>mb_intra = 0;

981 
for(i=0;i<6;i++) 
982 
s>block_last_index[i] = 1;

983 
s>mv_type = MV_TYPE_16X16; 
984 
if (s>pict_type == P_TYPE) {

985 
/* if P type, zero motion vector is implied */

986 
s>mv_dir = MV_DIR_FORWARD; 
987 
s>mv[0][0][0] = s>mv[0][0][1] = 0; 
988 
s>last_mv[0][0][0] = s>last_mv[0][0][1] = 0; 
989 
s>last_mv[0][1][0] = s>last_mv[0][1][1] = 0; 
990 
s>mb_skiped = 1;

991 
s>current_picture.mb_type[ s>mb_x + s>mb_y*s>mb_stride ]= MB_TYPE_SKIP  MB_TYPE_L0  MB_TYPE_16x16; 
992 
} else {

993 
/* if B type, reuse previous vectors and directions */

994 
s>mv[0][0][0] = s>last_mv[0][0][0]; 
995 
s>mv[0][0][1] = s>last_mv[0][0][1]; 
996 
s>mv[1][0][0] = s>last_mv[1][0][0]; 
997 
s>mv[1][0][1] = s>last_mv[1][0][1]; 
998  
999 
s>current_picture.mb_type[ s>mb_x + s>mb_y*s>mb_stride ]= 
1000 
s>current_picture.mb_type[ s>mb_x + s>mb_y*s>mb_stride  1]  MB_TYPE_SKIP;

1001 
// assert(s>current_picture.mb_type[ s>mb_x + s>mb_y*s>mb_stride  1]&(MB_TYPE_16x16MB_TYPE_16x8));

1002  
1003 
if((s>mv[0][0][0]s>mv[0][0][1]s>mv[1][0][0]s>mv[1][0][1])==0) 
1004 
s>mb_skiped = 1;

1005 
} 
1006  
1007 
return 0; 
1008 
} 
1009  
1010 
switch(s>pict_type) {

1011 
default:

1012 
case I_TYPE:

1013 
if (get_bits1(&s>gb) == 0) { 
1014 
if (get_bits1(&s>gb) == 0){ 
1015 
fprintf(stderr, "invalid mb type in I Frame at %d %d\n", s>mb_x, s>mb_y);

1016 
return 1; 
1017 
} 
1018 
mb_type = MB_TYPE_QUANT  MB_TYPE_INTRA; 
1019 
} else {

1020 
mb_type = MB_TYPE_INTRA; 
1021 
} 
1022 
break;

1023 
case P_TYPE:

1024 
mb_type = get_vlc2(&s>gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);

1025 
if (mb_type < 0){ 
1026 
fprintf(stderr, "invalid mb type in P Frame at %d %d\n", s>mb_x, s>mb_y);

1027 
return 1; 
1028 
} 
1029 
mb_type = ptype2mb_type[ mb_type ]; 
1030 
break;

1031 
case B_TYPE:

1032 
mb_type = get_vlc2(&s>gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);

1033 
if (mb_type < 0){ 
1034 
fprintf(stderr, "invalid mb type in B Frame at %d %d\n", s>mb_x, s>mb_y);

1035 
return 1; 
1036 
} 
1037 
mb_type = btype2mb_type[ mb_type ]; 
1038 
break;

1039 
} 
1040 
dprintf("mb_type=%x\n", mb_type);

1041 
// motion_type = 0; /* avoid warning */

1042 
if (IS_INTRA(mb_type)) {

1043 
/* compute dct type */

1044 
if (s>picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var? 
1045 
!s>frame_pred_frame_dct) { 
1046 
s>interlaced_dct = get_bits1(&s>gb); 
1047 
} 
1048  
1049 
if (IS_QUANT(mb_type))

1050 
s>qscale = get_qscale(s); 
1051 

1052 
if (s>concealment_motion_vectors) {

1053 
/* just parse them */

1054 
if (s>picture_structure != PICT_FRAME)

1055 
skip_bits1(&s>gb); /* field select */

1056 

1057 
s>mv[0][0][0]= s>last_mv[0][0][0]= s>last_mv[0][1][0] = 
1058 
mpeg_decode_motion(s, s>mpeg_f_code[0][0], s>last_mv[0][0][0]); 
1059 
s>mv[0][0][1]= s>last_mv[0][0][1]= s>last_mv[0][1][1] = 
1060 
mpeg_decode_motion(s, s>mpeg_f_code[0][1], s>last_mv[0][0][1]); 
1061  
1062 
skip_bits1(&s>gb); /* marker */

1063 
}else

1064 
memset(s>last_mv, 0, sizeof(s>last_mv)); /* reset mv prediction */ 
1065 
s>mb_intra = 1;

1066  
1067 
if (s>codec_id == CODEC_ID_MPEG2VIDEO) {

1068 
for(i=0;i<6;i++) { 
1069 
if (mpeg2_decode_block_intra(s, block[i], i) < 0) 
1070 
return 1; 
1071 
} 
1072 
} else {

1073 
for(i=0;i<6;i++) { 
1074 
if (mpeg1_decode_block_intra(s, block[i], i) < 0) 
1075 
return 1; 
1076 
} 
1077 
} 
1078 
} else {

1079 
if (mb_type & MB_TYPE_ZERO_MV){

1080 
assert(mb_type & MB_TYPE_PAT); 
1081  
1082 
/* compute dct type */

1083 
if (s>picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var? 
1084 
!s>frame_pred_frame_dct) { 
1085 
s>interlaced_dct = get_bits1(&s>gb); 
1086 
} 
1087  
1088 
if (IS_QUANT(mb_type))

1089 
s>qscale = get_qscale(s); 
1090  
1091 
s>mv_dir = MV_DIR_FORWARD; 
1092 
s>mv_type = MV_TYPE_16X16; 
1093 
s>last_mv[0][0][0] = 0; 
1094 
s>last_mv[0][0][1] = 0; 
1095 
s>last_mv[0][1][0] = 0; 
1096 
s>last_mv[0][1][1] = 0; 
1097 
s>mv[0][0][0] = 0; 
1098 
s>mv[0][0][1] = 0; 
1099 
}else{

1100 
assert(mb_type & MB_TYPE_L0L1); 
1101 
//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED

1102 
/* get additionnal motion vector type */

1103 
if (s>frame_pred_frame_dct)

1104 
motion_type = MT_FRAME; 
1105 
else{

1106 
motion_type = get_bits(&s>gb, 2);

1107 
} 
1108  
1109 
/* compute dct type */

1110 
if (s>picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var? 
1111 
!s>frame_pred_frame_dct && IS_PAT(mb_type)) { 
1112 
s>interlaced_dct = get_bits1(&s>gb); 
1113 
} 
1114  
1115 
if (IS_QUANT(mb_type))

1116 
s>qscale = get_qscale(s); 
1117  
1118 
/* motion vectors */

1119 
s>mv_dir = 0;

1120 
for(i=0;i<2;i++) { 
1121 
if (USES_LIST(mb_type, i)) {

1122 
s>mv_dir = (MV_DIR_FORWARD >> i); 
1123 
dprintf("motion_type=%d\n", motion_type);

1124 
switch(motion_type) {

1125 
case MT_FRAME: /* or MT_16X8 */ 
1126 
if (s>picture_structure == PICT_FRAME) {

1127 
/* MT_FRAME */

1128 
mb_type = MB_TYPE_16x16; 
1129 
s>mv_type = MV_TYPE_16X16; 
1130 
s>mv[i][0][0]= s>last_mv[i][0][0]= s>last_mv[i][1][0] = 
1131 
mpeg_decode_motion(s, s>mpeg_f_code[i][0], s>last_mv[i][0][0]); 
1132 
s>mv[i][0][1]= s>last_mv[i][0][1]= s>last_mv[i][1][1] = 
1133 
mpeg_decode_motion(s, s>mpeg_f_code[i][1], s>last_mv[i][0][1]); 
1134 
/* full_pel: only for mpeg1 */

1135 
if (s>full_pel[i]){

1136 
s>mv[i][0][0] <<= 1; 
1137 
s>mv[i][0][1] <<= 1; 
1138 
} 
1139 
} else {

1140 
/* MT_16X8 */

1141 
mb_type = MB_TYPE_16x8; 
1142 
s>mv_type = MV_TYPE_16X8; 
1143 
for(j=0;j<2;j++) { 
1144 
s>field_select[i][j] = get_bits1(&s>gb); 
1145 
for(k=0;k<2;k++) { 
1146 
val = mpeg_decode_motion(s, s>mpeg_f_code[i][k], 
1147 
s>last_mv[i][j][k]); 
1148 
s>last_mv[i][j][k] = val; 
1149 
s>mv[i][j][k] = val; 
1150 
} 
1151 
} 
1152 
} 
1153 
break;

1154 
case MT_FIELD:

1155 
s>mv_type = MV_TYPE_FIELD; 
1156 
if (s>picture_structure == PICT_FRAME) {

1157 
mb_type = MB_TYPE_16x8  MB_TYPE_INTERLACED; 
1158 
for(j=0;j<2;j++) { 
1159 
s>field_select[i][j] = get_bits1(&s>gb); 
1160 
val = mpeg_decode_motion(s, s>mpeg_f_code[i][0],

1161 
s>last_mv[i][j][0]);

1162 
s>last_mv[i][j][0] = val;

1163 
s>mv[i][j][0] = val;

1164 
dprintf("fmx=%d\n", val);

1165 
val = mpeg_decode_motion(s, s>mpeg_f_code[i][1],

1166 
s>last_mv[i][j][1] >> 1); 
1167 
s>last_mv[i][j][1] = val << 1; 
1168 
s>mv[i][j][1] = val;

1169 
dprintf("fmy=%d\n", val);

1170 
} 
1171 
} else {

1172 
mb_type = MB_TYPE_16x16; 
1173 
s>field_select[i][0] = get_bits1(&s>gb);

1174 
for(k=0;k<2;k++) { 
1175 
val = mpeg_decode_motion(s, s>mpeg_f_code[i][k], 
1176 
s>last_mv[i][0][k]);

1177 
s>last_mv[i][0][k] = val;

1178 
s>last_mv[i][1][k] = val;

1179 
s>mv[i][0][k] = val;

1180 
} 
1181 
} 
1182 
break;

1183 
case MT_DMV:

1184 
{ 
1185 
int dmx, dmy, mx, my, m;

1186  
1187 
mx = mpeg_decode_motion(s, s>mpeg_f_code[i][0],

1188 
s>last_mv[i][0][0]); 
1189 
s>last_mv[i][0][0] = mx; 
1190 
s>last_mv[i][1][0] = mx; 
1191 
dmx = get_dmv(s); 
1192 
my = mpeg_decode_motion(s, s>mpeg_f_code[i][1],

1193 
s>last_mv[i][0][1] >> 1); 
1194 
dmy = get_dmv(s); 
1195 
s>mv_type = MV_TYPE_DMV; 
1196  
1197  
1198 
s>last_mv[i][0][1] = my<<1; 
1199 
s>last_mv[i][1][1] = my<<1; 
1200  
1201 
s>mv[i][0][0] = mx; 
1202 
s>mv[i][0][1] = my; 
1203 
s>mv[i][1][0] = mx;//not used 
1204 
s>mv[i][1][1] = my;//not used 
1205  
1206 
if (s>picture_structure == PICT_FRAME) {

1207 
mb_type = MB_TYPE_16x16  MB_TYPE_INTERLACED; 
1208  
1209 
//m = 1 + 2 * s>top_field_first;

1210 
m = s>top_field_first ? 1 : 3; 
1211  
1212 
/* top > top pred */

1213 
s>mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx; 
1214 
s>mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy  1; 
1215 
m = 4  m;

1216 
s>mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx; 
1217 
s>mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1; 
1218 
} else {

1219 
mb_type = MB_TYPE_16x16; 
1220  
1221 
s>mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx; 
1222 
s>mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy; 
1223 
if(s>picture_structure == PICT_TOP_FIELD)

1224 
s>mv[i][2][1]; 
1225 
else

1226 
s>mv[i][2][1]++; 
1227 
} 
1228 
} 
1229 
break;

1230 
} 
1231 
} 
1232 
} 
1233 
} 
1234 

1235 
s>mb_intra = 0;

1236  
1237 
if (IS_PAT(mb_type)) {

1238 
cbp = get_vlc2(&s>gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);

1239 
if (cbp < 0){ 
1240 
fprintf(stderr, "invalid cbp at %d %d\n", s>mb_x, s>mb_y);

1241 
return 1; 
1242 
} 
1243 
cbp++; 
1244  
1245 
if (s>codec_id == CODEC_ID_MPEG2VIDEO) {

1246 
for(i=0;i<6;i++) { 
1247 
if (cbp & 32) { 
1248 
if (mpeg2_decode_block_non_intra(s, block[i], i) < 0) 
1249 
return 1; 
1250 
} else {

1251 
s>block_last_index[i] = 1;

1252 
} 
1253 
cbp+=cbp; 
1254 
} 
1255 
} else {

1256 
for(i=0;i<6;i++) { 
1257 
if (cbp & 32) { 
1258 
if (mpeg1_decode_block_inter(s, block[i], i) < 0) 
1259 
return 1; 
1260 
} else {

1261 
s>block_last_index[i] = 1;

1262 
} 
1263 
cbp+=cbp; 
1264 
} 
1265 
} 
1266 
}else{

1267 
for(i=0;i<6;i++) 
1268 
s>block_last_index[i] = 1;

1269 
} 
1270 
} 
1271  
1272 
s>current_picture.mb_type[ s>mb_x + s>mb_y*s>mb_stride ]= mb_type; 
1273  
1274 
return 0; 
1275 
} 
1276  
1277 
/* as h263, but only 17 codes */

1278 
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred) 
1279 
{ 
1280 
int code, sign, val, l, shift;

1281  
1282 
code = get_vlc2(&s>gb, mv_vlc.table, MV_VLC_BITS, 2);

1283 
if (code == 0) { 
1284 
return pred;

1285 
} 
1286 
if (code < 0) { 
1287 
return 0xffff; 
1288 
} 
1289  
1290 
sign = get_bits1(&s>gb); 
1291 
shift = fcode  1;

1292 
val = code; 
1293 
if (shift) {

1294 
val = (val  1) << shift;

1295 
val = get_bits(&s>gb, shift); 
1296 
val++; 
1297 
} 
1298 
if (sign)

1299 
val = val; 
1300 
val += pred; 
1301 

1302 
/* modulo decoding */

1303 
l = 1 << (shift+4); 
1304 
val = ((val + l)&(l*21))  l; 
1305 
return val;

1306 
} 
1307  
1308 
static inline int decode_dc(GetBitContext *gb, int component) 
1309 
{ 
1310 
int code, diff;

1311  
1312 
if (component == 0) { 
1313 
code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);

1314 
} else {

1315 
code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);

1316 
} 
1317 
if (code < 0){ 
1318 
fprintf(stderr, "invalid dc code at\n");

1319 
return 0xffff; 
1320 
} 
1321 
if (code == 0) { 
1322 
diff = 0;

1323 
} else {

1324 
diff = get_xbits(gb, code); 
1325 
} 
1326 
return diff;

1327 
} 
1328  
1329 
static inline int mpeg1_decode_block_intra(MpegEncContext *s, 
1330 
DCTELEM *block, 
1331 
int n)

1332 
{ 
1333 
int level, dc, diff, i, j, run;

1334 
int component;

1335 
RLTable *rl = &rl_mpeg1; 
1336 
uint8_t * const scantable= s>intra_scantable.permutated;

1337 
const uint16_t *quant_matrix= s>intra_matrix;

1338 
const int qscale= s>qscale; 
1339  
1340 
/* DC coef */

1341 
component = (n <= 3 ? 0 : n  4 + 1); 
1342 
diff = decode_dc(&s>gb, component); 
1343 
if (diff >= 0xffff) 
1344 
return 1; 
1345 
dc = s>last_dc[component]; 
1346 
dc += diff; 
1347 
s>last_dc[component] = dc; 
1348 
block[0] = dc<<3; 
1349 
dprintf("dc=%d diff=%d\n", dc, diff);

1350 
i = 0;

1351 
{ 
1352 
OPEN_READER(re, &s>gb); 
1353 
/* now quantify & encode AC coefs */

1354 
for(;;) {

1355 
UPDATE_CACHE(re, &s>gb); 
1356 
GET_RL_VLC(level, run, re, &s>gb, rl>rl_vlc[0], TEX_VLC_BITS, 2); 
1357 

1358 
if(level == 127){ 
1359 
break;

1360 
} else if(level != 0) { 
1361 
i += run; 
1362 
j = scantable[i]; 
1363 
level= (level*qscale*quant_matrix[j])>>3;

1364 
level= (level1)1; 
1365 
level = (level ^ SHOW_SBITS(re, &s>gb, 1))  SHOW_SBITS(re, &s>gb, 1); 
1366 
LAST_SKIP_BITS(re, &s>gb, 1);

1367 
} else {

1368 
/* escape */

1369 
run = SHOW_UBITS(re, &s>gb, 6)+1; LAST_SKIP_BITS(re, &s>gb, 6); 
1370 
UPDATE_CACHE(re, &s>gb); 
1371 
level = SHOW_SBITS(re, &s>gb, 8); SKIP_BITS(re, &s>gb, 8); 
1372 
if (level == 128) { 
1373 
level = SHOW_UBITS(re, &s>gb, 8)  256; LAST_SKIP_BITS(re, &s>gb, 8); 
1374 
} else if (level == 0) { 
1375 
level = SHOW_UBITS(re, &s>gb, 8) ; LAST_SKIP_BITS(re, &s>gb, 8); 
1376 
} 
1377 
i += run; 
1378 
j = scantable[i]; 
1379 
if(level<0){ 
1380 
level= level; 
1381 
level= (level*qscale*quant_matrix[j])>>3;

1382 
level= (level1)1; 
1383 
level= level; 
1384 
}else{

1385 
level= (level*qscale*quant_matrix[j])>>3;

1386 
level= (level1)1; 
1387 
} 
1388 
} 
1389 
if (i > 63){ 
1390 
fprintf(stderr, "actex damaged at %d %d\n", s>mb_x, s>mb_y);

1391 
return 1; 
1392 
} 
1393  
1394 
block[j] = level; 
1395 
} 
1396 
CLOSE_READER(re, &s>gb); 
1397 
} 
1398 
s>block_last_index[n] = i; 
1399 
return 0; 
1400 
} 
1401  
1402 
static inline int mpeg1_decode_block_inter(MpegEncContext *s, 
1403 
DCTELEM *block, 
1404 
int n)

1405 
{ 
1406 
int level, i, j, run;

1407 
RLTable *rl = &rl_mpeg1; 
1408 
uint8_t * const scantable= s>intra_scantable.permutated;

1409 
const uint16_t *quant_matrix= s>inter_matrix;

1410 
const int qscale= s>qscale; 
1411  
1412 
{ 
1413 
int v;

1414 
OPEN_READER(re, &s>gb); 
1415 
i = 1;

1416 
/* special case for the first coef. no need to add a second vlc table */

1417 
UPDATE_CACHE(re, &s>gb); 
1418 
v= SHOW_UBITS(re, &s>gb, 2);

1419 
if (v & 2) { 
1420 
LAST_SKIP_BITS(re, &s>gb, 2);

1421 
level= (3*qscale*quant_matrix[0])>>4; 
1422 
level= (level1)1; 
1423 
if(v&1) 
1424 
level= level; 
1425 
block[0] = level;

1426 
i++; 
1427 
} 
1428  
1429 
/* now quantify & encode AC coefs */

1430 
for(;;) {

1431 
UPDATE_CACHE(re, &s>gb); 
1432 
GET_RL_VLC(level, run, re, &s>gb, rl>rl_vlc[0], TEX_VLC_BITS, 2); 
1433 

1434 
if(level == 127){ 
1435 
break;

1436 
} else if(level != 0) { 
1437 
i += run; 
1438 
j = scantable[i]; 
1439 
level= ((level*2+1)*qscale*quant_matrix[j])>>4; 
1440 
level= (level1)1; 
1441 
level = (level ^ SHOW_SBITS(re, &s>gb, 1))  SHOW_SBITS(re, &s>gb, 1); 
1442 
LAST_SKIP_BITS(re, &s>gb, 1);

1443 
} else {

1444 
/* escape */

1445 
run = SHOW_UBITS(re, &s>gb, 6)+1; LAST_SKIP_BITS(re, &s>gb, 6); 
1446 
UPDATE_CACHE(re, &s>gb); 
1447 
level = SHOW_SBITS(re, &s>gb, 8); SKIP_BITS(re, &s>gb, 8); 
1448 
if (level == 128) { 
1449 
level = SHOW_UBITS(re, &s>gb, 8)  256; LAST_SKIP_BITS(re, &s>gb, 8); 
1450 
} else if (level == 0) { 
1451 
level = SHOW_UBITS(re, &s>gb, 8) ; LAST_SKIP_BITS(re, &s>gb, 8); 
1452 
} 
1453 
i += run; 
1454 
j = scantable[i]; 
1455 
if(level<0){ 
1456 
level= level; 
1457 
level= ((level*2+1)*qscale*quant_matrix[j])>>4; 
1458 
level= (level1)1; 
1459 
level= level; 
1460 
}else{

1461 
level= ((level*2+1)*qscale*quant_matrix[j])>>4; 
1462 
level= (level1)1; 
1463 
} 
1464 
} 
1465 
if (i > 63){ 
1466 
fprintf(stderr, "actex damaged at %d %d\n", s>mb_x, s>mb_y);

1467 
return 1; 
1468 
} 
1469  
1470 
block[j] = level; 
1471 
} 
1472 
CLOSE_READER(re, &s>gb); 
1473 
} 
1474 
s>block_last_index[n] = i; 
1475 
return 0; 
1476 
} 
1477  
1478 
/* Also does unquantization here, since I will never support mpeg2

1479 
encoding */

1480 
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
1481 
DCTELEM *block, 
1482 
int n)

1483 
{ 
1484 
int level, i, j, run;

1485 
RLTable *rl = &rl_mpeg1; 
1486 
uint8_t * const scantable= s>intra_scantable.permutated;

1487 
const uint16_t *quant_matrix;

1488 
const int qscale= s>qscale; 
1489 
int mismatch;

1490  
1491 
mismatch = 1;

1492  
1493 
{ 
1494 
int v;

1495 
OPEN_READER(re, &s>gb); 
1496 
i = 1;

1497 
if (n < 4) 
1498 
quant_matrix = s>inter_matrix; 
1499 
else

1500 
quant_matrix = s>chroma_inter_matrix; 
1501  
1502 
/* special case for the first coef. no need to add a second vlc table */

1503 
UPDATE_CACHE(re, &s>gb); 
1504 
v= SHOW_UBITS(re, &s>gb, 2);

1505 
if (v & 2) { 
1506 
LAST_SKIP_BITS(re, &s>gb, 2);

1507 
level= (3*qscale*quant_matrix[0])>>5; 
1508 
if(v&1) 
1509 
level= level; 
1510 
block[0] = level;

1511 
mismatch ^= level; 
1512 
i++; 
1513 
} 
1514  
1515 
/* now quantify & encode AC coefs */

1516 
for(;;) {

1517 
UPDATE_CACHE(re, &s>gb); 
1518 
GET_RL_VLC(level, run, re, &s>gb, rl>rl_vlc[0], TEX_VLC_BITS, 2); 
1519 

1520 
if(level == 127){ 
1521 
break;

1522 
} else if(level != 0) { 
1523 
i += run; 
1524 
j = scantable[i]; 
1525 
level= ((level*2+1)*qscale*quant_matrix[j])>>5; 
1526 
level = (level ^ SHOW_SBITS(re, &s>gb, 1))  SHOW_SBITS(re, &s>gb, 1); 
1527 
LAST_SKIP_BITS(re, &s>gb, 1);

1528 
} else {

1529 
/* escape */

1530 
run = SHOW_UBITS(re, &s>gb, 6)+1; LAST_SKIP_BITS(re, &s>gb, 6); 
1531 
UPDATE_CACHE(re, &s>gb); 
1532 
level = SHOW_SBITS(re, &s>gb, 12); SKIP_BITS(re, &s>gb, 12); 
1533  
1534 
i += run; 
1535 
j = scantable[i]; 
1536 
if(level<0){ 
1537 
level= ((level*2+1)*qscale*quant_matrix[j])>>5; 
1538 
level= level; 
1539 
}else{

1540 
level= ((level*2+1)*qscale*quant_matrix[j])>>5; 
1541 
} 
1542 
} 
1543 
if (i > 63){ 
1544 
fprintf(stderr, "actex damaged at %d %d\n", s>mb_x, s>mb_y);

1545 
return 1; 
1546 
} 
1547 

1548 
mismatch ^= level; 
1549 
block[j] = level; 
1550 
} 
1551 
CLOSE_READER(re, &s>gb); 
1552 
} 
1553 
block[63] ^= (mismatch & 1); 
1554 

1555 
s>block_last_index[n] = i; 
1556 
return 0; 
1557 
} 
1558  
1559 
static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
1560 
DCTELEM *block, 
1561 
int n)

1562 
{ 
1563 
int level, dc, diff, i, j, run;

1564 
int component;

1565 
RLTable *rl; 
1566 
uint8_t * const scantable= s>intra_scantable.permutated;

1567 
const uint16_t *quant_matrix;

1568 
const int qscale= s>qscale; 
1569 
int mismatch;

1570  
1571 
/* DC coef */

1572 
if (n < 4){ 
1573 
quant_matrix = s>intra_matrix; 
1574 
component = 0;

1575 
}else{

1576 
quant_matrix = s>chroma_intra_matrix; 
1577 
component = n  3;

1578 
} 
1579 
diff = decode_dc(&s>gb, component); 
1580 
if (diff >= 0xffff) 
1581 
return 1; 
1582 
dc = s>last_dc[component]; 
1583 
dc += diff; 
1584 
s>last_dc[component] = dc; 
1585 
block[0] = dc << (3  s>intra_dc_precision); 
1586 
dprintf("dc=%d\n", block[0]); 
1587 
mismatch = block[0] ^ 1; 
1588 
i = 0;

1589 
if (s>intra_vlc_format)

1590 
rl = &rl_mpeg2; 
1591 
else

1592 
rl = &rl_mpeg1; 
1593  
1594 
{ 
1595 
OPEN_READER(re, &s>gb); 
1596 
/* now quantify & encode AC coefs */

1597 
for(;;) {

1598 
UPDATE_CACHE(re, &s>gb); 
1599 
GET_RL_VLC(level, run, re, &s>gb, rl>rl_vlc[0], TEX_VLC_BITS, 2); 
1600 

1601 
if(level == 127){ 
1602 
break;

1603 
} else if(level != 0) { 
1604 
i += run; 
1605 
j = scantable[i]; 
1606 
level= (level*qscale*quant_matrix[j])>>4;

1607 
level = (level ^ SHOW_SBITS(re, &s>gb, 1))  SHOW_SBITS(re, &s>gb, 1); 
1608 
LAST_SKIP_BITS(re, &s>gb, 1);

1609 
} else {

1610 
/* escape */

1611 
run = SHOW_UBITS(re, &s>gb, 6)+1; LAST_SKIP_BITS(re, &s>gb, 6); 
1612 
UPDATE_CACHE(re, &s>gb); 
1613 
level = SHOW_SBITS(re, &s>gb, 12); SKIP_BITS(re, &s>gb, 12); 
1614 
i += run; 
1615 
j = scantable[i]; 
1616 
if(level<0){ 
1617 
level= (level*qscale*quant_matrix[j])>>4;

1618 
level= level; 
1619 
}else{

1620 
level= (level*qscale*quant_matrix[j])>>4;

1621 
} 
1622 
} 
1623 
if (i > 63){ 
1624 
fprintf(stderr, "actex damaged at %d %d\n", s>mb_x, s>mb_y);

1625 
return 1; 
1626 
} 
1627 

1628 
mismatch^= level; 
1629 
block[j] = level; 
1630 
} 
1631 
CLOSE_READER(re, &s>gb); 
1632 
} 
1633 
block[63]^= mismatch&1; 
1634 

1635 
s>block_last_index[n] = i; 
1636 
return 0; 
1637 
} 
1638  
1639 
typedef struct Mpeg1Context { 
1640 
MpegEncContext mpeg_enc_ctx; 
1641 
int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ 
1642 
int repeat_field; /* true if we must repeat the field */ 
1643 
} Mpeg1Context; 
1644  
1645 
static int mpeg_decode_init(AVCodecContext *avctx) 
1646 
{ 
1647 
Mpeg1Context *s = avctx>priv_data; 
1648 

1649 
s>mpeg_enc_ctx.flags= avctx>flags; 
1650 
common_init(&s>mpeg_enc_ctx); 
1651 
init_vlcs(); 
1652  
1653 
s>mpeg_enc_ctx_allocated = 0;

1654 
s>mpeg_enc_ctx.picture_number = 0;

1655 
s>repeat_field = 0;

1656 
s>mpeg_enc_ctx.codec_id= avctx>codec>id; 
1657 
return 0; 
1658 
} 
1659  
1660 
/* return the 8 bit start code value and update the search

1661 
state. Return 1 if no start code found */

1662 
static int find_start_code(uint8_t **pbuf_ptr, uint8_t *buf_end) 
1663 
{ 
1664 
uint8_t *buf_ptr; 
1665 
unsigned int state=0xFFFFFFFF, v; 
1666 
int val;

1667  
1668 
buf_ptr = *pbuf_ptr; 
1669 
while (buf_ptr < buf_end) {

1670 
v = *buf_ptr++; 
1671 
if (state == 0x000001) { 
1672 
state = ((state << 8)  v) & 0xffffff; 
1673 
val = state; 
1674 
goto found;

1675 
} 
1676 
state = ((state << 8)  v) & 0xffffff; 
1677 
} 
1678 
val = 1;

1679 
found:

1680 
*pbuf_ptr = buf_ptr; 
1681 
return val;

1682 
} 
1683  
1684 
static int mpeg1_decode_picture(AVCodecContext *avctx, 
1685 
uint8_t *buf, int buf_size)

1686 
{ 
1687 
Mpeg1Context *s1 = avctx>priv_data; 
1688 
MpegEncContext *s = &s1>mpeg_enc_ctx; 
1689 
int ref, f_code;

1690  
1691 
init_get_bits(&s>gb, buf, buf_size*8);

1692  
1693 
ref = get_bits(&s>gb, 10); /* temporal ref */ 
1694 
s>pict_type = get_bits(&s>gb, 3);

1695 
dprintf("pict_type=%d number=%d\n", s>pict_type, s>picture_number);

1696  
1697 
skip_bits(&s>gb, 16);

1698 
if (s>pict_type == P_TYPE  s>pict_type == B_TYPE) {

1699 
s>full_pel[0] = get_bits1(&s>gb);

1700 
f_code = get_bits(&s>gb, 3);

1701 
if (f_code == 0) 
1702 
return 1; 
1703 
s>mpeg_f_code[0][0] = f_code; 
1704 
s>mpeg_f_code[0][1] = f_code; 
1705 
} 
1706 
if (s>pict_type == B_TYPE) {

1707 
s>full_pel[1] = get_bits1(&s>gb);

1708 
f_code = get_bits(&s>gb, 3);

1709 
if (f_code == 0) 
1710 
return 1; 
1711 
s>mpeg_f_code[1][0] = f_code; 
1712 
s>mpeg_f_code[1][1] = f_code; 
1713 
} 
1714 
s>current_picture.pict_type= s>pict_type; 
1715 
s>current_picture.key_frame= s>pict_type == I_TYPE; 
1716 

1717 
s>y_dc_scale = 8;

1718 
s>c_dc_scale = 8;

1719 
s>first_slice = 1;

1720 
return 0; 
1721 
} 
1722  
1723 
static void mpeg_decode_sequence_extension(MpegEncContext *s) 
1724 
{ 
1725 
int horiz_size_ext, vert_size_ext;

1726 
int bit_rate_ext, vbv_buf_ext;

1727 
int frame_rate_ext_n, frame_rate_ext_d;

1728 
int level, profile;

1729 
float aspect;

1730  
1731 
skip_bits(&s>gb, 1); /* profil and level esc*/ 
1732 
profile= get_bits(&s>gb, 3);

1733 
level= get_bits(&s>gb, 4);

1734 
s>progressive_sequence = get_bits1(&s>gb); /* progressive_sequence */

1735 
skip_bits(&s>gb, 2); /* chroma_format */ 
1736 
horiz_size_ext = get_bits(&s>gb, 2);

1737 
vert_size_ext = get_bits(&s>gb, 2);

1738 
s>width = (horiz_size_ext << 12);

1739 
s>height = (vert_size_ext << 12);

1740 
bit_rate_ext = get_bits(&s>gb, 12); /* XXX: handle it */ 
1741 
s>bit_rate = ((s>bit_rate / 400)  (bit_rate_ext << 12)) * 400; 
1742 
skip_bits1(&s>gb); /* marker */

1743 
vbv_buf_ext = get_bits(&s>gb, 8);

1744  
1745 
s>low_delay = get_bits1(&s>gb); 
1746 
if(s>flags & CODEC_FLAG_LOW_DELAY) s>low_delay=1; 
1747  
1748 
frame_rate_ext_n = get_bits(&s>gb, 2);

1749 
frame_rate_ext_d = get_bits(&s>gb, 5);

1750 
av_reduce( 
1751 
&s>avctx>frame_rate, 
1752 
&s>avctx>frame_rate_base, 
1753 
frame_rate_tab[s>frame_rate_index] * (frame_rate_ext_n+1),

1754 
MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d+1),

1755 
1<<30); 
1756  
1757 
dprintf("sequence extension\n");

1758 
s>codec_id= s>avctx>codec_id= CODEC_ID_MPEG2VIDEO; 
1759 
s>avctx>sub_id = 2; /* indicates mpeg2 found */ 
1760  
1761 
aspect= mpeg2_aspect[s>aspect_ratio_info]; 
1762 
if(aspect>0.0) s>avctx>aspect_ratio= s>width/(aspect*s>height); 
1763 
else if(aspect<0.0) s>avctx>aspect_ratio= 1.0/aspect; 
1764 

1765 
if(s>avctx>debug & FF_DEBUG_PICT_INFO)

1766 
printf("profile: %d, level: %d \n", profile, level);

1767 
} 
1768  
1769 
static void mpeg_decode_quant_matrix_extension(MpegEncContext *s) 
1770 
{ 
1771 
int i, v, j;

1772  
1773 
dprintf("matrix extension\n");

1774  
1775 
if (get_bits1(&s>gb)) {

1776 
for(i=0;i<64;i++) { 
1777 
v = get_bits(&s>gb, 8);

1778 
j= s>dsp.idct_permutation[ ff_zigzag_direct[i] ]; 
1779 
s>intra_matrix[j] = v; 
1780 
s>chroma_intra_matrix[j] = v; 
1781 
} 
1782 
} 
1783 
if (get_bits1(&s>gb)) {

1784 
for(i=0;i<64;i++) { 
1785 
v = get_bits(&s>gb, 8);

1786 
j= s>dsp.idct_permutation[ ff_zigzag_direct[i] ]; 
1787 
s>inter_matrix[j] = v; 
1788 
s>chroma_inter_matrix[j] = v; 
1789 
} 
1790 
} 
1791 
if (get_bits1(&s>gb)) {

1792 
for(i=0;i<64;i++) { 
1793 
v = get_bits(&s>gb, 8);

1794 
j= s>dsp.idct_permutation[ ff_zigzag_direct[i] ]; 
1795 
s>chroma_intra_matrix[j] = v; 
1796 
} 
1797 
} 
1798 
if (get_bits1(&s>gb)) {

1799 
for(i=0;i<64;i++) { 
1800 
v = get_bits(&s>gb, 8);

1801 
j= s>dsp.idct_permutation[ ff_zigzag_direct[i] ]; 
1802 
s>chroma_inter_matrix[j] = v; 
1803 
} 
1804 
} 
1805 
} 
1806  
1807 
static void mpeg_decode_picture_coding_extension(MpegEncContext *s) 
1808 
{ 
1809 
s>full_pel[0] = s>full_pel[1] = 0; 
1810 
s>mpeg_f_code[0][0] = get_bits(&s>gb, 4); 
1811 
s>mpeg_f_code[0][1] = get_bits(&s>gb, 4); 
1812 
s>mpeg_f_code[1][0] = get_bits(&s>gb, 4); 
1813 
s>mpeg_f_code[1][1] = get_bits(&s>gb, 4); 
1814 
s>intra_dc_precision = get_bits(&s>gb, 2);

1815 
s>picture_structure = get_bits(&s>gb, 2);

1816 
s>top_field_first = get_bits1(&s>gb); 
1817 
s>frame_pred_frame_dct = get_bits1(&s>gb); 
1818 
s>concealment_motion_vectors = get_bits1(&s>gb); 
1819 
s>q_scale_type = get_bits1(&s>gb); 
1820 
s>intra_vlc_format = get_bits1(&s>gb); 
1821 
s>alternate_scan = get_bits1(&s>gb); 
1822 
s>repeat_first_field = get_bits1(&s>gb); 
1823 
s>chroma_420_type = get_bits1(&s>gb); 
1824 
s>progressive_frame = get_bits1(&s>gb); 
1825 

1826 
if(s>picture_structure == PICT_FRAME)

1827 
s>first_field=0;

1828 
else{

1829 
s>first_field ^= 1;

1830 
memset(s>mbskip_table, 0, s>mb_stride*s>mb_height);

1831 
} 
1832 

1833 
if(s>alternate_scan){

1834 
ff_init_scantable(s>dsp.idct_permutation, &s>inter_scantable , ff_alternate_vertical_scan); 
1835 
ff_init_scantable(s>dsp.idct_permutation, &s>intra_scantable , ff_alternate_vertical_scan); 
1836 
ff_init_scantable(s>dsp.idct_permutation, &s>intra_h_scantable, ff_alternate_vertical_scan); 
1837 
ff_init_scantable(s>dsp.idct_permutation, &s>intra_v_scantable, ff_alternate_vertical_scan); 
1838 
}else{

1839 
ff_init_scantable(s>dsp.idct_permutation, &s>inter_scantable , ff_zigzag_direct); 
1840 
ff_init_scantable(s>dsp.idct_permutation, &s>intra_scantable , ff_zigzag_direct); 
1841 
ff_init_scantable(s>dsp.idct_permutation, &s>intra_h_scantable, ff_alternate_horizontal_scan); 
1842 
ff_init_scantable(s>dsp.idct_permutation, &s>intra_v_scantable, ff_alternate_vertical_scan); 
1843 
} 
1844 

1845 
/* composite display not parsed */

1846 
dprintf("intra_dc_precision=%d\n", s>intra_dc_precision);

1847 
dprintf("picture_structure=%d\n", s>picture_structure);

1848 
dprintf("top field first=%d\n", s>top_field_first);

1849 
dprintf("repeat first field=%d\n", s>repeat_first_field);

1850 
dprintf("conceal=%d\n", s>concealment_motion_vectors);

1851 
dprintf("intra_vlc_format=%d\n", s>intra_vlc_format);

1852 
dprintf("alternate_scan=%d\n", s>alternate_scan);

1853 
dprintf("frame_pred_frame_dct=%d\n", s>frame_pred_frame_dct);

1854 
dprintf("progressive_frame=%d\n", s>progressive_frame);

1855 
} 
1856  
1857 
static void mpeg_decode_extension(AVCodecContext *avctx, 
1858 
uint8_t *buf, int buf_size)

1859 
{ 
1860 
Mpeg1Context *s1 = avctx>priv_data; 
1861 
MpegEncContext *s = &s1>mpeg_enc_ctx; 
1862 
int ext_type;

1863  
1864 
init_get_bits(&s>gb, buf, buf_size*8);

1865 

1866 
ext_type = get_bits(&s>gb, 4);

1867 
switch(ext_type) {

1868 
case 0x1: 
1869 
/* sequence ext */

1870 
mpeg_decode_sequence_extension(s); 
1871 
break;

1872 
case 0x3: 
1873 
/* quant matrix extension */

1874 
mpeg_decode_quant_matrix_extension(s); 
1875 
break;

1876 
case 0x8: 
1877 
/* picture extension */

1878 
mpeg_decode_picture_coding_extension(s); 
1879 
break;

1880 
} 
1881 
} 
1882  
1883 
static void exchange_uv(AVFrame *f){ 
1884 
uint8_t *t= f>data[1];

1885 
f>data[1]= f>data[2]; 
1886 
f>data[2]= t;

1887 
} 
1888  
1889 
#define DECODE_SLICE_FATAL_ERROR 2 
1890 
#define DECODE_SLICE_ERROR 1 
1891 
#define DECODE_SLICE_OK 0 
1892  
1893 
/**

1894 
* decodes a slice.

1895 
* @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br>

1896 
* DECODE_SLICE_ERROR if the slice is damaged<br>

1897 
* DECODE_SLICE_OK if this slice is ok<br>

1898 
*/

1899 
static int mpeg_decode_slice(AVCodecContext *avctx, 
1900 
AVFrame *pict, 
1901 
int start_code,

1902 
uint8_t **buf, int buf_size)

1903 
{ 
1904 
Mpeg1Context *s1 = avctx>priv_data; 
1905 
MpegEncContext *s = &s1>mpeg_enc_ctx; 
1906 
int ret;

1907 
const int field_pic= s>picture_structure != PICT_FRAME; 
1908  
1909 
s>resync_mb_x= s>mb_x = 
1910 
s>resync_mb_y= s>mb_y = 1;

1911 

1912 
start_code = (start_code  1) & 0xff; 
1913 
if (start_code >= s>mb_height){

1914 
fprintf(stderr, "slice below image (%d >= %d)\n", start_code, s>mb_height);

1915 
return 1; 
1916 
} 
1917 

1918 
ff_mpeg1_clean_buffers(s); 
1919 
s>interlaced_dct = 0;

1920 

1921 
/* start frame decoding */

1922 
if (s>first_slice) {

1923 
if(s>first_field  s>picture_structure==PICT_FRAME){

1924 
if(MPV_frame_start(s, avctx) < 0) 
1925 
return DECODE_SLICE_FATAL_ERROR;

1926  
1927 
ff_er_frame_start(s); 
1928  
1929 
/* first check if we must repeat the frame */

1930 
s>current_picture_ptr>repeat_pict = 0;

1931 
if (s>repeat_first_field) {

1932 
if (s>progressive_sequence) {

1933 
if (s>top_field_first)

1934 
s>current_picture_ptr>repeat_pict = 4;

1935 
else

1936 
s>current_picture_ptr>repeat_pict = 2;

1937 
} else if (s>progressive_frame) { 
1938 
s>current_picture_ptr>repeat_pict = 1;

1939 
} 
1940 
} 
1941 
//printf("%d\n", s>current_picture_ptr>repeat_pict);

1942  
1943 
if(s>avctx>debug&FF_DEBUG_PICT_INFO){

1944 
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",

1945 
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], 
1946 
s>pict_type == I_TYPE ? "I" : (s>pict_type == P_TYPE ? "P" : (s>pict_type == B_TYPE ? "B" : "S")), 
1947 
s>progressive_sequence ? "pro" :"", s>alternate_scan ? "alt" :"", s>top_field_first ? "top" :"", 
1948 
s>intra_dc_precision, s>picture_structure, s>frame_pred_frame_dct, s>concealment_motion_vectors, 
1949 
s>q_scale_type, s>intra_vlc_format, s>repeat_first_field, s>chroma_420_type ? "420" :""); 
1950 
} 
1951 
}else{ //second field 
1952 
int i;

1953 

1954 
if(!s>current_picture_ptr){

1955 
fprintf(stderr, "first field missing\n");

1956 
return 1; 
1957 
} 
1958 

1959 
for(i=0; i<4; i++){ 
1960 
s>current_picture.data[i] = s>current_picture_ptr>data[i]; 
1961 
if(s>picture_structure == PICT_BOTTOM_FIELD){

1962 
s>current_picture.data[i] += s>current_picture_ptr>linesize[i]; 
1963 
} 
1964 
} 
1965 
} 
1966 
#ifdef HAVE_XVMC

1967 
// MPV_frame_start will call this function too,

1968 
// but we need to call it on every field

1969 
if(s>avctx>xvmc_acceleration)

1970 
XVMC_field_start(s,avctx); 
1971 
#endif

1972 
}//fi(s>first_slice)

1973 
s>first_slice = 0;

1974  
1975 
init_get_bits(&s>gb, *buf, buf_size*8);

1976  
1977 
s>qscale = get_qscale(s); 
1978 
if(s>qscale == 0){ 
1979 
fprintf(stderr, "qscale == 0\n");

1980 
return 1; 
1981 
} 
1982 

1983 
/* extra slice info */

1984 
while (get_bits1(&s>gb) != 0) { 
1985 
skip_bits(&s>gb, 8);

1986 
} 
1987 

1988 
s>mb_x=0;

1989  
1990 
for(;;) {

1991 
int code = get_vlc2(&s>gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2); 
1992 
if (code < 0){ 
1993 
fprintf(stderr, "first mb_incr damaged\n");

1994 
return 1; 
1995 
} 
1996 
if (code >= 33) { 
1997 
if (code == 33) { 
1998 
s>mb_x += 33;

1999 
} 
2000 
/* otherwise, stuffing, nothing to do */

2001 
} else {

2002 
s>mb_x += code; 
2003 
break;

2004 
} 
2005 
} 
2006 

2007 
s>resync_mb_x= s>mb_x; 
2008 
s>resync_mb_y= s>mb_y = start_code; 
2009 
s>mb_skip_run= 0;

2010 
ff_init_block_index(s); 
2011  
2012 
for(;;) {

2013 
s>dsp.clear_blocks(s>block[0]);

2014  
2015 
ret = mpeg_decode_mb(s, s>block); 
2016  
2017 
dprintf("ret=%d\n", ret);

2018 
if (ret < 0) 
2019 
return 1; 
2020 

2021 
if(s>motion_val && s>pict_type != B_TYPE){ //note motion_val is normally NULL unless we want to extract the MVs 
2022 
const int wrap = s>block_wrap[0]; 
2023 
const int xy = s>mb_x*2 + 1 + (s>mb_y*2 +1)*wrap; 
2024 
int motion_x, motion_y;

2025  
2026 
if (s>mb_intra) {

2027 
motion_x = motion_y = 0;

2028 
}else if (s>mv_type == MV_TYPE_16X16) { 
2029 
motion_x = s>mv[0][0][0]; 
2030 
motion_y = s>mv[0][0][1]; 
2031 
} else /*if (s>mv_type == MV_TYPE_FIELD)*/ { 
2032 
motion_x = s>mv[0][0][0] + s>mv[0][1][0]; 
2033 
motion_y = s>mv[0][0][1] + s>mv[0][1][1]; 
2034 
motion_x = (motion_x>>1)  (motion_x&1); 
2035 
} 
2036 
s>motion_val[xy][0] = motion_x;

2037 
s>motion_val[xy][1] = motion_y;

2038 
s>motion_val[xy + 1][0] = motion_x; 
2039 
s>motion_val[xy + 1][1] = motion_y; 
2040 
s>motion_val[xy + wrap][0] = motion_x;

2041 
s>motion_val[xy + wrap][1] = motion_y;

2042 
s>motion_val[xy + 1 + wrap][0] = motion_x; 
2043 
s>motion_val[xy + 1 + wrap][1] = motion_y; 
2044 
} 
2045 

2046 
s>dest[0] += 16; 
2047 
s>dest[1] += 8; 
2048 
s>dest[2] += 8; 
2049  
2050 
MPV_decode_mb(s, s>block); 
2051 

2052 
if (++s>mb_x >= s>mb_width) {

2053 
if(s>avctx>codec_tag == ff_get_fourcc("VCR2")) 
2054 
exchange_uv((AVFrame*)s>current_picture_ptr); 
2055  
2056 
ff_draw_horiz_band(s, 16*s>mb_y, 16); 
2057  
2058 
if(s>avctx>codec_tag == ff_get_fourcc("VCR2")) 
2059 
exchange_uv((AVFrame*)s>current_picture_ptr); 
2060  
2061 
s>mb_x = 0;

2062 
s>mb_y++; 
2063  
2064 
if(s>mb_y<<field_pic >= s>mb_height){

2065 
int left= s>gb.size_in_bits  get_bits_count(&s>gb);

2066  
2067 
if(left < 0  (left && show_bits(&s>gb, FFMIN(left, 23))) 
2068 
 (avctx>error_resilience >= FF_ER_AGGRESSIVE && left>8)){

2069 
fprintf(stderr, "end missmatch left=%d\n", left);

2070 
return 1; 
2071 
}else

2072 
goto eos;

2073 
} 
2074 

2075 
ff_init_block_index(s); 
2076 
} 
2077  
2078 
/* skip mb handling */

2079 
if (s>mb_skip_run == 1) { 
2080 
/* read again increment */

2081 
s>mb_skip_run = 0;

2082 
for(;;) {

2083 
int code = get_vlc2(&s>gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2); 
2084 
if (code < 0){ 
2085 
fprintf(stderr, "mb incr damaged\n");

2086 
return 1; 
2087 
} 
2088 
if (code >= 33) { 
2089 
if (code == 33) { 
2090 
s>mb_skip_run += 33;

2091 
}else if(code == 35){ 
2092 
if(s>mb_skip_run != 0  show_bits(&s>gb, 15) != 0){ 
2093 
fprintf(stderr, "slice missmatch\n");

2094 
return 1; 
2095 
} 
2096 
goto eos; /* end of slice */ 
2097 
} 
2098 
/* otherwise, stuffing, nothing to do */

2099 
} else {

2100 
s>mb_skip_run += code; 
2101 
break;

2102 
} 
2103 
} 
2104 
} 
2105 
} 
2106 
eos: // end of slice 
2107 
*buf += get_bits_count(&s>gb)/8  1; 
2108 
//printf("y %d %d %d %d\n", s>resync_mb_x, s>resync_mb_y, s>mb_x, s>mb_y);

2109 
return 0; 
2110 
} 
2111  
2112 
/**

2113 
* handles slice ends.

2114 
* @return 1 if it seems to be the last slice of

2115 
*/

2116 
static int slice_end(AVCodecContext *avctx, AVFrame *pict) 
2117 
{ 
2118 
Mpeg1Context *s1 = avctx>priv_data; 
2119 
MpegEncContext *s = &s1>mpeg_enc_ctx; 
2120 

2121 
if (!s1>mpeg_enc_ctx_allocated  !s>current_picture_ptr)

2122 
return 0; 
2123  
2124 
#ifdef HAVE_XVMC

2125 
if(s>avctx>xvmc_acceleration)

2126 
XVMC_field_end(s); 
2127 
#endif

2128 
/* end of slice reached */

2129 
if (/*s>mb_y<<field_pic == s>mb_height &&*/ !s>first_field) { 
2130 
/* end of image */

2131  
2132 
if(s>codec_id == CODEC_ID_MPEG2VIDEO){

2133 
s>current_picture_ptr>qscale_type= FF_QSCALE_TYPE_MPEG2; 
2134 
}else

2135 
s>current_picture_ptr>qscale_type= FF_QSCALE_TYPE_MPEG1; 
2136  
2137 
ff_er_frame_end(s); 
2138  
2139 
MPV_frame_end(s); 
2140  
2141 
if (s>pict_type == B_TYPE  s>low_delay) {

2142 
*pict= *(AVFrame*)s>current_picture_ptr; 
2143 
ff_print_debug_info(s, s>current_picture_ptr); 
2144 
} else {

2145 
s>picture_number++; 
2146 
/* latency of 1 frame for I and P frames */

2147 
/* XXX: use another variable than picture_number */

2148 
if (s>last_picture_ptr != NULL) { 
2149 
*pict= *(AVFrame*)s>last_picture_ptr; 
2150 
ff_print_debug_info(s, s>last_picture_ptr); 
2151 
} 
2152 
} 
2153 
if(s>avctx>codec_tag == ff_get_fourcc("VCR2")) 
2154 
exchange_uv(pict); 
2155  
2156 
return 1; 
2157 
} else {

2158 
return 0; 
2159 
} 
2160 
} 
2161  
2162 
static int mpeg1_decode_sequence(AVCodecContext *avctx, 
2163 
uint8_t *buf, int buf_size)

2164 
{ 
2165 
Mpeg1Context *s1 = avctx>priv_data; 
2166 
MpegEncContext *s = &s1>mpeg_enc_ctx; 
2167 
int width, height, i, v, j;

2168 
float aspect;

2169  
2170 
init_get_bits(&s>gb, buf, buf_size*8);

2171  
2172 
width = get_bits(&s>gb, 12);

2173 
height = get_bits(&s>gb, 12);

2174 
s>aspect_ratio_info= get_bits(&s>gb, 4);

2175 
if(s>codec_id == CODEC_ID_MPEG1VIDEO){

2176 
aspect= mpeg1_aspect[s>aspect_ratio_info]; 
2177 
if(aspect!=0.0) avctx>aspect_ratio= width/(aspect*height); 
2178 
} 
2179  
2180 
s>frame_rate_index = get_bits(&s>gb, 4);

2181 
if (s>frame_rate_index == 0) 
2182 
return 1; 
2183 
s>bit_rate = get_bits(&s>gb, 18) * 400; 
2184 
if (get_bits1(&s>gb) == 0) /* marker */ 
2185 
return 1; 
2186 
if (width <= 0  height <= 0  
2187 
(width % 2) != 0  (height % 2) != 0) 
2188 
return 1; 
2189 
if (width != s>width 

2190 
height != s>height) { 
2191 
/* start new mpeg1 context decoding */

2192 
s>out_format = FMT_MPEG1; 
2193 
if (s1>mpeg_enc_ctx_allocated) {

2194 
MPV_common_end(s); 
2195 
} 
2196 
s>width = width; 
2197 
s>height = height; 
2198 
avctx>has_b_frames= 1;

2199 
s>avctx = avctx; 
2200 
avctx>width = width; 
2201 
avctx>height = height; 
2202 
av_reduce( 
2203 
&avctx>frame_rate, 
2204 
&avctx>frame_rate_base, 
2205 
frame_rate_tab[s>frame_rate_index], 
2206 
MPEG1_FRAME_RATE_BASE, //FIXME store in allready reduced form

2207 
1<<30 
2208 
); 
2209 
avctx>bit_rate = s>bit_rate; 
2210 

2211 
//get_format() or set_video(width,height,aspect,pix_fmt);

2212 
//until then pix_fmt may be changed right after codec init

2213 
if( avctx>pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )

2214 
avctx>idct_algo = FF_IDCT_SIMPLE; 
2215  
2216 
if (MPV_common_init(s) < 0) 
2217 
return 1; 
2218 
s1>mpeg_enc_ctx_allocated = 1;

2219 
} 
2220  
2221 
skip_bits(&s>gb, 10); /* vbv_buffer_size */ 
2222 
skip_bits(&s>gb, 1);

2223  
2224 
/* get matrix */

2225 
if (get_bits1(&s>gb)) {

2226 
for(i=0;i<64;i++) { 
2227 
v = get_bits(&s>gb, 8);

2228 
j = s>intra_scantable.permutated[i]; 
2229 
s>intra_matrix[j] = v; 
2230 
s>chroma_intra_matrix[j] = v; 
2231 
} 
2232 
#ifdef DEBUG

2233 
dprintf("intra matrix present\n");

2234 
for(i=0;i<64;i++) 
2235 
dprintf(" %d", s>intra_matrix[s>intra_scantable.permutated[i]]);

2236 
printf("\n");

2237 
#endif

2238 
} else {

2239 
for(i=0;i<64;i++) { 
2240 
int j= s>dsp.idct_permutation[i];

2241 
v = ff_mpeg1_default_intra_matrix[i]; 
2242 
s>intra_matrix[j] = v; 
2243 
s>chroma_intra_matrix[j] = v; 
2244 
} 
2245 
} 
2246 
if (get_bits1(&s>gb)) {

2247 
for(i=0;i<64;i++) { 
2248 
v = get_bits(&s>gb, 8);

2249 
j = s>intra_scantable.permutated[i]; 
2250 
s>inter_matrix[j] = v; 
2251 
s>chroma_inter_matrix[j] = v; 
2252 
} 
2253 
#ifdef DEBUG

2254 
dprintf("non intra matrix present\n");

2255 
for(i=0;i<64;i++) 
2256 
dprintf(" %d", s>inter_matrix[s>intra_scantable.permutated[i]]);

2257 
printf("\n");

2258 
#endif

2259 
} else {

2260 
for(i=0;i<64;i++) { 
2261 
int j= s>dsp.idct_permutation[i];

2262 
v = ff_mpeg1_default_non_intra_matrix[i]; 
2263 
s>inter_matrix[j] = v; 
2264 
s>chroma_inter_matrix[j] = v; 
2265 
} 
2266 
} 
2267  
2268 
/* we set mpeg2 parameters so that it emulates mpeg1 */

2269 
s>progressive_sequence = 1;

2270 
s>progressive_frame = 1;

2271 
s>picture_structure = PICT_FRAME; 
2272 
s>frame_pred_frame_dct = 1;

2273 
s>codec_id= s>avctx>codec_id= CODEC_ID_MPEG1VIDEO; 
2274 
avctx>sub_id = 1; /* indicates mpeg1 */ 
2275 
return 0; 
2276 
} 
2277  
2278 
static int vcr2_init_sequence(AVCodecContext *avctx) 
2279 
{ 
2280 
Mpeg1Context *s1 = avctx>priv_data; 
2281 
MpegEncContext *s = &s1>mpeg_enc_ctx; 
2282 
int i, v;

2283  
2284 
/* start new mpeg1 context decoding */

2285 
s>out_format = FMT_MPEG1; 
2286 
if (s1>mpeg_enc_ctx_allocated) {

2287 
MPV_common_end(s); 
2288 
} 
2289 
s>width = avctx>width; 
2290 
s>height = avctx>height; 
2291 
avctx>has_b_frames= 0; //true? 
2292 
s>low_delay= 1;

2293 
s>avctx = avctx; 
2294  
2295 
//get_format() or set_video(width,height,aspect,pix_fmt);

2296 
//until then pix_fmt may be changed right after codec init

2297 
if( avctx>pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )

2298 
avctx>idct_algo = FF_IDCT_SIMPLE; 
2299 

2300 
if (MPV_common_init(s) < 0) 
2301 
return 1; 
2302 
s1>mpeg_enc_ctx_allocated = 1;

2303  
2304 
for(i=0;i<64;i++) { 
2305 
int j= s>dsp.idct_permutation[i];

2306 
v = ff_mpeg1_default_intra_matrix[i]; 
2307 
s>intra_matrix[j] = v; 
2308 
s>chroma_intra_matrix[j] = v; 
2309  
2310 
v = ff_mpeg1_default_non_intra_matrix[i]; 
2311 
s>inter_matrix[j] = v; 
2312 
s>chroma_inter_matrix[j] = v; 
2313 
} 
2314  
2315 
s>progressive_sequence = 1;

2316 
s>progressive_frame = 1;

2317 
s>picture_structure = PICT_FRAME; 
2318 
s>frame_pred_frame_dct = 1;

2319 
s>codec_id= s>avctx>codec_id= CODEC_ID_MPEG2VIDEO; 
2320 
avctx>sub_id = 2; /* indicates mpeg2 */ 
2321 
return 0; 
2322 
} 
2323  
2324  
2325 
static void mpeg_decode_user_data(AVCodecContext *avctx, 
2326 
const uint8_t *buf, int buf_size) 
2327 
{ 
2328 
const uint8_t *p;

2329 
int len, flags;

2330 
p = buf; 
2331 
len = buf_size; 
2332  
2333 
/* we parse the DTG active format information */

2334 
if (len >= 5 && 
2335 
p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') { 
2336 
flags = p[4];

2337 
p += 5;

2338 
len = 5;

2339 
if (flags & 0x80) { 
2340 
/* skip event id */

2341 
if (len < 2) 
2342 
return;

2343 
p += 2;

2344 
len = 2;

2345 
} 
2346 
if (flags & 0x40) { 
2347 
if (len < 1) 
2348 
return;

2349 
avctx>dtg_active_format = p[0] & 0x0f; 
2350 
} 
2351 
} 
2352 
} 
2353  
2354 
/**

2355 
* finds the end of the current frame in the bitstream.

2356 
* @return the position of the first byte of the next frame, or 1

2357 
*/

2358 
static int mpeg1_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){ 
2359 
ParseContext *pc= &s>parse_context; 
2360 
int i;

2361 
uint32_t state; 
2362 

2363 
state= pc>state; 
2364 

2365 
i=0;

2366 
if(!pc>frame_start_found){

2367 
for(i=0; i<buf_size; i++){ 
2368 
state= (state<<8)  buf[i];

2369 
if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){

2370 
i++; 
2371 
pc>frame_start_found=1;

2372 
break;

2373 
} 
2374 
} 
2375 
} 
2376 

2377 
if(pc>frame_start_found){

2378 
for(; i<buf_size; i++){

2379 
state= (state<<8)  buf[i];

2380 
if((state&0xFFFFFF00) == 0x100){ 
2381 
if(state < SLICE_MIN_START_CODE  state > SLICE_MAX_START_CODE){

2382 
pc>frame_start_found=0;

2383 
pc>state=1;

2384 
return i3; 
2385 
} 
2386 
} 
2387 
} 
2388 
} 
2389 
pc>state= state; 
2390 
return END_NOT_FOUND;

2391 
} 
2392  
2393 
/* handle buffering and image synchronisation */

2394 
static int mpeg_decode_frame(AVCodecContext *avctx, 
2395 
void *data, int *data_size, 
2396 
uint8_t *buf, int buf_size)

2397 
{ 
2398 
Mpeg1Context *s = avctx>priv_data; 
2399 
uint8_t *buf_end, *buf_ptr; 
2400 
int ret, start_code, input_size;

2401 
AVFrame *picture = data; 
2402 
MpegEncContext *s2 = &s>mpeg_enc_ctx; 
2403 
dprintf("fill_buffer\n");

2404  
2405 
*data_size = 0;

2406  
2407 
/* special case for last picture */

2408 
if (buf_size == 0 && s2>low_delay==0 && s2>next_picture_ptr) { 
2409 
*picture= *(AVFrame*)s2>next_picture_ptr; 
2410 
s2>next_picture_ptr= NULL;

2411  
2412 
*data_size = sizeof(AVFrame);

2413 
return 0; 
2414 
} 
2415  
2416 
if(s2>flags&CODEC_FLAG_TRUNCATED){

2417 
int next= mpeg1_find_frame_end(s2, buf, buf_size);

2418 

2419 
if( ff_combine_frame(s2, next, &buf, &buf_size) < 0 ) 
2420 
return buf_size;

2421 
} 
2422 

2423 
buf_ptr = buf; 
2424 
buf_end = buf + buf_size; 
2425  
2426 
#if 0

2427 
if (s>repeat_field % 2 == 1) {

2428 
s>repeat_field++;

2429 
//fprintf(stderr,"\nRepeating last frame: %d > %d! pict: %d %d", avctx>frame_number1, avctx>frame_number,

2430 
// s2>picture_number, s>repeat_field);

2431 
if (avctx>flags & CODEC_FLAG_REPEAT_FIELD) {

2432 
*data_size = sizeof(AVPicture);

2433 
goto the_end;

2434 
}

2435 
}

2436 
#endif

2437  
2438 
if(s>mpeg_enc_ctx_allocated==0 && avctx>codec_tag == ff_get_fourcc("VCR2")) 
2439 
vcr2_init_sequence(avctx); 
2440  
2441 
for(;;) {

2442 
/* find start next code */

2443 
start_code = find_start_code(&buf_ptr, buf_end); 
2444 
if (start_code < 0){ 
2445 
if (slice_end(avctx, picture)) {

2446 
if(s2>last_picture_ptr) //FIXME merge with the stuff in mpeg_decode_slice 
2447 
*data_size = sizeof(AVPicture);

2448 
} 
2449 
return FFMAX(0, buf_ptr  buf  s2>parse_context.last_index); 
2450 
} 
2451 

2452 
input_size = buf_end  buf_ptr; 
2453  
2454 
if(avctx>debug & FF_DEBUG_STARTCODE){

2455 
printf("%3X at %d left %d\n", start_code, buf_ptrbuf, input_size);

2456 
} 
2457  
2458 
/* prepare data for next start code */

2459 
switch(start_code) {

2460 
case SEQ_START_CODE:

2461 
mpeg1_decode_sequence(avctx, buf_ptr, 
2462 
input_size); 
2463 
break;

2464 

2465 
case PICTURE_START_CODE:

2466 
/* we have a complete image : we try to decompress it */

2467 
mpeg1_decode_picture(avctx, 
2468 
buf_ptr, input_size); 
2469 
break;

2470 
case EXT_START_CODE:

2471 
mpeg_decode_extension(avctx, 
2472 
buf_ptr, input_size); 
2473 
break;

2474 
case USER_START_CODE:

2475 
mpeg_decode_user_data(avctx, 
2476 
buf_ptr, input_size); 
2477 
break;

2478 
case GOP_START_CODE:

2479 
s2>first_field=0;

2480 
break;

2481 
default:

2482 
if (start_code >= SLICE_MIN_START_CODE &&

2483 
start_code <= SLICE_MAX_START_CODE) { 
2484 

2485 
/* skip b frames if we dont have reference frames */

2486 
if(s2>last_picture_ptr==NULL && s2>pict_type==B_TYPE) break; 
2487 
/* skip b frames if we are in a hurry */

2488 
if(avctx>hurry_up && s2>pict_type==B_TYPE) break; 
2489 
/* skip everything if we are in a hurry>=5 */

2490 
if(avctx>hurry_up>=5) break; 
2491 

2492 
if (!s>mpeg_enc_ctx_allocated) break; 
2493  
2494 
ret = mpeg_decode_slice(avctx, picture, 
2495 
start_code, &buf_ptr, input_size); 
2496 
emms_c(); 
2497  
2498 
if(ret < 0){ 
2499 
if(s2>resync_mb_x>=0 && s2>resync_mb_y>=0) 
2500 
ff_er_add_slice(s2, s2>resync_mb_x, s2>resync_mb_y, s2>mb_x, s2>mb_y, AC_ERRORDC_ERRORMV_ERROR); 
2501 
if(ret==DECODE_SLICE_FATAL_ERROR) return 1; 
2502 
}else{

2503 
ff_er_add_slice(s2, s2>resync_mb_x, s2>resync_mb_y, s2>mb_x1, s2>mb_y, AC_ENDDC_ENDMV_END);

2504 
} 
2505 
} 
2506 
break;

2507 
} 
2508 
} 
2509 
} 
2510  
2511 
static int mpeg_decode_end(AVCodecContext *avctx) 
2512 
{ 
2513 
Mpeg1Context *s = avctx>priv_data; 
2514  
2515 
if (s>mpeg_enc_ctx_allocated)

2516 
MPV_common_end(&s>mpeg_enc_ctx); 
2517 
return 0; 
2518 
} 
2519  
2520 
AVCodec mpeg1video_decoder = { 
2521 
"mpeg1video",

2522 
CODEC_TYPE_VIDEO, 
2523 
CODEC_ID_MPEG1VIDEO, 
2524 
sizeof(Mpeg1Context),

2525 
mpeg_decode_init, 
2526 
NULL,

2527 
mpeg_decode_end, 
2528 
mpeg_decode_frame, 
2529 
CODEC_CAP_DRAW_HORIZ_BAND  CODEC_CAP_DR1  CODEC_CAP_TRUNCATED, 
2530 
.flush= ff_mpeg_flush, 
2531 
}; 
2532  
2533 
AVCodec mpeg2video_decoder = { 
2534 
"mpeg2video",

2535 
CODEC_TYPE_VIDEO, 
2536 
CODEC_ID_MPEG2VIDEO, 
2537 
sizeof(Mpeg1Context),

2538 
mpeg_decode_init, 
2539 
NULL,

2540 
mpeg_decode_end, 
2541 
mpeg_decode_frame, 
2542 
CODEC_CAP_DRAW_HORIZ_BAND  CODEC_CAP_DR1  CODEC_CAP_TRUNCATED, 
2543 
.flush= ff_mpeg_flush, 
2544 
}; 
2545  
2546 
#ifdef HAVE_XVMC

2547 
static int mpeg_mc_decode_init(AVCodecContext *avctx){ 
2548 
Mpeg1Context *s; 
2549  
2550 
if( !(avctx>slice_flags & SLICE_FLAG_CODED_ORDER) )

2551 
return 1; 
2552 
if( !(avctx>slice_flags & SLICE_FLAG_ALLOW_FIELD) )

2553 
dprintf("mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");

2554  
2555 
mpeg_decode_init(avctx); 
2556 
s = avctx>priv_data; 
2557  
2558 
avctx>pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT; 
2559 
avctx>xvmc_acceleration = 1;

2560  
2561 
return 0; 
2562 
} 
2563  
2564 
AVCodec mpeg_xvmc_decoder = { 
2565 
"mpegvideo_xvmc",

2566 
CODEC_TYPE_VIDEO, 
2567 
CODEC_ID_MPEG2VIDEO_XVMC, 
2568 
sizeof(Mpeg1Context),

2569 
mpeg_mc_decode_init, 
2570 
NULL,

2571 
mpeg_decode_end, 
2572 
mpeg_decode_frame, 
2573 
CODEC_CAP_DRAW_HORIZ_BAND  CODEC_CAP_DR1  CODEC_CAP_TRUNCATED, 
2574 
}; 
2575  
2576 
#endif

2577  
2578 
/* this is ugly i know, but the alternative is too make

2579 
hundreds of vars global and prefix them with ff_mpeg1_

2580 
which is far uglier. */

2581 
#include "mdec.c" 