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