remove unneeded casts
[libav.git] / libavcodec / cavs.c
1 /*
2 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
3 * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "avcodec.h"
21 #include "bitstream.h"
22 #include "golomb.h"
23 #include "mpegvideo.h"
24 #include "cavsdata.h"
25
26 typedef struct {
27 MpegEncContext s;
28 Picture picture; //currently decoded frame
29 Picture DPB[2]; //reference frames
30 int dist[2]; //temporal distances from current frame to ref frames
31 int profile, level;
32 int aspect_ratio;
33 int mb_width, mb_height;
34 int pic_type;
35 int progressive;
36 int pic_structure;
37 int skip_mode_flag;
38 int loop_filter_disable;
39 int alpha_offset, beta_offset;
40 int ref_flag;
41 int mbx, mby;
42 int flags;
43 int stc;
44 uint8_t *cy, *cu, *cv;
45 int left_qp;
46 uint8_t *top_qp;
47
48 /* mv motion vector cache
49 0: D3 B2 B3 C2
50 4: A1 X0 X1 -
51 8: A3 X2 X3 -
52
53 X are the vectors in the current macroblock (5,6,9,10)
54 A is the macroblock to the left (4,8)
55 B is the macroblock to the top (1,2)
56 C is the macroblock to the top-right (3)
57 D is the macroblock to the top-left (0)
58
59 the same is repeated for backward motion vectors */
60 vector_t mv[2*4*3];
61 vector_t *top_mv[2];
62 vector_t *col_mv;
63
64 /* luma pred mode cache
65 0: -- B2 B3
66 3: A1 X0 X1
67 6: A3 X2 X3 */
68 int pred_mode_Y[3*3];
69 int *top_pred_Y;
70 int l_stride, c_stride;
71 int luma_scan[4];
72 int qp;
73 int qp_fixed;
74 int cbp;
75
76 /* intra prediction is done with un-deblocked samples
77 they are saved here before deblocking the MB */
78 uint8_t *top_border_y, *top_border_u, *top_border_v;
79 uint8_t left_border_y[16], left_border_u[8], left_border_v[8];
80 uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
81
82 void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
83 void (*intra_pred_c[7])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
84 uint8_t *col_type_base;
85 uint8_t *col_type;
86 int sym_factor;
87 int direct_den[2];
88 int scale_den[2];
89 int got_keyframe;
90 } AVSContext;
91
92 /*****************************************************************************
93 *
94 * in-loop deblocking filter
95 *
96 ****************************************************************************/
97
98 static inline int get_bs_p(vector_t *mvP, vector_t *mvQ) {
99 if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA))
100 return 2;
101 if(mvP->ref != mvQ->ref)
102 return 1;
103 if( (abs(mvP->x - mvQ->x) >= 4) || (abs(mvP->y - mvQ->y) >= 4) )
104 return 1;
105 return 0;
106 }
107
108 static inline int get_bs_b(vector_t *mvP, vector_t *mvQ) {
109 if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA)) {
110 return 2;
111 } else {
112 vector_t *mvPbw = mvP + MV_BWD_OFFS;
113 vector_t *mvQbw = mvQ + MV_BWD_OFFS;
114 if( (abs( mvP->x - mvQ->x) >= 4) ||
115 (abs( mvP->y - mvQ->y) >= 4) ||
116 (abs(mvPbw->x - mvQbw->x) >= 4) ||
117 (abs(mvPbw->y - mvQbw->y) >= 4) )
118 return 1;
119 }
120 return 0;
121 }
122
123 /* boundary strength (bs) mapping:
124 *
125 * --4---5--
126 * 0 2 |
127 * | 6 | 7 |
128 * 1 3 |
129 * ---------
130 *
131 */
132
133 #define SET_PARAMS \
134 alpha = alpha_tab[clip(qp_avg + h->alpha_offset,0,63)]; \
135 beta = beta_tab[clip(qp_avg + h->beta_offset, 0,63)]; \
136 tc = tc_tab[clip(qp_avg + h->alpha_offset,0,63)];
137
138 static void filter_mb(AVSContext *h, enum mb_t mb_type) {
139 uint8_t bs[8];
140 int qp_avg, alpha, beta, tc;
141 int i;
142
143 /* save un-deblocked lines */
144 h->topleft_border_y = h->top_border_y[h->mbx*16+15];
145 h->topleft_border_u = h->top_border_u[h->mbx*8+7];
146 h->topleft_border_v = h->top_border_v[h->mbx*8+7];
147 memcpy(&h->top_border_y[h->mbx*16], h->cy + 15* h->l_stride,16);
148 memcpy(&h->top_border_u[h->mbx* 8], h->cu + 7* h->c_stride,8);
149 memcpy(&h->top_border_v[h->mbx* 8], h->cv + 7* h->c_stride,8);
150 for(i=0;i<8;i++) {
151 h->left_border_y[i*2+0] = *(h->cy + 15 + (i*2+0)*h->l_stride);
152 h->left_border_y[i*2+1] = *(h->cy + 15 + (i*2+1)*h->l_stride);
153 h->left_border_u[i] = *(h->cu + 7 + i*h->c_stride);
154 h->left_border_v[i] = *(h->cv + 7 + i*h->c_stride);
155 }
156 if(!h->loop_filter_disable) {
157 /* clear bs */
158 *((uint64_t *)bs) = 0;
159 /* determine bs */
160 switch(mb_type) {
161 case I_8X8:
162 *((uint64_t *)bs) = 0x0202020202020202ULL;
163 break;
164 case P_8X8:
165 case P_8X16:
166 bs[2] = get_bs_p(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1]);
167 bs[3] = get_bs_p(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3]);
168 case P_16X8:
169 bs[6] = get_bs_p(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2]);
170 bs[7] = get_bs_p(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3]);
171 case P_16X16:
172 case P_SKIP:
173 bs[0] = get_bs_p(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0]);
174 bs[1] = get_bs_p(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2]);
175 bs[4] = get_bs_p(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0]);
176 bs[5] = get_bs_p(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1]);
177 break;
178 case B_SKIP:
179 case B_DIRECT:
180 case B_8X8:
181 bs[2] = get_bs_b(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1]);
182 bs[3] = get_bs_b(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3]);
183 bs[6] = get_bs_b(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2]);
184 bs[7] = get_bs_b(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3]);
185 case B_FWD_16X16:
186 case B_BWD_16X16:
187 case B_SYM_16X16:
188 bs[0] = get_bs_b(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0]);
189 bs[1] = get_bs_b(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2]);
190 bs[4] = get_bs_b(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0]);
191 bs[5] = get_bs_b(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1]);
192 break;
193 default:
194 if(mb_type & 1) { //16X8
195 bs[6] = bs[7] = get_bs_b(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2]);
196 } else { //8X16
197 bs[2] = bs[3] = get_bs_b(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1]);
198 }
199 bs[0] = get_bs_b(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0]);
200 bs[1] = get_bs_b(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2]);
201 bs[4] = get_bs_b(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0]);
202 bs[5] = get_bs_b(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1]);
203 }
204 if( *((uint64_t *)bs) ) {
205 if(h->flags & A_AVAIL) {
206 qp_avg = (h->qp + h->left_qp + 1) >> 1;
207 SET_PARAMS;
208 h->s.dsp.cavs_filter_lv(h->cy,h->l_stride,alpha,beta,tc,bs[0],bs[1]);
209 h->s.dsp.cavs_filter_cv(h->cu,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
210 h->s.dsp.cavs_filter_cv(h->cv,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
211 }
212 qp_avg = h->qp;
213 SET_PARAMS;
214 h->s.dsp.cavs_filter_lv(h->cy + 8,h->l_stride,alpha,beta,tc,bs[2],bs[3]);
215 h->s.dsp.cavs_filter_lh(h->cy + 8*h->l_stride,h->l_stride,alpha,beta,tc,
216 bs[6],bs[7]);
217
218 if(h->flags & B_AVAIL) {
219 qp_avg = (h->qp + h->top_qp[h->mbx] + 1) >> 1;
220 SET_PARAMS;
221 h->s.dsp.cavs_filter_lh(h->cy,h->l_stride,alpha,beta,tc,bs[4],bs[5]);
222 h->s.dsp.cavs_filter_ch(h->cu,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
223 h->s.dsp.cavs_filter_ch(h->cv,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
224 }
225 }
226 }
227 h->left_qp = h->qp;
228 h->top_qp[h->mbx] = h->qp;
229 }
230
231 #undef SET_PARAMS
232
233 /*****************************************************************************
234 *
235 * spatial intra prediction
236 *
237 ****************************************************************************/
238
239 static inline void load_intra_pred_luma(AVSContext *h, uint8_t *top,
240 uint8_t *left, int block) {
241 int i;
242
243 switch(block) {
244 case 0:
245 memcpy(&left[1],h->left_border_y,16);
246 left[0] = left[1];
247 left[17] = left[16];
248 memcpy(&top[1],&h->top_border_y[h->mbx*16],16);
249 top[17] = top[16];
250 top[0] = top[1];
251 if((h->flags & A_AVAIL) && (h->flags & B_AVAIL))
252 left[0] = top[0] = h->topleft_border_y;
253 break;
254 case 1:
255 for(i=0;i<8;i++)
256 left[i+1] = *(h->cy + 7 + i*h->l_stride);
257 memset(&left[9],left[8],9);
258 left[0] = left[1];
259 memcpy(&top[1],&h->top_border_y[h->mbx*16+8],8);
260 if(h->flags & C_AVAIL)
261 memcpy(&top[9],&h->top_border_y[(h->mbx + 1)*16],8);
262 else
263 memset(&top[9],top[8],9);
264 top[17] = top[16];
265 top[0] = top[1];
266 if(h->flags & B_AVAIL)
267 left[0] = top[0] = h->top_border_y[h->mbx*16+7];
268 break;
269 case 2:
270 memcpy(&left[1],&h->left_border_y[8],8);
271 memset(&left[9],left[8],9);
272 memcpy(&top[1],h->cy + 7*h->l_stride,16);
273 top[17] = top[16];
274 left[0] = h->left_border_y[7];
275 top[0] = top[1];
276 if(h->flags & A_AVAIL)
277 top[0] = left[0];
278 break;
279 case 3:
280 for(i=0;i<9;i++)
281 left[i] = *(h->cy + 7 + (i+7)*h->l_stride);
282 memset(&left[9],left[8],9);
283 memcpy(&top[0],h->cy + 7 + 7*h->l_stride,9);
284 memset(&top[9],top[8],9);
285 break;
286 }
287 }
288
289 static inline void load_intra_pred_chroma(uint8_t *stop, uint8_t *sleft,
290 uint8_t stopleft, uint8_t *dtop,
291 uint8_t *dleft, int stride, int flags) {
292 int i;
293
294 if(flags & A_AVAIL) {
295 for(i=0; i<8; i++)
296 dleft[i+1] = sleft[i];
297 dleft[0] = dleft[1];
298 dleft[9] = dleft[8];
299 }
300 if(flags & B_AVAIL) {
301 for(i=0; i<8; i++)
302 dtop[i+1] = stop[i];
303 dtop[0] = dtop[1];
304 dtop[9] = dtop[8];
305 if(flags & A_AVAIL)
306 dleft[0] = dtop[0] = stopleft;
307 }
308 }
309
310 static void intra_pred_vert(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
311 int y;
312 uint64_t a = *((uint64_t *)(&top[1]));
313 for(y=0;y<8;y++) {
314 *((uint64_t *)(d+y*stride)) = a;
315 }
316 }
317
318 static void intra_pred_horiz(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
319 int y;
320 uint64_t a;
321 for(y=0;y<8;y++) {
322 a = left[y+1] * 0x0101010101010101ULL;
323 *((uint64_t *)(d+y*stride)) = a;
324 }
325 }
326
327 static void intra_pred_dc_128(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
328 int y;
329 uint64_t a = 0x8080808080808080ULL;
330 for(y=0;y<8;y++)
331 *((uint64_t *)(d+y*stride)) = a;
332 }
333
334 static void intra_pred_plane(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
335 int x,y,ia;
336 int ih = 0;
337 int iv = 0;
338 uint8_t *cm = cropTbl + MAX_NEG_CROP;
339
340 for(x=0; x<4; x++) {
341 ih += (x+1)*(top[5+x]-top[3-x]);
342 iv += (x+1)*(left[5+x]-left[3-x]);
343 }
344 ia = (top[8]+left[8])<<4;
345 ih = (17*ih+16)>>5;
346 iv = (17*iv+16)>>5;
347 for(y=0; y<8; y++)
348 for(x=0; x<8; x++)
349 d[y*stride+x] = cm[(ia+(x-3)*ih+(y-3)*iv+16)>>5];
350 }
351
352 #define LOWPASS(ARRAY,INDEX) \
353 (( ARRAY[(INDEX)-1] + 2*ARRAY[(INDEX)] + ARRAY[(INDEX)+1] + 2) >> 2)
354
355 static void intra_pred_lp(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
356 int x,y;
357 for(y=0; y<8; y++)
358 for(x=0; x<8; x++)
359 d[y*stride+x] = (LOWPASS(top,x+1) + LOWPASS(left,y+1)) >> 1;
360 }
361
362 static void intra_pred_down_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
363 int x,y;
364 for(y=0; y<8; y++)
365 for(x=0; x<8; x++)
366 d[y*stride+x] = (LOWPASS(top,x+y+2) + LOWPASS(left,x+y+2)) >> 1;
367 }
368
369 static void intra_pred_down_right(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
370 int x,y;
371 for(y=0; y<8; y++)
372 for(x=0; x<8; x++)
373 if(x==y)
374 d[y*stride+x] = (left[1]+2*top[0]+top[1]+2)>>2;
375 else if(x>y)
376 d[y*stride+x] = LOWPASS(top,x-y);
377 else
378 d[y*stride+x] = LOWPASS(left,y-x);
379 }
380
381 static void intra_pred_lp_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
382 int x,y;
383 for(y=0; y<8; y++)
384 for(x=0; x<8; x++)
385 d[y*stride+x] = LOWPASS(left,y+1);
386 }
387
388 static void intra_pred_lp_top(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
389 int x,y;
390 for(y=0; y<8; y++)
391 for(x=0; x<8; x++)
392 d[y*stride+x] = LOWPASS(top,x+1);
393 }
394
395 #undef LOWPASS
396
397 static inline void modify_pred(const int8_t *mod_table, int *mode) {
398 int newmode = mod_table[*mode];
399 if(newmode < 0) {
400 av_log(NULL, AV_LOG_ERROR, "Illegal intra prediction mode\n");
401 *mode = 0;
402 } else {
403 *mode = newmode;
404 }
405 }
406
407 /*****************************************************************************
408 *
409 * motion compensation
410 *
411 ****************************************************************************/
412
413 static inline void mc_dir_part(AVSContext *h,Picture *pic,int square,
414 int chroma_height,int delta,int list,uint8_t *dest_y,
415 uint8_t *dest_cb,uint8_t *dest_cr,int src_x_offset,
416 int src_y_offset,qpel_mc_func *qpix_op,
417 h264_chroma_mc_func chroma_op,vector_t *mv){
418 MpegEncContext * const s = &h->s;
419 const int mx= mv->x + src_x_offset*8;
420 const int my= mv->y + src_y_offset*8;
421 const int luma_xy= (mx&3) + ((my&3)<<2);
422 uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->l_stride;
423 uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->c_stride;
424 uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->c_stride;
425 int extra_width= 0; //(s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
426 int extra_height= extra_width;
427 int emu=0;
428 const int full_mx= mx>>2;
429 const int full_my= my>>2;
430 const int pic_width = 16*h->mb_width;
431 const int pic_height = 16*h->mb_height;
432
433 if(!pic->data[0])
434 return;
435 if(mx&7) extra_width -= 3;
436 if(my&7) extra_height -= 3;
437
438 if( full_mx < 0-extra_width
439 || full_my < 0-extra_height
440 || full_mx + 16/*FIXME*/ > pic_width + extra_width
441 || full_my + 16/*FIXME*/ > pic_height + extra_height){
442 ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->l_stride, h->l_stride,
443 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
444 src_y= s->edge_emu_buffer + 2 + 2*h->l_stride;
445 emu=1;
446 }
447
448 qpix_op[luma_xy](dest_y, src_y, h->l_stride); //FIXME try variable height perhaps?
449 if(!square){
450 qpix_op[luma_xy](dest_y + delta, src_y + delta, h->l_stride);
451 }
452
453 if(emu){
454 ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->c_stride,
455 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
456 src_cb= s->edge_emu_buffer;
457 }
458 chroma_op(dest_cb, src_cb, h->c_stride, chroma_height, mx&7, my&7);
459
460 if(emu){
461 ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->c_stride,
462 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
463 src_cr= s->edge_emu_buffer;
464 }
465 chroma_op(dest_cr, src_cr, h->c_stride, chroma_height, mx&7, my&7);
466 }
467
468 static inline void mc_part_std(AVSContext *h,int square,int chroma_height,int delta,
469 uint8_t *dest_y,uint8_t *dest_cb,uint8_t *dest_cr,
470 int x_offset, int y_offset,qpel_mc_func *qpix_put,
471 h264_chroma_mc_func chroma_put,qpel_mc_func *qpix_avg,
472 h264_chroma_mc_func chroma_avg, vector_t *mv){
473 qpel_mc_func *qpix_op= qpix_put;
474 h264_chroma_mc_func chroma_op= chroma_put;
475
476 dest_y += 2*x_offset + 2*y_offset*h->l_stride;
477 dest_cb += x_offset + y_offset*h->c_stride;
478 dest_cr += x_offset + y_offset*h->c_stride;
479 x_offset += 8*h->mbx;
480 y_offset += 8*h->mby;
481
482 if(mv->ref >= 0){
483 Picture *ref= &h->DPB[mv->ref];
484 mc_dir_part(h, ref, square, chroma_height, delta, 0,
485 dest_y, dest_cb, dest_cr, x_offset, y_offset,
486 qpix_op, chroma_op, mv);
487
488 qpix_op= qpix_avg;
489 chroma_op= chroma_avg;
490 }
491
492 if((mv+MV_BWD_OFFS)->ref >= 0){
493 Picture *ref= &h->DPB[0];
494 mc_dir_part(h, ref, square, chroma_height, delta, 1,
495 dest_y, dest_cb, dest_cr, x_offset, y_offset,
496 qpix_op, chroma_op, mv+MV_BWD_OFFS);
497 }
498 }
499
500 static void inter_pred(AVSContext *h) {
501 /* always do 8x8 blocks TODO: are larger blocks worth it? */
502 mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 0,
503 h->s.dsp.put_cavs_qpel_pixels_tab[1],
504 h->s.dsp.put_h264_chroma_pixels_tab[1],
505 h->s.dsp.avg_cavs_qpel_pixels_tab[1],
506 h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X0]);
507 mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 0,
508 h->s.dsp.put_cavs_qpel_pixels_tab[1],
509 h->s.dsp.put_h264_chroma_pixels_tab[1],
510 h->s.dsp.avg_cavs_qpel_pixels_tab[1],
511 h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X1]);
512 mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 4,
513 h->s.dsp.put_cavs_qpel_pixels_tab[1],
514 h->s.dsp.put_h264_chroma_pixels_tab[1],
515 h->s.dsp.avg_cavs_qpel_pixels_tab[1],
516 h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X2]);
517 mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 4,
518 h->s.dsp.put_cavs_qpel_pixels_tab[1],
519 h->s.dsp.put_h264_chroma_pixels_tab[1],
520 h->s.dsp.avg_cavs_qpel_pixels_tab[1],
521 h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X3]);
522 /* set intra prediction modes to default values */
523 h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
524 h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
525 }
526
527 /*****************************************************************************
528 *
529 * motion vector prediction
530 *
531 ****************************************************************************/
532
533 static inline void set_mvs(vector_t *mv, enum block_t size) {
534 switch(size) {
535 case BLK_16X16:
536 mv[MV_STRIDE ] = mv[0];
537 mv[MV_STRIDE+1] = mv[0];
538 case BLK_16X8:
539 mv[1] = mv[0];
540 break;
541 case BLK_8X16:
542 mv[MV_STRIDE] = mv[0];
543 break;
544 }
545 }
546
547 static inline void store_mvs(AVSContext *h) {
548 h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 0] = h->mv[MV_FWD_X0];
549 h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 1] = h->mv[MV_FWD_X1];
550 h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 2] = h->mv[MV_FWD_X2];
551 h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 3] = h->mv[MV_FWD_X3];
552 }
553
554 static inline void scale_mv(AVSContext *h, int *d_x, int *d_y, vector_t *src, int distp) {
555 int den = h->scale_den[src->ref];
556
557 *d_x = (src->x*distp*den + 256 + (src->x>>31)) >> 9;
558 *d_y = (src->y*distp*den + 256 + (src->y>>31)) >> 9;
559 }
560
561 static inline void mv_pred_median(AVSContext *h, vector_t *mvP, vector_t *mvA, vector_t *mvB, vector_t *mvC) {
562 int ax, ay, bx, by, cx, cy;
563 int len_ab, len_bc, len_ca, len_mid;
564
565 /* scale candidates according to their temporal span */
566 scale_mv(h, &ax, &ay, mvA, mvP->dist);
567 scale_mv(h, &bx, &by, mvB, mvP->dist);
568 scale_mv(h, &cx, &cy, mvC, mvP->dist);
569 /* find the geometrical median of the three candidates */
570 len_ab = abs(ax - bx) + abs(ay - by);
571 len_bc = abs(bx - cx) + abs(by - cy);
572 len_ca = abs(cx - ax) + abs(cy - ay);
573 len_mid = mid_pred(len_ab, len_bc, len_ca);
574 if(len_mid == len_ab) {
575 mvP->x = cx;
576 mvP->y = cy;
577 } else if(len_mid == len_bc) {
578 mvP->x = ax;
579 mvP->y = ay;
580 } else {
581 mvP->x = bx;
582 mvP->y = by;
583 }
584 }
585
586 static inline void mv_pred_direct(AVSContext *h, vector_t *pmv_fw,
587 vector_t *pmv_bw, vector_t *col_mv) {
588 int den = h->direct_den[col_mv->ref];
589 int m = col_mv->x >> 31;
590
591 pmv_fw->dist = h->dist[1];
592 pmv_bw->dist = h->dist[0];
593 pmv_fw->ref = 1;
594 pmv_bw->ref = 0;
595 /* scale the co-located motion vector according to its temporal span */
596 pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
597 pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
598 m = col_mv->y >> 31;
599 pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
600 pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
601 }
602
603 static inline void mv_pred_sym(AVSContext *h, vector_t *src, enum block_t size) {
604 vector_t *dst = src + MV_BWD_OFFS;
605
606 /* backward mv is the scaled and negated forward mv */
607 dst->x = -((src->x * h->sym_factor + 256) >> 9);
608 dst->y = -((src->y * h->sym_factor + 256) >> 9);
609 dst->ref = 0;
610 dst->dist = h->dist[0];
611 set_mvs(dst, size);
612 }
613
614 static void mv_pred(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC,
615 enum mv_pred_t mode, enum block_t size, int ref) {
616 vector_t *mvP = &h->mv[nP];
617 vector_t *mvA = &h->mv[nP-1];
618 vector_t *mvB = &h->mv[nP-4];
619 vector_t *mvC = &h->mv[nC];
620 int mvAref = mvA->ref;
621 int mvBref = mvB->ref;
622 int mvCref;
623
624 mvP->ref = ref;
625 mvP->dist = h->dist[mvP->ref];
626 if(mvC->ref == NOT_AVAIL)
627 mvC = &h->mv[nP-5]; // set to top-left (mvD)
628 mvCref = mvC->ref;
629 if(mode == MV_PRED_PSKIP) {
630 if((mvAref == NOT_AVAIL) || (mvBref == NOT_AVAIL) ||
631 ((mvA->x | mvA->y | mvA->ref) == 0) ||
632 ((mvB->x | mvB->y | mvB->ref) == 0) ) {
633 mvP->x = mvP->y = 0;
634 set_mvs(mvP,size);
635 return;
636 }
637 }
638 /* if there is only one suitable candidate, take it */
639 if((mvAref >= 0) && (mvBref < 0) && (mvCref < 0)) {
640 mvP->x = mvA->x;
641 mvP->y = mvA->y;
642 } else if((mvAref < 0) && (mvBref >= 0) && (mvCref < 0)) {
643 mvP->x = mvB->x;
644 mvP->y = mvB->y;
645 } else if((mvAref < 0) && (mvBref < 0) && (mvCref >= 0)) {
646 mvP->x = mvC->x;
647 mvP->y = mvC->y;
648 } else {
649 switch(mode) {
650 case MV_PRED_LEFT:
651 if(mvAref == mvP->ref) {
652 mvP->x = mvA->x;
653 mvP->y = mvA->y;
654 } else
655 mv_pred_median(h, mvP, mvA, mvB, mvC);
656 break;
657 case MV_PRED_TOP:
658 if(mvBref == mvP->ref) {
659 mvP->x = mvB->x;
660 mvP->y = mvB->y;
661 } else
662 mv_pred_median(h, mvP, mvA, mvB, mvC);
663 break;
664 case MV_PRED_TOPRIGHT:
665 if(mvCref == mvP->ref) {
666 mvP->x = mvC->x;
667 mvP->y = mvC->y;
668 } else
669 mv_pred_median(h, mvP, mvA, mvB, mvC);
670 break;
671 default:
672 mv_pred_median(h, mvP, mvA, mvB, mvC);
673 break;
674 }
675 }
676 if(mode < MV_PRED_PSKIP) {
677 mvP->x += get_se_golomb(&h->s.gb);
678 mvP->y += get_se_golomb(&h->s.gb);
679 }
680 set_mvs(mvP,size);
681 }
682
683 /*****************************************************************************
684 *
685 * residual data decoding
686 *
687 ****************************************************************************/
688
689 /* kth-order exponential golomb code */
690 static inline int get_ue_code(GetBitContext *gb, int order) {
691 if(order)
692 return (get_ue_golomb(gb) << order) + get_bits(gb,order);
693 return get_ue_golomb(gb);
694 }
695
696 static int decode_residual_block(AVSContext *h, GetBitContext *gb,
697 const residual_vlc_t *r, int esc_golomb_order,
698 int qp, uint8_t *dst, int stride) {
699 int i,pos = -1;
700 int level_code, esc_code, level, run, mask;
701 int level_buf[64];
702 int run_buf[64];
703 int dqm = dequant_mul[qp];
704 int dqs = dequant_shift[qp];
705 int dqa = 1 << (dqs - 1);
706 const uint8_t *scantab = ff_zigzag_direct;
707 DCTELEM block[64];
708
709 memset(block,0,64*sizeof(DCTELEM));
710 for(i=0;i<65;i++) {
711 level_code = get_ue_code(gb,r->golomb_order);
712 if(level_code >= ESCAPE_CODE) {
713 run = (level_code - ESCAPE_CODE) >> 1;
714 esc_code = get_ue_code(gb,esc_golomb_order);
715 level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
716 while(level > r->inc_limit)
717 r++;
718 mask = -(level_code & 1);
719 level = (level^mask) - mask;
720 } else {
721 if(level_code < 0)
722 return -1;
723 level = r->rltab[level_code][0];
724 if(!level) //end of block signal
725 break;
726 run = r->rltab[level_code][1];
727 r += r->rltab[level_code][2];
728 }
729 level_buf[i] = level;
730 run_buf[i] = run;
731 }
732 /* inverse scan and dequantization */
733 for(i=i-1;i>=0;i--) {
734 pos += 1 + run_buf[i];
735 if(pos > 63) {
736 av_log(h->s.avctx, AV_LOG_ERROR,
737 "position out of block bounds at pic %d MB(%d,%d)\n",
738 h->picture.poc, h->mbx, h->mby);
739 return -1;
740 }
741 block[scantab[pos]] = (level_buf[i]*dqm + dqa) >> dqs;
742 }
743 h->s.dsp.cavs_idct8_add(dst,block,stride);
744 return 0;
745 }
746
747
748 static inline void decode_residual_chroma(AVSContext *h) {
749 if(h->cbp & (1<<4))
750 decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
751 h->cu,h->c_stride);
752 if(h->cbp & (1<<5))
753 decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
754 h->cv,h->c_stride);
755 }
756
757 static inline void decode_residual_inter(AVSContext *h) {
758 int block;
759
760 /* get coded block pattern */
761 h->cbp = cbp_tab[get_ue_golomb(&h->s.gb)][1];
762 /* get quantizer */
763 if(h->cbp && !h->qp_fixed)
764 h->qp += get_se_golomb(&h->s.gb);
765 for(block=0;block<4;block++)
766 if(h->cbp & (1<<block))
767 decode_residual_block(h,&h->s.gb,inter_2dvlc,0,h->qp,
768 h->cy + h->luma_scan[block], h->l_stride);
769 decode_residual_chroma(h);
770 }
771
772 /*****************************************************************************
773 *
774 * macroblock level
775 *
776 ****************************************************************************/
777
778 static inline void init_mb(AVSContext *h) {
779 int i;
780
781 /* copy predictors from top line (MB B and C) into cache */
782 for(i=0;i<3;i++) {
783 h->mv[MV_FWD_B2+i] = h->top_mv[0][h->mbx*2+i];
784 h->mv[MV_BWD_B2+i] = h->top_mv[1][h->mbx*2+i];
785 }
786 h->pred_mode_Y[1] = h->top_pred_Y[h->mbx*2+0];
787 h->pred_mode_Y[2] = h->top_pred_Y[h->mbx*2+1];
788 /* clear top predictors if MB B is not available */
789 if(!(h->flags & B_AVAIL)) {
790 h->mv[MV_FWD_B2] = un_mv;
791 h->mv[MV_FWD_B3] = un_mv;
792 h->mv[MV_BWD_B2] = un_mv;
793 h->mv[MV_BWD_B3] = un_mv;
794 h->pred_mode_Y[1] = h->pred_mode_Y[2] = NOT_AVAIL;
795 h->flags &= ~(C_AVAIL|D_AVAIL);
796 } else if(h->mbx) {
797 h->flags |= D_AVAIL;
798 }
799 if(h->mbx == h->mb_width-1) //MB C not available
800 h->flags &= ~C_AVAIL;
801 /* clear top-right predictors if MB C is not available */
802 if(!(h->flags & C_AVAIL)) {
803 h->mv[MV_FWD_C2] = un_mv;
804 h->mv[MV_BWD_C2] = un_mv;
805 }
806 /* clear top-left predictors if MB D is not available */
807 if(!(h->flags & D_AVAIL)) {
808 h->mv[MV_FWD_D3] = un_mv;
809 h->mv[MV_BWD_D3] = un_mv;
810 }
811 /* set pointer for co-located macroblock type */
812 h->col_type = &h->col_type_base[h->mby*h->mb_width + h->mbx];
813 }
814
815 static inline void check_for_slice(AVSContext *h);
816
817 static inline int next_mb(AVSContext *h) {
818 int i;
819
820 h->flags |= A_AVAIL;
821 h->cy += 16;
822 h->cu += 8;
823 h->cv += 8;
824 /* copy mvs as predictors to the left */
825 for(i=0;i<=20;i+=4)
826 h->mv[i] = h->mv[i+2];
827 /* copy bottom mvs from cache to top line */
828 h->top_mv[0][h->mbx*2+0] = h->mv[MV_FWD_X2];
829 h->top_mv[0][h->mbx*2+1] = h->mv[MV_FWD_X3];
830 h->top_mv[1][h->mbx*2+0] = h->mv[MV_BWD_X2];
831 h->top_mv[1][h->mbx*2+1] = h->mv[MV_BWD_X3];
832 /* next MB address */
833 h->mbx++;
834 if(h->mbx == h->mb_width) { //new mb line
835 h->flags = B_AVAIL|C_AVAIL;
836 /* clear left pred_modes */
837 h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
838 /* clear left mv predictors */
839 for(i=0;i<=20;i+=4)
840 h->mv[i] = un_mv;
841 h->mbx = 0;
842 h->mby++;
843 /* re-calculate sample pointers */
844 h->cy = h->picture.data[0] + h->mby*16*h->l_stride;
845 h->cu = h->picture.data[1] + h->mby*8*h->c_stride;
846 h->cv = h->picture.data[2] + h->mby*8*h->c_stride;
847 if(h->mby == h->mb_height) { //frame end
848 return 0;
849 } else {
850 //check_for_slice(h);
851 }
852 }
853 return 1;
854 }
855
856 static void decode_mb_i(AVSContext *h, int is_i_pic) {
857 GetBitContext *gb = &h->s.gb;
858 int block, pred_mode_uv;
859 uint8_t top[18];
860 uint8_t left[18];
861 uint8_t *d;
862
863 /* get intra prediction modes from stream */
864 for(block=0;block<4;block++) {
865 int nA,nB,predpred;
866 int pos = scan3x3[block];
867
868 nA = h->pred_mode_Y[pos-1];
869 nB = h->pred_mode_Y[pos-3];
870 if((nA == NOT_AVAIL) || (nB == NOT_AVAIL))
871 predpred = 2;
872 else
873 predpred = FFMIN(nA,nB);
874 if(get_bits1(gb))
875 h->pred_mode_Y[pos] = predpred;
876 else {
877 h->pred_mode_Y[pos] = get_bits(gb,2);
878 if(h->pred_mode_Y[pos] >= predpred)
879 h->pred_mode_Y[pos]++;
880 }
881 }
882 pred_mode_uv = get_ue_golomb(gb);
883 if(pred_mode_uv > 6) {
884 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
885 pred_mode_uv = 0;
886 }
887
888 /* save pred modes before they get modified */
889 h->pred_mode_Y[3] = h->pred_mode_Y[5];
890 h->pred_mode_Y[6] = h->pred_mode_Y[8];
891 h->top_pred_Y[h->mbx*2+0] = h->pred_mode_Y[7];
892 h->top_pred_Y[h->mbx*2+1] = h->pred_mode_Y[8];
893
894 /* modify pred modes according to availability of neighbour samples */
895 if(!(h->flags & A_AVAIL)) {
896 modify_pred(left_modifier_l, &h->pred_mode_Y[4] );
897 modify_pred(left_modifier_l, &h->pred_mode_Y[7] );
898 modify_pred(left_modifier_c, &pred_mode_uv );
899 }
900 if(!(h->flags & B_AVAIL)) {
901 modify_pred(top_modifier_l, &h->pred_mode_Y[4] );
902 modify_pred(top_modifier_l, &h->pred_mode_Y[5] );
903 modify_pred(top_modifier_c, &pred_mode_uv );
904 }
905
906 /* get coded block pattern */
907 if(is_i_pic)
908 h->cbp = cbp_tab[get_ue_golomb(gb)][0];
909 if(h->cbp && !h->qp_fixed)
910 h->qp += get_se_golomb(gb); //qp_delta
911
912 /* luma intra prediction interleaved with residual decode/transform/add */
913 for(block=0;block<4;block++) {
914 d = h->cy + h->luma_scan[block];
915 load_intra_pred_luma(h, top, left, block);
916 h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
917 (d, top, left, h->l_stride);
918 if(h->cbp & (1<<block))
919 decode_residual_block(h,gb,intra_2dvlc,1,h->qp,d,h->l_stride);
920 }
921
922 /* chroma intra prediction */
923 load_intra_pred_chroma(&h->top_border_u[h->mbx*8], h->left_border_u,
924 h->topleft_border_u, top, left, h->c_stride, h->flags);
925 h->intra_pred_c[pred_mode_uv](h->cu, top, left, h->c_stride);
926 load_intra_pred_chroma(&h->top_border_v[h->mbx*8], h->left_border_v,
927 h->topleft_border_v, top, left, h->c_stride, h->flags);
928 h->intra_pred_c[pred_mode_uv](h->cv, top, left, h->c_stride);
929
930 decode_residual_chroma(h);
931 filter_mb(h,I_8X8);
932
933 /* mark motion vectors as intra */
934 h->mv[MV_FWD_X0] = intra_mv;
935 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
936 h->mv[MV_BWD_X0] = intra_mv;
937 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
938 if(h->pic_type != FF_B_TYPE)
939 *h->col_type = I_8X8;
940 }
941
942 static void mb_skip_p(AVSContext *h) {
943 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
944 inter_pred(h);
945 store_mvs(h);
946 filter_mb(h,P_SKIP);
947 *h->col_type = P_SKIP;
948 }
949
950
951 static void mb_skip_b(AVSContext *h) {
952 int i;
953
954 if(!(*h->col_type)) {
955 /* intra MB at co-location, do in-plane prediction */
956 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
957 mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
958 } else {
959 /* direct prediction from co-located P MB, block-wise */
960 for(i=0;i<4;i++)
961 mv_pred_direct(h,&h->mv[mv_scan[i]],
962 &h->mv[mv_scan[i]+MV_BWD_OFFS],
963 &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + i]);
964 }
965 }
966
967 static void decode_mb_p(AVSContext *h, enum mb_t mb_type) {
968 GetBitContext *gb = &h->s.gb;
969 int ref[4];
970
971 switch(mb_type) {
972 case P_SKIP:
973 mb_skip_p(h);
974 return;
975 case P_16X16:
976 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
977 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
978 break;
979 case P_16X8:
980 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
981 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
982 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
983 mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
984 break;
985 case P_8X16:
986 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
987 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
988 mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
989 mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
990 break;
991 case P_8X8:
992 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
993 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
994 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
995 ref[3] = h->ref_flag ? 0 : get_bits1(gb);
996 mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
997 mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
998 mv_pred(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
999 mv_pred(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
1000 }
1001 inter_pred(h);
1002 store_mvs(h);
1003 decode_residual_inter(h);
1004 filter_mb(h,mb_type);
1005 *h->col_type = mb_type;
1006 }
1007
1008 static void decode_mb_b(AVSContext *h, enum mb_t mb_type) {
1009 int block;
1010 enum sub_mb_t sub_type[4];
1011 int flags;
1012
1013 /* reset all MVs */
1014 h->mv[MV_FWD_X0] = dir_mv;
1015 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
1016 h->mv[MV_BWD_X0] = dir_mv;
1017 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
1018 switch(mb_type) {
1019 case B_SKIP:
1020 mb_skip_b(h);
1021 inter_pred(h);
1022 filter_mb(h,B_SKIP);
1023 return;
1024 case B_DIRECT:
1025 mb_skip_b(h);
1026 break;
1027 case B_FWD_16X16:
1028 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
1029 break;
1030 case B_SYM_16X16:
1031 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
1032 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
1033 break;
1034 case B_BWD_16X16:
1035 mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
1036 break;
1037 case B_8X8:
1038 for(block=0;block<4;block++)
1039 sub_type[block] = get_bits(&h->s.gb,2);
1040 for(block=0;block<4;block++) {
1041 switch(sub_type[block]) {
1042 case B_SUB_DIRECT:
1043 if(!(*h->col_type)) {
1044 /* intra MB at co-location, do in-plane prediction */
1045 mv_pred(h, mv_scan[block], mv_scan[block]-3,
1046 MV_PRED_BSKIP, BLK_8X8, 1);
1047 mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
1048 mv_scan[block]-3+MV_BWD_OFFS,
1049 MV_PRED_BSKIP, BLK_8X8, 0);
1050 } else
1051 mv_pred_direct(h,&h->mv[mv_scan[block]],
1052 &h->mv[mv_scan[block]+MV_BWD_OFFS],
1053 &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + block]);
1054 break;
1055 case B_SUB_FWD:
1056 mv_pred(h, mv_scan[block], mv_scan[block]-3,
1057 MV_PRED_MEDIAN, BLK_8X8, 1);
1058 break;
1059 case B_SUB_SYM:
1060 mv_pred(h, mv_scan[block], mv_scan[block]-3,
1061 MV_PRED_MEDIAN, BLK_8X8, 1);
1062 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
1063 break;
1064 }
1065 }
1066 for(block=0;block<4;block++) {
1067 if(sub_type[block] == B_SUB_BWD)
1068 mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
1069 mv_scan[block]+MV_BWD_OFFS-3,
1070 MV_PRED_MEDIAN, BLK_8X8, 0);
1071 }
1072 break;
1073 default:
1074 assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
1075 flags = b_partition_flags[(mb_type-1)>>1];
1076 if(mb_type & 1) { /* 16x8 macroblock types */
1077 if(flags & FWD0)
1078 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
1079 if(flags & SYM0) {
1080 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
1081 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
1082 }
1083 if(flags & FWD1)
1084 mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
1085 if(flags & SYM1) {
1086 mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
1087 mv_pred_sym(h, &h->mv[9], BLK_16X8);
1088 }
1089 if(flags & BWD0)
1090 mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
1091 if(flags & BWD1)
1092 mv_pred(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
1093 } else { /* 8x16 macroblock types */
1094 if(flags & FWD0)
1095 mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
1096 if(flags & SYM0) {
1097 mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
1098 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
1099 }
1100 if(flags & FWD1)
1101 mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 1);
1102 if(flags & SYM1) {
1103 mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 1);
1104 mv_pred_sym(h, &h->mv[6], BLK_8X16);
1105 }
1106 if(flags & BWD0)
1107 mv_pred(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
1108 if(flags & BWD1)
1109 mv_pred(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 0);
1110 }
1111 }
1112 inter_pred(h);
1113 decode_residual_inter(h);
1114 filter_mb(h,mb_type);
1115 }
1116
1117 /*****************************************************************************
1118 *
1119 * slice level
1120 *
1121 ****************************************************************************/
1122
1123 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
1124 if(h->stc > 0xAF)
1125 av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
1126 h->mby = h->stc;
1127 if((h->mby == 0) && (!h->qp_fixed)){
1128 h->qp_fixed = get_bits1(gb);
1129 h->qp = get_bits(gb,6);
1130 }
1131 /* inter frame or second slice can have weighting params */
1132 if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
1133 if(get_bits1(gb)) { //slice_weighting_flag
1134 av_log(h->s.avctx, AV_LOG_ERROR,
1135 "weighted prediction not yet supported\n");
1136 }
1137 return 0;
1138 }
1139
1140 static inline void check_for_slice(AVSContext *h) {
1141 GetBitContext *gb = &h->s.gb;
1142 int align;
1143 align = (-get_bits_count(gb)) & 7;
1144 if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
1145 get_bits_long(gb,24+align);
1146 h->stc = get_bits(gb,8);
1147 decode_slice_header(h,gb);
1148 }
1149 }
1150
1151 /*****************************************************************************
1152 *
1153 * frame level
1154 *
1155 ****************************************************************************/
1156
1157 static void init_pic(AVSContext *h) {
1158 int i;
1159
1160 /* clear some predictors */
1161 for(i=0;i<=20;i+=4)
1162 h->mv[i] = un_mv;
1163 h->mv[MV_BWD_X0] = dir_mv;
1164 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
1165 h->mv[MV_FWD_X0] = dir_mv;
1166 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
1167 h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
1168 h->cy = h->picture.data[0];
1169 h->cu = h->picture.data[1];
1170 h->cv = h->picture.data[2];
1171 h->l_stride = h->picture.linesize[0];
1172 h->c_stride = h->picture.linesize[1];
1173 h->luma_scan[2] = 8*h->l_stride;
1174 h->luma_scan[3] = 8*h->l_stride+8;
1175 h->mbx = h->mby = 0;
1176 h->flags = 0;
1177 }
1178
1179 static int decode_pic(AVSContext *h) {
1180 MpegEncContext *s = &h->s;
1181 int i,skip_count;
1182 enum mb_t mb_type;
1183
1184 if (!s->context_initialized) {
1185 if (MPV_common_init(s) < 0)
1186 return -1;
1187 }
1188 get_bits(&s->gb,16);//bbv_dwlay
1189 if(h->stc == PIC_PB_START_CODE) {
1190 h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
1191 /* make sure we have the reference frames we need */
1192 if(!h->DPB[0].data[0] ||
1193 (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
1194 return -1;
1195 } else {
1196 h->pic_type = FF_I_TYPE;
1197 if(get_bits1(&s->gb))
1198 get_bits(&s->gb,16);//time_code
1199 }
1200 /* release last B frame */
1201 if(h->picture.data[0])
1202 s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
1203
1204 s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
1205 init_pic(h);
1206 h->picture.poc = get_bits(&s->gb,8)*2;
1207
1208 /* get temporal distances and MV scaling factors */
1209 if(h->pic_type != FF_B_TYPE) {
1210 h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
1211 } else {
1212 h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
1213 }
1214 h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
1215 h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
1216 h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
1217 if(h->pic_type == FF_B_TYPE) {
1218 h->sym_factor = h->dist[0]*h->scale_den[1];
1219 } else {
1220 h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
1221 h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
1222 }
1223
1224 if(s->low_delay)
1225 get_ue_golomb(&s->gb); //bbv_check_times
1226 h->progressive = get_bits1(&s->gb);
1227 if(h->progressive)
1228 h->pic_structure = 1;
1229 else if(!(h->pic_structure = get_bits1(&s->gb) && (h->stc == PIC_PB_START_CODE)) )
1230 get_bits1(&s->gb); //advanced_pred_mode_disable
1231 skip_bits1(&s->gb); //top_field_first
1232 skip_bits1(&s->gb); //repeat_first_field
1233 h->qp_fixed = get_bits1(&s->gb);
1234 h->qp = get_bits(&s->gb,6);
1235 if(h->pic_type == FF_I_TYPE) {
1236 if(!h->progressive && !h->pic_structure)
1237 skip_bits1(&s->gb);//what is this?
1238 skip_bits(&s->gb,4); //reserved bits
1239 } else {
1240 if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
1241 h->ref_flag = get_bits1(&s->gb);
1242 skip_bits(&s->gb,4); //reserved bits
1243 h->skip_mode_flag = get_bits1(&s->gb);
1244 }
1245 h->loop_filter_disable = get_bits1(&s->gb);
1246 if(!h->loop_filter_disable && get_bits1(&s->gb)) {
1247 h->alpha_offset = get_se_golomb(&s->gb);
1248 h->beta_offset = get_se_golomb(&s->gb);
1249 } else {
1250 h->alpha_offset = h->beta_offset = 0;
1251 }
1252 check_for_slice(h);
1253 if(h->pic_type == FF_I_TYPE) {
1254 do {
1255 init_mb(h);
1256 decode_mb_i(h,1);
1257 } while(next_mb(h));
1258 } else if(h->pic_type == FF_P_TYPE) {
1259 do {
1260 if(h->skip_mode_flag) {
1261 skip_count = get_ue_golomb(&s->gb);
1262 for(i=0;i<skip_count;i++) {
1263 init_mb(h);
1264 mb_skip_p(h);
1265 if(!next_mb(h))
1266 goto done;
1267 }
1268 mb_type = get_ue_golomb(&s->gb) + P_16X16;
1269 } else {
1270 mb_type = get_ue_golomb(&s->gb) + P_SKIP;
1271 }
1272 init_mb(h);
1273 if(mb_type > P_8X8) {
1274 h->cbp = cbp_tab[mb_type - P_8X8 - 1][0];
1275 decode_mb_i(h,0);
1276 } else {
1277 decode_mb_p(h,mb_type);
1278 }
1279 } while(next_mb(h));
1280 } else { //FF_B_TYPE
1281 do {
1282 if(h->skip_mode_flag) {
1283 skip_count = get_ue_golomb(&s->gb);
1284 for(i=0;i<skip_count;i++) {
1285 init_mb(h);
1286 mb_skip_b(h);
1287 inter_pred(h);
1288 filter_mb(h,B_SKIP);
1289 if(!next_mb(h))
1290 goto done;
1291 }
1292 mb_type = get_ue_golomb(&s->gb) + B_DIRECT;
1293 } else {
1294 mb_type = get_ue_golomb(&s->gb) + B_SKIP;
1295 }
1296 init_mb(h);
1297 if(mb_type > B_8X8) {
1298 h->cbp = cbp_tab[mb_type - B_8X8 - 1][0];
1299 decode_mb_i(h,0);
1300 } else {
1301 decode_mb_b(h,mb_type);
1302 }
1303 } while(next_mb(h));
1304 }
1305 done:
1306 if(h->pic_type != FF_B_TYPE) {
1307 if(h->DPB[1].data[0])
1308 s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
1309 memcpy(&h->DPB[1], &h->DPB[0], sizeof(Picture));
1310 memcpy(&h->DPB[0], &h->picture, sizeof(Picture));
1311 memset(&h->picture,0,sizeof(Picture));
1312 }
1313 return 0;
1314 }
1315
1316 /*****************************************************************************
1317 *
1318 * headers and interface
1319 *
1320 ****************************************************************************/
1321
1322 static void init_top_lines(AVSContext *h) {
1323 /* alloc top line of predictors */
1324 h->top_qp = av_malloc( h->mb_width);
1325 h->top_mv[0] = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
1326 h->top_mv[1] = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
1327 h->top_pred_Y = av_malloc( h->mb_width*2*sizeof(int));
1328 h->top_border_y = av_malloc((h->mb_width+1)*16);
1329 h->top_border_u = av_malloc((h->mb_width+1)*8);
1330 h->top_border_v = av_malloc((h->mb_width+1)*8);
1331
1332 /* alloc space for co-located MVs and types */
1333 h->col_mv = av_malloc( h->mb_width*h->mb_height*4*sizeof(vector_t));
1334 h->col_type_base = av_malloc(h->mb_width*h->mb_height);
1335 }
1336
1337 static int decode_seq_header(AVSContext *h) {
1338 MpegEncContext *s = &h->s;
1339 extern const AVRational frame_rate_tab[];
1340 int frame_rate_code;
1341
1342 h->profile = get_bits(&s->gb,8);
1343 h->level = get_bits(&s->gb,8);
1344 skip_bits1(&s->gb); //progressive sequence
1345 s->width = get_bits(&s->gb,14);
1346 s->height = get_bits(&s->gb,14);
1347 skip_bits(&s->gb,2); //chroma format
1348 skip_bits(&s->gb,3); //sample_precision
1349 h->aspect_ratio = get_bits(&s->gb,4);
1350 frame_rate_code = get_bits(&s->gb,4);
1351 skip_bits(&s->gb,18);//bit_rate_lower
1352 skip_bits1(&s->gb); //marker_bit
1353 skip_bits(&s->gb,12);//bit_rate_upper
1354 s->low_delay = get_bits1(&s->gb);
1355 h->mb_width = (s->width + 15) >> 4;
1356 h->mb_height = (s->height + 15) >> 4;
1357 h->s.avctx->time_base.den = frame_rate_tab[frame_rate_code].num;
1358 h->s.avctx->time_base.num = frame_rate_tab[frame_rate_code].den;
1359 h->s.avctx->width = s->width;
1360 h->s.avctx->height = s->height;
1361 if(!h->top_qp)
1362 init_top_lines(h);
1363 return 0;
1364 }
1365
1366 /**
1367 * finds the end of the current frame in the bitstream.
1368 * @return the position of the first byte of the next frame, or -1
1369 */
1370 int ff_cavs_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size) {
1371 int pic_found, i;
1372 uint32_t state;
1373
1374 pic_found= pc->frame_start_found;
1375 state= pc->state;
1376
1377 i=0;
1378 if(!pic_found){
1379 for(i=0; i<buf_size; i++){
1380 state= (state<<8) | buf[i];
1381 if(state == PIC_I_START_CODE || state == PIC_PB_START_CODE){
1382 i++;
1383 pic_found=1;
1384 break;
1385 }
1386 }
1387 }
1388
1389 if(pic_found){
1390 /* EOF considered as end of frame */
1391 if (buf_size == 0)
1392 return 0;
1393 for(; i<buf_size; i++){
1394 state= (state<<8) | buf[i];
1395 if((state&0xFFFFFF00) == 0x100){
1396 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
1397 pc->frame_start_found=0;
1398 pc->state=-1;
1399 return i-3;
1400 }
1401 }
1402 }
1403 }
1404 pc->frame_start_found= pic_found;
1405 pc->state= state;
1406 return END_NOT_FOUND;
1407 }
1408
1409 void ff_cavs_flush(AVCodecContext * avctx) {
1410 AVSContext *h = avctx->priv_data;
1411 h->got_keyframe = 0;
1412 }
1413
1414 static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
1415 uint8_t * buf, int buf_size) {
1416 AVSContext *h = avctx->priv_data;
1417 MpegEncContext *s = &h->s;
1418 int input_size;
1419 const uint8_t *buf_end;
1420 const uint8_t *buf_ptr;
1421 AVFrame *picture = data;
1422 uint32_t stc;
1423
1424 s->avctx = avctx;
1425
1426 if (buf_size == 0) {
1427 if(!s->low_delay && h->DPB[0].data[0]) {
1428 *data_size = sizeof(AVPicture);
1429 *picture = *(AVFrame *) &h->DPB[0];
1430 }
1431 return 0;
1432 }
1433
1434 buf_ptr = buf;
1435 buf_end = buf + buf_size;
1436 for(;;) {
1437 buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
1438 if(stc & 0xFFFFFE00)
1439 return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
1440 input_size = (buf_end - buf_ptr)*8;
1441 switch(stc) {
1442 case SEQ_START_CODE:
1443 init_get_bits(&s->gb, buf_ptr, input_size);
1444 decode_seq_header(h);
1445 break;
1446 case PIC_I_START_CODE:
1447 if(!h->got_keyframe) {
1448 if(h->DPB[0].data[0])
1449 avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
1450 if(h->DPB[1].data[0])
1451 avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
1452 h->got_keyframe = 1;
1453 }
1454 case PIC_PB_START_CODE:
1455 *data_size = 0;
1456 if(!h->got_keyframe)
1457 break;
1458 init_get_bits(&s->gb, buf_ptr, input_size);
1459 h->stc = stc;
1460 if(decode_pic(h))
1461 break;
1462 *data_size = sizeof(AVPicture);
1463 if(h->pic_type != FF_B_TYPE) {
1464 if(h->DPB[1].data[0]) {
1465 *picture = *(AVFrame *) &h->DPB[1];
1466 } else {
1467 *data_size = 0;
1468 }
1469 } else
1470 *picture = *(AVFrame *) &h->picture;
1471 break;
1472 case EXT_START_CODE:
1473 //mpeg_decode_extension(avctx,buf_ptr, input_size);
1474 break;
1475 case USER_START_CODE:
1476 //mpeg_decode_user_data(avctx,buf_ptr, input_size);
1477 break;
1478 default:
1479 if (stc >= SLICE_MIN_START_CODE &&
1480 stc <= SLICE_MAX_START_CODE) {
1481 init_get_bits(&s->gb, buf_ptr, input_size);
1482 decode_slice_header(h, &s->gb);
1483 }
1484 break;
1485 }
1486 }
1487 }
1488
1489 static int cavs_decode_init(AVCodecContext * avctx) {
1490 AVSContext *h = avctx->priv_data;
1491 MpegEncContext * const s = &h->s;
1492
1493 MPV_decode_defaults(s);
1494 s->avctx = avctx;
1495
1496 avctx->pix_fmt= PIX_FMT_YUV420P;
1497
1498 h->luma_scan[0] = 0;
1499 h->luma_scan[1] = 8;
1500 h->intra_pred_l[ INTRA_L_VERT] = intra_pred_vert;
1501 h->intra_pred_l[ INTRA_L_HORIZ] = intra_pred_horiz;
1502 h->intra_pred_l[ INTRA_L_LP] = intra_pred_lp;
1503 h->intra_pred_l[ INTRA_L_DOWN_LEFT] = intra_pred_down_left;
1504 h->intra_pred_l[INTRA_L_DOWN_RIGHT] = intra_pred_down_right;
1505 h->intra_pred_l[ INTRA_L_LP_LEFT] = intra_pred_lp_left;
1506 h->intra_pred_l[ INTRA_L_LP_TOP] = intra_pred_lp_top;
1507 h->intra_pred_l[ INTRA_L_DC_128] = intra_pred_dc_128;
1508 h->intra_pred_c[ INTRA_C_LP] = intra_pred_lp;
1509 h->intra_pred_c[ INTRA_C_HORIZ] = intra_pred_horiz;
1510 h->intra_pred_c[ INTRA_C_VERT] = intra_pred_vert;
1511 h->intra_pred_c[ INTRA_C_PLANE] = intra_pred_plane;
1512 h->intra_pred_c[ INTRA_C_LP_LEFT] = intra_pred_lp_left;
1513 h->intra_pred_c[ INTRA_C_LP_TOP] = intra_pred_lp_top;
1514 h->intra_pred_c[ INTRA_C_DC_128] = intra_pred_dc_128;
1515 h->mv[ 7] = un_mv;
1516 h->mv[19] = un_mv;
1517 return 0;
1518 }
1519
1520 static int cavs_decode_end(AVCodecContext * avctx) {
1521 AVSContext *h = avctx->priv_data;
1522
1523 av_free(h->top_qp);
1524 av_free(h->top_mv[0]);
1525 av_free(h->top_mv[1]);
1526 av_free(h->top_pred_Y);
1527 av_free(h->top_border_y);
1528 av_free(h->top_border_u);
1529 av_free(h->top_border_v);
1530 av_free(h->col_mv);
1531 av_free(h->col_type_base);
1532 return 0;
1533 }
1534
1535 AVCodec cavs_decoder = {
1536 "cavs",
1537 CODEC_TYPE_VIDEO,
1538 CODEC_ID_CAVS,
1539 sizeof(AVSContext),
1540 cavs_decode_init,
1541 NULL,
1542 cavs_decode_end,
1543 cavs_decode_frame,
1544 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY, //FIXME is this correct ?
1545 .flush= ff_cavs_flush,
1546 };