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