Mercurial > libavcodec.hg
comparison wmv2.c @ 5939:b47573cb7401 libavcodec
split wmv2 encoder and decoder in their own files
author | aurel |
---|---|
date | Sun, 25 Nov 2007 16:23:42 +0000 |
parents | 83ac4620c6ed |
children | f4859c13426b |
comparison
equal
deleted
inserted
replaced
5938:7bc05f20e435 | 5939:b47573cb7401 |
---|---|
16 * You should have received a copy of the GNU Lesser General Public | 16 * You should have received a copy of the GNU Lesser General Public |
17 * License along with FFmpeg; if not, write to the Free Software | 17 * License along with FFmpeg; if not, write to the Free Software |
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 */ | 19 */ |
20 | 20 |
21 /** | |
22 * @file wmv2.c | |
23 * wmv2 codec. | |
24 */ | |
25 | |
26 #include "avcodec.h" | 21 #include "avcodec.h" |
27 #include "dsputil.h" | |
28 #include "mpegvideo.h" | 22 #include "mpegvideo.h" |
29 #include "msmpeg4.h" | |
30 #include "msmpeg4data.h" | 23 #include "msmpeg4data.h" |
31 #include "simple_idct.h" | 24 #include "simple_idct.h" |
32 #include "intrax8.h" | 25 #include "wmv2.h" |
33 | |
34 #define SKIP_TYPE_NONE 0 | |
35 #define SKIP_TYPE_MPEG 1 | |
36 #define SKIP_TYPE_ROW 2 | |
37 #define SKIP_TYPE_COL 3 | |
38 | 26 |
39 | 27 |
40 typedef struct Wmv2Context{ | 28 void ff_wmv2_common_init(Wmv2Context * w){ |
41 MpegEncContext s; | |
42 IntraX8Context x8; | |
43 int j_type_bit; | |
44 int j_type; | |
45 int abt_flag; | |
46 int abt_type; | |
47 int abt_type_table[6]; | |
48 int per_mb_abt; | |
49 int per_block_abt; | |
50 int mspel_bit; | |
51 int cbp_table_index; | |
52 int top_left_mv_flag; | |
53 int per_mb_rl_bit; | |
54 int skip_type; | |
55 int hshift; | |
56 | |
57 ScanTable abt_scantable[2]; | |
58 DECLARE_ALIGNED_8(DCTELEM, abt_block2[6][64]); | |
59 }Wmv2Context; | |
60 | |
61 static void wmv2_common_init(Wmv2Context * w){ | |
62 MpegEncContext * const s= &w->s; | 29 MpegEncContext * const s= &w->s; |
63 | 30 |
64 ff_init_scantable(s->dsp.idct_permutation, &w->abt_scantable[0], wmv2_scantableA); | 31 ff_init_scantable(s->dsp.idct_permutation, &w->abt_scantable[0], wmv2_scantableA); |
65 ff_init_scantable(s->dsp.idct_permutation, &w->abt_scantable[1], wmv2_scantableB); | 32 ff_init_scantable(s->dsp.idct_permutation, &w->abt_scantable[1], wmv2_scantableB); |
66 } | |
67 | |
68 #ifdef CONFIG_WMV2_ENCODER | |
69 | |
70 static int encode_ext_header(Wmv2Context *w){ | |
71 MpegEncContext * const s= &w->s; | |
72 PutBitContext pb; | |
73 int code; | |
74 | |
75 init_put_bits(&pb, s->avctx->extradata, s->avctx->extradata_size); | |
76 | |
77 put_bits(&pb, 5, s->avctx->time_base.den / s->avctx->time_base.num); //yes 29.97 -> 29 | |
78 put_bits(&pb, 11, FFMIN(s->bit_rate/1024, 2047)); | |
79 | |
80 put_bits(&pb, 1, w->mspel_bit=1); | |
81 put_bits(&pb, 1, s->loop_filter); | |
82 put_bits(&pb, 1, w->abt_flag=1); | |
83 put_bits(&pb, 1, w->j_type_bit=1); | |
84 put_bits(&pb, 1, w->top_left_mv_flag=0); | |
85 put_bits(&pb, 1, w->per_mb_rl_bit=1); | |
86 put_bits(&pb, 3, code=1); | |
87 | |
88 flush_put_bits(&pb); | |
89 | |
90 s->slice_height = s->mb_height / code; | |
91 | |
92 return 0; | |
93 } | |
94 | |
95 static int wmv2_encode_init(AVCodecContext *avctx){ | |
96 Wmv2Context * const w= avctx->priv_data; | |
97 | |
98 if(MPV_encode_init(avctx) < 0) | |
99 return -1; | |
100 | |
101 wmv2_common_init(w); | |
102 | |
103 avctx->extradata_size= 4; | |
104 avctx->extradata= av_mallocz(avctx->extradata_size + 10); | |
105 encode_ext_header(w); | |
106 | |
107 return 0; | |
108 } | |
109 | |
110 #if 0 /* unused, remove? */ | |
111 static int wmv2_encode_end(AVCodecContext *avctx){ | |
112 | |
113 if(MPV_encode_end(avctx) < 0) | |
114 return -1; | |
115 | |
116 avctx->extradata_size= 0; | |
117 av_freep(&avctx->extradata); | |
118 | |
119 return 0; | |
120 } | |
121 #endif | |
122 | |
123 int ff_wmv2_encode_picture_header(MpegEncContext * s, int picture_number) | |
124 { | |
125 Wmv2Context * const w= (Wmv2Context*)s; | |
126 | |
127 put_bits(&s->pb, 1, s->pict_type - 1); | |
128 if(s->pict_type == I_TYPE){ | |
129 put_bits(&s->pb, 7, 0); | |
130 } | |
131 put_bits(&s->pb, 5, s->qscale); | |
132 | |
133 s->dc_table_index = 1; | |
134 s->mv_table_index = 1; /* only if P frame */ | |
135 // s->use_skip_mb_code = 1; /* only if P frame */ | |
136 s->per_mb_rl_table = 0; | |
137 s->mspel= 0; | |
138 w->per_mb_abt=0; | |
139 w->abt_type=0; | |
140 w->j_type=0; | |
141 | |
142 assert(s->flipflop_rounding); | |
143 | |
144 if (s->pict_type == I_TYPE) { | |
145 assert(s->no_rounding==1); | |
146 if(w->j_type_bit) put_bits(&s->pb, 1, w->j_type); | |
147 | |
148 if(w->per_mb_rl_bit) put_bits(&s->pb, 1, s->per_mb_rl_table); | |
149 | |
150 if(!s->per_mb_rl_table){ | |
151 ff_msmpeg4_code012(&s->pb, s->rl_chroma_table_index); | |
152 ff_msmpeg4_code012(&s->pb, s->rl_table_index); | |
153 } | |
154 | |
155 put_bits(&s->pb, 1, s->dc_table_index); | |
156 | |
157 s->inter_intra_pred= 0; | |
158 }else{ | |
159 int cbp_index; | |
160 | |
161 put_bits(&s->pb, 2, SKIP_TYPE_NONE); | |
162 | |
163 ff_msmpeg4_code012(&s->pb, cbp_index=0); | |
164 if(s->qscale <= 10){ | |
165 int map[3]= {0,2,1}; | |
166 w->cbp_table_index= map[cbp_index]; | |
167 }else if(s->qscale <= 20){ | |
168 int map[3]= {1,0,2}; | |
169 w->cbp_table_index= map[cbp_index]; | |
170 }else{ | |
171 int map[3]= {2,1,0}; | |
172 w->cbp_table_index= map[cbp_index]; | |
173 } | |
174 | |
175 if(w->mspel_bit) put_bits(&s->pb, 1, s->mspel); | |
176 | |
177 if(w->abt_flag){ | |
178 put_bits(&s->pb, 1, w->per_mb_abt^1); | |
179 if(!w->per_mb_abt){ | |
180 ff_msmpeg4_code012(&s->pb, w->abt_type); | |
181 } | |
182 } | |
183 | |
184 if(w->per_mb_rl_bit) put_bits(&s->pb, 1, s->per_mb_rl_table); | |
185 | |
186 if(!s->per_mb_rl_table){ | |
187 ff_msmpeg4_code012(&s->pb, s->rl_table_index); | |
188 s->rl_chroma_table_index = s->rl_table_index; | |
189 } | |
190 put_bits(&s->pb, 1, s->dc_table_index); | |
191 put_bits(&s->pb, 1, s->mv_table_index); | |
192 | |
193 s->inter_intra_pred= 0;//(s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE); | |
194 } | |
195 s->esc3_level_length= 0; | |
196 s->esc3_run_length= 0; | |
197 | |
198 return 0; | |
199 } | |
200 | |
201 /* Nearly identical to wmv1 but that is just because we do not use the | |
202 * useless M$ crap features. It is duplicated here in case someone wants | |
203 * to add support for these crap features. */ | |
204 void ff_wmv2_encode_mb(MpegEncContext * s, | |
205 DCTELEM block[6][64], | |
206 int motion_x, int motion_y) | |
207 { | |
208 Wmv2Context * const w= (Wmv2Context*)s; | |
209 int cbp, coded_cbp, i; | |
210 int pred_x, pred_y; | |
211 uint8_t *coded_block; | |
212 | |
213 ff_msmpeg4_handle_slices(s); | |
214 | |
215 if (!s->mb_intra) { | |
216 /* compute cbp */ | |
217 cbp = 0; | |
218 for (i = 0; i < 6; i++) { | |
219 if (s->block_last_index[i] >= 0) | |
220 cbp |= 1 << (5 - i); | |
221 } | |
222 | |
223 put_bits(&s->pb, | |
224 wmv2_inter_table[w->cbp_table_index][cbp + 64][1], | |
225 wmv2_inter_table[w->cbp_table_index][cbp + 64][0]); | |
226 | |
227 /* motion vector */ | |
228 h263_pred_motion(s, 0, 0, &pred_x, &pred_y); | |
229 ff_msmpeg4_encode_motion(s, motion_x - pred_x, | |
230 motion_y - pred_y); | |
231 } else { | |
232 /* compute cbp */ | |
233 cbp = 0; | |
234 coded_cbp = 0; | |
235 for (i = 0; i < 6; i++) { | |
236 int val, pred; | |
237 val = (s->block_last_index[i] >= 1); | |
238 cbp |= val << (5 - i); | |
239 if (i < 4) { | |
240 /* predict value for close blocks only for luma */ | |
241 pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block); | |
242 *coded_block = val; | |
243 val = val ^ pred; | |
244 } | |
245 coded_cbp |= val << (5 - i); | |
246 } | |
247 #if 0 | |
248 if (coded_cbp) | |
249 printf("cbp=%x %x\n", cbp, coded_cbp); | |
250 #endif | |
251 | |
252 if (s->pict_type == I_TYPE) { | |
253 put_bits(&s->pb, | |
254 ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]); | |
255 } else { | |
256 put_bits(&s->pb, | |
257 wmv2_inter_table[w->cbp_table_index][cbp][1], | |
258 wmv2_inter_table[w->cbp_table_index][cbp][0]); | |
259 } | |
260 put_bits(&s->pb, 1, 0); /* no AC prediction yet */ | |
261 if(s->inter_intra_pred){ | |
262 s->h263_aic_dir=0; | |
263 put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]); | |
264 } | |
265 } | |
266 | |
267 for (i = 0; i < 6; i++) { | |
268 ff_msmpeg4_encode_block(s, block[i], i); | |
269 } | |
270 } | |
271 #endif //CONFIG_WMV2_ENCODER | |
272 | |
273 static void parse_mb_skip(Wmv2Context * w){ | |
274 int mb_x, mb_y; | |
275 MpegEncContext * const s= &w->s; | |
276 uint32_t * const mb_type= s->current_picture_ptr->mb_type; | |
277 | |
278 w->skip_type= get_bits(&s->gb, 2); | |
279 switch(w->skip_type){ | |
280 case SKIP_TYPE_NONE: | |
281 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
282 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
283 mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_16x16 | MB_TYPE_L0; | |
284 } | |
285 } | |
286 break; | |
287 case SKIP_TYPE_MPEG: | |
288 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
289 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
290 mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0; | |
291 } | |
292 } | |
293 break; | |
294 case SKIP_TYPE_ROW: | |
295 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
296 if(get_bits1(&s->gb)){ | |
297 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
298 mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0; | |
299 } | |
300 }else{ | |
301 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
302 mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0; | |
303 } | |
304 } | |
305 } | |
306 break; | |
307 case SKIP_TYPE_COL: | |
308 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
309 if(get_bits1(&s->gb)){ | |
310 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
311 mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0; | |
312 } | |
313 }else{ | |
314 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
315 mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0; | |
316 } | |
317 } | |
318 } | |
319 break; | |
320 } | |
321 } | |
322 | |
323 static int decode_ext_header(Wmv2Context *w){ | |
324 MpegEncContext * const s= &w->s; | |
325 GetBitContext gb; | |
326 int fps; | |
327 int code; | |
328 | |
329 if(s->avctx->extradata_size<4) return -1; | |
330 | |
331 init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8); | |
332 | |
333 fps = get_bits(&gb, 5); | |
334 s->bit_rate = get_bits(&gb, 11)*1024; | |
335 w->mspel_bit = get_bits1(&gb); | |
336 s->loop_filter = get_bits1(&gb); | |
337 w->abt_flag = get_bits1(&gb); | |
338 w->j_type_bit = get_bits1(&gb); | |
339 w->top_left_mv_flag= get_bits1(&gb); | |
340 w->per_mb_rl_bit = get_bits1(&gb); | |
341 code = get_bits(&gb, 3); | |
342 | |
343 if(code==0) return -1; | |
344 | |
345 s->slice_height = s->mb_height / code; | |
346 | |
347 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ | |
348 av_log(s->avctx, AV_LOG_DEBUG, "fps:%d, br:%d, qpbit:%d, abt_flag:%d, j_type_bit:%d, tl_mv_flag:%d, mbrl_bit:%d, code:%d, loop_filter:%d, slices:%d\n", | |
349 fps, s->bit_rate, w->mspel_bit, w->abt_flag, w->j_type_bit, w->top_left_mv_flag, w->per_mb_rl_bit, code, s->loop_filter, | |
350 code); | |
351 } | |
352 return 0; | |
353 } | |
354 | |
355 int ff_wmv2_decode_picture_header(MpegEncContext * s) | |
356 { | |
357 Wmv2Context * const w= (Wmv2Context*)s; | |
358 int code; | |
359 | |
360 #if 0 | |
361 { | |
362 int i; | |
363 for(i=0; i<s->gb.size*8; i++) | |
364 printf("%d", get_bits1(&s->gb)); | |
365 // get_bits1(&s->gb); | |
366 printf("END\n"); | |
367 return -1; | |
368 } | |
369 #endif | |
370 if(s->picture_number==0) | |
371 decode_ext_header(w); | |
372 | |
373 s->pict_type = get_bits1(&s->gb) + 1; | |
374 if(s->pict_type == I_TYPE){ | |
375 code = get_bits(&s->gb, 7); | |
376 av_log(s->avctx, AV_LOG_DEBUG, "I7:%X/\n", code); | |
377 } | |
378 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5); | |
379 if(s->qscale < 0) | |
380 return -1; | |
381 | |
382 return 0; | |
383 } | |
384 | |
385 int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s) | |
386 { | |
387 Wmv2Context * const w= (Wmv2Context*)s; | |
388 | |
389 if (s->pict_type == I_TYPE) { | |
390 if(w->j_type_bit) w->j_type= get_bits1(&s->gb); | |
391 else w->j_type= 0; //FIXME check | |
392 | |
393 if(!w->j_type){ | |
394 if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb); | |
395 else s->per_mb_rl_table= 0; | |
396 | |
397 if(!s->per_mb_rl_table){ | |
398 s->rl_chroma_table_index = decode012(&s->gb); | |
399 s->rl_table_index = decode012(&s->gb); | |
400 } | |
401 | |
402 s->dc_table_index = get_bits1(&s->gb); | |
403 } | |
404 s->inter_intra_pred= 0; | |
405 s->no_rounding = 1; | |
406 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ | |
407 av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n", | |
408 s->qscale, | |
409 s->rl_chroma_table_index, | |
410 s->rl_table_index, | |
411 s->dc_table_index, | |
412 s->per_mb_rl_table, | |
413 w->j_type); | |
414 } | |
415 }else{ | |
416 int cbp_index; | |
417 w->j_type=0; | |
418 | |
419 parse_mb_skip(w); | |
420 cbp_index= decode012(&s->gb); | |
421 if(s->qscale <= 10){ | |
422 int map[3]= {0,2,1}; | |
423 w->cbp_table_index= map[cbp_index]; | |
424 }else if(s->qscale <= 20){ | |
425 int map[3]= {1,0,2}; | |
426 w->cbp_table_index= map[cbp_index]; | |
427 }else{ | |
428 int map[3]= {2,1,0}; | |
429 w->cbp_table_index= map[cbp_index]; | |
430 } | |
431 | |
432 if(w->mspel_bit) s->mspel= get_bits1(&s->gb); | |
433 else s->mspel= 0; //FIXME check | |
434 | |
435 if(w->abt_flag){ | |
436 w->per_mb_abt= get_bits1(&s->gb)^1; | |
437 if(!w->per_mb_abt){ | |
438 w->abt_type= decode012(&s->gb); | |
439 } | |
440 } | |
441 | |
442 if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb); | |
443 else s->per_mb_rl_table= 0; | |
444 | |
445 if(!s->per_mb_rl_table){ | |
446 s->rl_table_index = decode012(&s->gb); | |
447 s->rl_chroma_table_index = s->rl_table_index; | |
448 } | |
449 | |
450 s->dc_table_index = get_bits1(&s->gb); | |
451 s->mv_table_index = get_bits1(&s->gb); | |
452 | |
453 s->inter_intra_pred= 0;//(s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE); | |
454 s->no_rounding ^= 1; | |
455 | |
456 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ | |
457 av_log(s->avctx, AV_LOG_DEBUG, "rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d mspel:%d per_mb_abt:%d abt_type:%d cbp:%d ii:%d\n", | |
458 s->rl_table_index, | |
459 s->rl_chroma_table_index, | |
460 s->dc_table_index, | |
461 s->mv_table_index, | |
462 s->per_mb_rl_table, | |
463 s->qscale, | |
464 s->mspel, | |
465 w->per_mb_abt, | |
466 w->abt_type, | |
467 w->cbp_table_index, | |
468 s->inter_intra_pred); | |
469 } | |
470 } | |
471 s->esc3_level_length= 0; | |
472 s->esc3_run_length= 0; | |
473 | |
474 s->picture_number++; //FIXME ? | |
475 | |
476 | |
477 if(w->j_type){ | |
478 ff_intrax8_decode_picture(&w->x8, 2*s->qscale, (s->qscale-1)|1 ); | |
479 return 1; | |
480 } | |
481 | |
482 return 0; | |
483 } | |
484 | |
485 static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr){ | |
486 MpegEncContext * const s= &w->s; | |
487 int ret; | |
488 | |
489 ret= ff_msmpeg4_decode_motion(s, mx_ptr, my_ptr); | |
490 | |
491 if(ret<0) return -1; | |
492 | |
493 if((((*mx_ptr)|(*my_ptr)) & 1) && s->mspel) | |
494 w->hshift= get_bits1(&s->gb); | |
495 else | |
496 w->hshift= 0; | |
497 | |
498 //printf("%d %d ", *mx_ptr, *my_ptr); | |
499 | |
500 return 0; | |
501 } | |
502 | |
503 static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py){ | |
504 MpegEncContext * const s= &w->s; | |
505 int xy, wrap, diff, type; | |
506 int16_t *A, *B, *C, *mot_val; | |
507 | |
508 wrap = s->b8_stride; | |
509 xy = s->block_index[0]; | |
510 | |
511 mot_val = s->current_picture.motion_val[0][xy]; | |
512 | |
513 A = s->current_picture.motion_val[0][xy - 1]; | |
514 B = s->current_picture.motion_val[0][xy - wrap]; | |
515 C = s->current_picture.motion_val[0][xy + 2 - wrap]; | |
516 | |
517 if(s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag) | |
518 diff= FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1])); | |
519 else | |
520 diff=0; | |
521 | |
522 if(diff >= 8) | |
523 type= get_bits1(&s->gb); | |
524 else | |
525 type= 2; | |
526 | |
527 if(type == 0){ | |
528 *px= A[0]; | |
529 *py= A[1]; | |
530 }else if(type == 1){ | |
531 *px= B[0]; | |
532 *py= B[1]; | |
533 }else{ | |
534 /* special case for first (slice) line */ | |
535 if (s->first_slice_line) { | |
536 *px = A[0]; | |
537 *py = A[1]; | |
538 } else { | |
539 *px = mid_pred(A[0], B[0], C[0]); | |
540 *py = mid_pred(A[1], B[1], C[1]); | |
541 } | |
542 } | |
543 | |
544 return mot_val; | |
545 } | |
546 | |
547 static inline int wmv2_decode_inter_block(Wmv2Context *w, DCTELEM *block, int n, int cbp){ | |
548 MpegEncContext * const s= &w->s; | |
549 static const int sub_cbp_table[3]= {2,3,1}; | |
550 int sub_cbp; | |
551 | |
552 if(!cbp){ | |
553 s->block_last_index[n] = -1; | |
554 | |
555 return 0; | |
556 } | |
557 | |
558 if(w->per_block_abt) | |
559 w->abt_type= decode012(&s->gb); | |
560 #if 0 | |
561 if(w->per_block_abt) | |
562 printf("B%d", w->abt_type); | |
563 #endif | |
564 w->abt_type_table[n]= w->abt_type; | |
565 | |
566 if(w->abt_type){ | |
567 // const uint8_t *scantable= w->abt_scantable[w->abt_type-1].permutated; | |
568 const uint8_t *scantable= w->abt_scantable[w->abt_type-1].scantable; | |
569 // const uint8_t *scantable= w->abt_type-1 ? w->abt_scantable[1].permutated : w->abt_scantable[0].scantable; | |
570 | |
571 sub_cbp= sub_cbp_table[ decode012(&s->gb) ]; | |
572 // printf("S%d", sub_cbp); | |
573 | |
574 if(sub_cbp&1){ | |
575 if (ff_msmpeg4_decode_block(s, block, n, 1, scantable) < 0) | |
576 return -1; | |
577 } | |
578 | |
579 if(sub_cbp&2){ | |
580 if (ff_msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable) < 0) | |
581 return -1; | |
582 } | |
583 s->block_last_index[n] = 63; | |
584 | |
585 return 0; | |
586 }else{ | |
587 return ff_msmpeg4_decode_block(s, block, n, 1, s->inter_scantable.permutated); | |
588 } | |
589 } | 33 } |
590 | 34 |
591 static void wmv2_add_block(Wmv2Context *w, DCTELEM *block1, uint8_t *dst, int stride, int n){ | 35 static void wmv2_add_block(Wmv2Context *w, DCTELEM *block1, uint8_t *dst, int stride, int n){ |
592 MpegEncContext * const s= &w->s; | 36 MpegEncContext * const s= &w->s; |
593 | 37 |
711 src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); | 155 src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); |
712 ptr= s->edge_emu_buffer; | 156 ptr= s->edge_emu_buffer; |
713 } | 157 } |
714 pix_op[1][dxy](dest_cr, ptr, uvlinesize, h >> 1); | 158 pix_op[1][dxy](dest_cr, ptr, uvlinesize, h >> 1); |
715 } | 159 } |
716 | |
717 | |
718 int ff_wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) | |
719 { | |
720 Wmv2Context * const w= (Wmv2Context*)s; | |
721 int cbp, code, i; | |
722 uint8_t *coded_val; | |
723 | |
724 if(w->j_type) return 0; | |
725 | |
726 if (s->pict_type == P_TYPE) { | |
727 if(IS_SKIP(s->current_picture.mb_type[s->mb_y * s->mb_stride + s->mb_x])){ | |
728 /* skip mb */ | |
729 s->mb_intra = 0; | |
730 for(i=0;i<6;i++) | |
731 s->block_last_index[i] = -1; | |
732 s->mv_dir = MV_DIR_FORWARD; | |
733 s->mv_type = MV_TYPE_16X16; | |
734 s->mv[0][0][0] = 0; | |
735 s->mv[0][0][1] = 0; | |
736 s->mb_skipped = 1; | |
737 w->hshift=0; | |
738 return 0; | |
739 } | |
740 | |
741 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[w->cbp_table_index].table, MB_NON_INTRA_VLC_BITS, 3); | |
742 if (code < 0) | |
743 return -1; | |
744 s->mb_intra = (~code & 0x40) >> 6; | |
745 | |
746 cbp = code & 0x3f; | |
747 } else { | |
748 s->mb_intra = 1; | |
749 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2); | |
750 if (code < 0){ | |
751 av_log(s->avctx, AV_LOG_ERROR, "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y); | |
752 return -1; | |
753 } | |
754 /* predict coded block pattern */ | |
755 cbp = 0; | |
756 for(i=0;i<6;i++) { | |
757 int val = ((code >> (5 - i)) & 1); | |
758 if (i < 4) { | |
759 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val); | |
760 val = val ^ pred; | |
761 *coded_val = val; | |
762 } | |
763 cbp |= val << (5 - i); | |
764 } | |
765 } | |
766 | |
767 if (!s->mb_intra) { | |
768 int mx, my; | |
769 //printf("P at %d %d\n", s->mb_x, s->mb_y); | |
770 wmv2_pred_motion(w, &mx, &my); | |
771 | |
772 if(cbp){ | |
773 s->dsp.clear_blocks(s->block[0]); | |
774 if(s->per_mb_rl_table){ | |
775 s->rl_table_index = decode012(&s->gb); | |
776 s->rl_chroma_table_index = s->rl_table_index; | |
777 } | |
778 | |
779 if(w->abt_flag && w->per_mb_abt){ | |
780 w->per_block_abt= get_bits1(&s->gb); | |
781 if(!w->per_block_abt) | |
782 w->abt_type= decode012(&s->gb); | |
783 }else | |
784 w->per_block_abt=0; | |
785 } | |
786 | |
787 if (wmv2_decode_motion(w, &mx, &my) < 0) | |
788 return -1; | |
789 | |
790 s->mv_dir = MV_DIR_FORWARD; | |
791 s->mv_type = MV_TYPE_16X16; | |
792 s->mv[0][0][0] = mx; | |
793 s->mv[0][0][1] = my; | |
794 | |
795 for (i = 0; i < 6; i++) { | |
796 if (wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
797 { | |
798 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding inter block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
799 return -1; | |
800 } | |
801 } | |
802 } else { | |
803 //if(s->pict_type==P_TYPE) | |
804 // printf("%d%d ", s->inter_intra_pred, cbp); | |
805 //printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24)); | |
806 s->ac_pred = get_bits1(&s->gb); | |
807 if(s->inter_intra_pred){ | |
808 s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1); | |
809 // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y); | |
810 } | |
811 if(s->per_mb_rl_table && cbp){ | |
812 s->rl_table_index = decode012(&s->gb); | |
813 s->rl_chroma_table_index = s->rl_table_index; | |
814 } | |
815 | |
816 s->dsp.clear_blocks(s->block[0]); | |
817 for (i = 0; i < 6; i++) { | |
818 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) | |
819 { | |
820 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding intra block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
821 return -1; | |
822 } | |
823 } | |
824 } | |
825 | |
826 return 0; | |
827 } | |
828 | |
829 static int wmv2_decode_init(AVCodecContext *avctx){ | |
830 Wmv2Context * const w= avctx->priv_data; | |
831 | |
832 if(avctx->idct_algo==FF_IDCT_AUTO){ | |
833 avctx->idct_algo=FF_IDCT_WMV2; | |
834 } | |
835 | |
836 if(ff_h263_decode_init(avctx) < 0) | |
837 return -1; | |
838 | |
839 wmv2_common_init(w); | |
840 | |
841 ff_intrax8_common_init(&w->x8,&w->s); | |
842 | |
843 return 0; | |
844 } | |
845 | |
846 #ifdef CONFIG_WMV2_DECODER | |
847 AVCodec wmv2_decoder = { | |
848 "wmv2", | |
849 CODEC_TYPE_VIDEO, | |
850 CODEC_ID_WMV2, | |
851 sizeof(Wmv2Context), | |
852 wmv2_decode_init, | |
853 NULL, | |
854 ff_h263_decode_end, | |
855 ff_h263_decode_frame, | |
856 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1, | |
857 }; | |
858 #endif | |
859 | |
860 #ifdef CONFIG_WMV2_ENCODER | |
861 AVCodec wmv2_encoder = { | |
862 "wmv2", | |
863 CODEC_TYPE_VIDEO, | |
864 CODEC_ID_WMV2, | |
865 sizeof(Wmv2Context), | |
866 wmv2_encode_init, | |
867 MPV_encode_picture, | |
868 MPV_encode_end, | |
869 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, | |
870 }; | |
871 #endif |