2 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
3 * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
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.
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.
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
22 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
23 * @author Stefan Gehrer <stefan.gehrer@gmx.de>
27 #include "bitstream.h"
29 #include "mpegvideo.h"
34 Picture picture; ///< currently decoded frame
35 Picture DPB[2]; ///< reference frames
36 int dist[2]; ///< temporal distances from current frame to ref frames
39 int mb_width, mb_height;
43 int skip_mode_flag; ///< select between skip_count or one skip_flag per MB
44 int loop_filter_disable;
45 int alpha_offset, beta_offset;
47 int mbx, mby; ///< macroblock coordinates
48 int flags; ///< availability flags of neighbouring macroblocks
49 int stc; ///< last start code
50 uint8_t *cy, *cu, *cv; ///< current MB sample pointers
54 /** mv motion vector cache
59 X are the vectors in the current macroblock (5,6,9,10)
60 A is the macroblock to the left (4,8)
61 B is the macroblock to the top (1,2)
62 C is the macroblock to the top-right (3)
63 D is the macroblock to the top-left (0)
65 the same is repeated for backward motion vectors */
70 /** luma pred mode cache
76 int l_stride, c_stride;
83 /** intra prediction is done with un-deblocked samples
84 they are saved here before deblocking the MB */
85 uint8_t *top_border_y, *top_border_u, *top_border_v;
86 uint8_t left_border_y[26], left_border_u[10], left_border_v[10];
87 uint8_t intern_border_y[26];
88 uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
90 void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
91 void (*intra_pred_c[7])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
92 uint8_t *col_type_base;
95 /* scaling factors for MV prediction */
96 int sym_factor; ///< for scaling in symmetrical B block
97 int direct_den[2]; ///< for scaling in direct B block
98 int scale_den[2]; ///< for scaling neighbouring MVs
104 /*****************************************************************************
106 * in-loop deblocking filter
108 ****************************************************************************/
110 static inline int get_bs(vector_t *mvP, vector_t *mvQ, int b) {
111 if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA))
113 if( (abs(mvP->x - mvQ->x) >= 4) || (abs(mvP->y - mvQ->y) >= 4) )
118 if( (abs(mvP->x - mvQ->x) >= 4) || (abs(mvP->y - mvQ->y) >= 4) )
121 if(mvP->ref != mvQ->ref)
128 alpha = alpha_tab[clip(qp_avg + h->alpha_offset,0,63)]; \
129 beta = beta_tab[clip(qp_avg + h->beta_offset, 0,63)]; \
130 tc = tc_tab[clip(qp_avg + h->alpha_offset,0,63)];
133 * in-loop deblocking filter for a single macroblock
135 * boundary strength (bs) mapping:
144 static void filter_mb(AVSContext *h, enum mb_t mb_type) {
145 DECLARE_ALIGNED_8(uint8_t, bs[8]);
146 int qp_avg, alpha, beta, tc;
149 /* save un-deblocked lines */
150 h->topleft_border_y = h->top_border_y[h->mbx*16+15];
151 h->topleft_border_u = h->top_border_u[h->mbx*10+8];
152 h->topleft_border_v = h->top_border_v[h->mbx*10+8];
153 memcpy(&h->top_border_y[h->mbx*16], h->cy + 15* h->l_stride,16);
154 memcpy(&h->top_border_u[h->mbx*10+1], h->cu + 7* h->c_stride,8);
155 memcpy(&h->top_border_v[h->mbx*10+1], h->cv + 7* h->c_stride,8);
157 h->left_border_y[i*2+1] = *(h->cy + 15 + (i*2+0)*h->l_stride);
158 h->left_border_y[i*2+2] = *(h->cy + 15 + (i*2+1)*h->l_stride);
159 h->left_border_u[i+1] = *(h->cu + 7 + i*h->c_stride);
160 h->left_border_v[i+1] = *(h->cv + 7 + i*h->c_stride);
162 if(!h->loop_filter_disable) {
165 *((uint64_t *)bs) = 0x0202020202020202ULL;
167 *((uint64_t *)bs) = 0;
168 if(partition_flags[mb_type] & SPLITV){
169 bs[2] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1], mb_type > P_8X8);
170 bs[3] = get_bs(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3], mb_type > P_8X8);
172 if(partition_flags[mb_type] & SPLITH){
173 bs[6] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2], mb_type > P_8X8);
174 bs[7] = get_bs(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3], mb_type > P_8X8);
176 bs[0] = get_bs(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0], mb_type > P_8X8);
177 bs[1] = get_bs(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2], mb_type > P_8X8);
178 bs[4] = get_bs(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0], mb_type > P_8X8);
179 bs[5] = get_bs(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1], mb_type > P_8X8);
181 if( *((uint64_t *)bs) ) {
182 if(h->flags & A_AVAIL) {
183 qp_avg = (h->qp + h->left_qp + 1) >> 1;
185 h->s.dsp.cavs_filter_lv(h->cy,h->l_stride,alpha,beta,tc,bs[0],bs[1]);
186 h->s.dsp.cavs_filter_cv(h->cu,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
187 h->s.dsp.cavs_filter_cv(h->cv,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
191 h->s.dsp.cavs_filter_lv(h->cy + 8,h->l_stride,alpha,beta,tc,bs[2],bs[3]);
192 h->s.dsp.cavs_filter_lh(h->cy + 8*h->l_stride,h->l_stride,alpha,beta,tc,
195 if(h->flags & B_AVAIL) {
196 qp_avg = (h->qp + h->top_qp[h->mbx] + 1) >> 1;
198 h->s.dsp.cavs_filter_lh(h->cy,h->l_stride,alpha,beta,tc,bs[4],bs[5]);
199 h->s.dsp.cavs_filter_ch(h->cu,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
200 h->s.dsp.cavs_filter_ch(h->cv,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
205 h->top_qp[h->mbx] = h->qp;
210 /*****************************************************************************
212 * spatial intra prediction
214 ****************************************************************************/
216 static inline void load_intra_pred_luma(AVSContext *h, uint8_t *top,
217 uint8_t **left, int block) {
222 *left = h->left_border_y;
223 h->left_border_y[0] = h->left_border_y[1];
224 memset(&h->left_border_y[17],h->left_border_y[16],9);
225 memcpy(&top[1],&h->top_border_y[h->mbx*16],16);
228 if((h->flags & A_AVAIL) && (h->flags & B_AVAIL))
229 h->left_border_y[0] = top[0] = h->topleft_border_y;
232 *left = h->intern_border_y;
234 h->intern_border_y[i+1] = *(h->cy + 7 + i*h->l_stride);
235 memset(&h->intern_border_y[9],h->intern_border_y[8],9);
236 h->intern_border_y[0] = h->intern_border_y[1];
237 memcpy(&top[1],&h->top_border_y[h->mbx*16+8],8);
238 if(h->flags & C_AVAIL)
239 memcpy(&top[9],&h->top_border_y[(h->mbx + 1)*16],8);
241 memset(&top[9],top[8],9);
244 if(h->flags & B_AVAIL)
245 h->intern_border_y[0] = top[0] = h->top_border_y[h->mbx*16+7];
248 *left = &h->left_border_y[8];
249 memcpy(&top[1],h->cy + 7*h->l_stride,16);
252 if(h->flags & A_AVAIL)
253 top[0] = h->left_border_y[8];
256 *left = &h->intern_border_y[8];
258 h->intern_border_y[i+9] = *(h->cy + 7 + (i+8)*h->l_stride);
259 memset(&h->intern_border_y[17],h->intern_border_y[16],9);
260 memcpy(&top[0],h->cy + 7 + 7*h->l_stride,9);
261 memset(&top[9],top[8],9);
266 static void intra_pred_vert(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
268 uint64_t a = unaligned64(&top[1]);
270 *((uint64_t *)(d+y*stride)) = a;
274 static void intra_pred_horiz(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
278 a = left[y+1] * 0x0101010101010101ULL;
279 *((uint64_t *)(d+y*stride)) = a;
283 static void intra_pred_dc_128(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
285 uint64_t a = 0x8080808080808080ULL;
287 *((uint64_t *)(d+y*stride)) = a;
290 static void intra_pred_plane(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
294 uint8_t *cm = cropTbl + MAX_NEG_CROP;
297 ih += (x+1)*(top[5+x]-top[3-x]);
298 iv += (x+1)*(left[5+x]-left[3-x]);
300 ia = (top[8]+left[8])<<4;
305 d[y*stride+x] = cm[(ia+(x-3)*ih+(y-3)*iv+16)>>5];
308 #define LOWPASS(ARRAY,INDEX) \
309 (( ARRAY[(INDEX)-1] + 2*ARRAY[(INDEX)] + ARRAY[(INDEX)+1] + 2) >> 2)
311 static void intra_pred_lp(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
315 d[y*stride+x] = (LOWPASS(top,x+1) + LOWPASS(left,y+1)) >> 1;
318 static void intra_pred_down_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
322 d[y*stride+x] = (LOWPASS(top,x+y+2) + LOWPASS(left,x+y+2)) >> 1;
325 static void intra_pred_down_right(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
330 d[y*stride+x] = (left[1]+2*top[0]+top[1]+2)>>2;
332 d[y*stride+x] = LOWPASS(top,x-y);
334 d[y*stride+x] = LOWPASS(left,y-x);
337 static void intra_pred_lp_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
341 d[y*stride+x] = LOWPASS(left,y+1);
344 static void intra_pred_lp_top(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
348 d[y*stride+x] = LOWPASS(top,x+1);
353 static inline void modify_pred(const int_fast8_t *mod_table, int *mode) {
354 *mode = mod_table[*mode];
356 av_log(NULL, AV_LOG_ERROR, "Illegal intra prediction mode\n");
361 /*****************************************************************************
363 * motion compensation
365 ****************************************************************************/
367 static inline void mc_dir_part(AVSContext *h,Picture *pic,int square,
368 int chroma_height,int delta,int list,uint8_t *dest_y,
369 uint8_t *dest_cb,uint8_t *dest_cr,int src_x_offset,
370 int src_y_offset,qpel_mc_func *qpix_op,
371 h264_chroma_mc_func chroma_op,vector_t *mv){
372 MpegEncContext * const s = &h->s;
373 const int mx= mv->x + src_x_offset*8;
374 const int my= mv->y + src_y_offset*8;
375 const int luma_xy= (mx&3) + ((my&3)<<2);
376 uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->l_stride;
377 uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->c_stride;
378 uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->c_stride;
379 int extra_width= 0; //(s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
380 int extra_height= extra_width;
382 const int full_mx= mx>>2;
383 const int full_my= my>>2;
384 const int pic_width = 16*h->mb_width;
385 const int pic_height = 16*h->mb_height;
389 if(mx&7) extra_width -= 3;
390 if(my&7) extra_height -= 3;
392 if( full_mx < 0-extra_width
393 || full_my < 0-extra_height
394 || full_mx + 16/*FIXME*/ > pic_width + extra_width
395 || full_my + 16/*FIXME*/ > pic_height + extra_height){
396 ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->l_stride, h->l_stride,
397 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
398 src_y= s->edge_emu_buffer + 2 + 2*h->l_stride;
402 qpix_op[luma_xy](dest_y, src_y, h->l_stride); //FIXME try variable height perhaps?
404 qpix_op[luma_xy](dest_y + delta, src_y + delta, h->l_stride);
408 ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->c_stride,
409 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
410 src_cb= s->edge_emu_buffer;
412 chroma_op(dest_cb, src_cb, h->c_stride, chroma_height, mx&7, my&7);
415 ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->c_stride,
416 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
417 src_cr= s->edge_emu_buffer;
419 chroma_op(dest_cr, src_cr, h->c_stride, chroma_height, mx&7, my&7);
422 static inline void mc_part_std(AVSContext *h,int square,int chroma_height,int delta,
423 uint8_t *dest_y,uint8_t *dest_cb,uint8_t *dest_cr,
424 int x_offset, int y_offset,qpel_mc_func *qpix_put,
425 h264_chroma_mc_func chroma_put,qpel_mc_func *qpix_avg,
426 h264_chroma_mc_func chroma_avg, vector_t *mv){
427 qpel_mc_func *qpix_op= qpix_put;
428 h264_chroma_mc_func chroma_op= chroma_put;
430 dest_y += 2*x_offset + 2*y_offset*h->l_stride;
431 dest_cb += x_offset + y_offset*h->c_stride;
432 dest_cr += x_offset + y_offset*h->c_stride;
433 x_offset += 8*h->mbx;
434 y_offset += 8*h->mby;
437 Picture *ref= &h->DPB[mv->ref];
438 mc_dir_part(h, ref, square, chroma_height, delta, 0,
439 dest_y, dest_cb, dest_cr, x_offset, y_offset,
440 qpix_op, chroma_op, mv);
443 chroma_op= chroma_avg;
446 if((mv+MV_BWD_OFFS)->ref >= 0){
447 Picture *ref= &h->DPB[0];
448 mc_dir_part(h, ref, square, chroma_height, delta, 1,
449 dest_y, dest_cb, dest_cr, x_offset, y_offset,
450 qpix_op, chroma_op, mv+MV_BWD_OFFS);
454 static void inter_pred(AVSContext *h, enum mb_t mb_type) {
455 if(partition_flags[mb_type] == 0){ // 16x16
456 mc_part_std(h, 1, 8, 0, h->cy, h->cu, h->cv, 0, 0,
457 h->s.dsp.put_cavs_qpel_pixels_tab[0],
458 h->s.dsp.put_h264_chroma_pixels_tab[0],
459 h->s.dsp.avg_cavs_qpel_pixels_tab[0],
460 h->s.dsp.avg_h264_chroma_pixels_tab[0],&h->mv[MV_FWD_X0]);
462 mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 0,
463 h->s.dsp.put_cavs_qpel_pixels_tab[1],
464 h->s.dsp.put_h264_chroma_pixels_tab[1],
465 h->s.dsp.avg_cavs_qpel_pixels_tab[1],
466 h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X0]);
467 mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 0,
468 h->s.dsp.put_cavs_qpel_pixels_tab[1],
469 h->s.dsp.put_h264_chroma_pixels_tab[1],
470 h->s.dsp.avg_cavs_qpel_pixels_tab[1],
471 h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X1]);
472 mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 4,
473 h->s.dsp.put_cavs_qpel_pixels_tab[1],
474 h->s.dsp.put_h264_chroma_pixels_tab[1],
475 h->s.dsp.avg_cavs_qpel_pixels_tab[1],
476 h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X2]);
477 mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 4,
478 h->s.dsp.put_cavs_qpel_pixels_tab[1],
479 h->s.dsp.put_h264_chroma_pixels_tab[1],
480 h->s.dsp.avg_cavs_qpel_pixels_tab[1],
481 h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X3]);
483 /* set intra prediction modes to default values */
484 h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
485 h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
488 /*****************************************************************************
490 * motion vector prediction
492 ****************************************************************************/
494 static inline void set_mvs(vector_t *mv, enum block_t size) {
497 mv[MV_STRIDE ] = mv[0];
498 mv[MV_STRIDE+1] = mv[0];
503 mv[MV_STRIDE] = mv[0];
508 static inline void store_mvs(AVSContext *h) {
509 h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 0] = h->mv[MV_FWD_X0];
510 h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 1] = h->mv[MV_FWD_X1];
511 h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 2] = h->mv[MV_FWD_X2];
512 h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 3] = h->mv[MV_FWD_X3];
515 static inline void scale_mv(AVSContext *h, int *d_x, int *d_y, vector_t *src, int distp) {
516 int den = h->scale_den[src->ref];
518 *d_x = (src->x*distp*den + 256 + (src->x>>31)) >> 9;
519 *d_y = (src->y*distp*den + 256 + (src->y>>31)) >> 9;
522 static inline void mv_pred_median(AVSContext *h, vector_t *mvP, vector_t *mvA, vector_t *mvB, vector_t *mvC) {
523 int ax, ay, bx, by, cx, cy;
524 int len_ab, len_bc, len_ca, len_mid;
526 /* scale candidates according to their temporal span */
527 scale_mv(h, &ax, &ay, mvA, mvP->dist);
528 scale_mv(h, &bx, &by, mvB, mvP->dist);
529 scale_mv(h, &cx, &cy, mvC, mvP->dist);
530 /* find the geometrical median of the three candidates */
531 len_ab = abs(ax - bx) + abs(ay - by);
532 len_bc = abs(bx - cx) + abs(by - cy);
533 len_ca = abs(cx - ax) + abs(cy - ay);
534 len_mid = mid_pred(len_ab, len_bc, len_ca);
535 if(len_mid == len_ab) {
538 } else if(len_mid == len_bc) {
547 static inline void mv_pred_direct(AVSContext *h, vector_t *pmv_fw,
549 vector_t *pmv_bw = pmv_fw + MV_BWD_OFFS;
550 int den = h->direct_den[col_mv->ref];
551 int m = col_mv->x >> 31;
553 pmv_fw->dist = h->dist[1];
554 pmv_bw->dist = h->dist[0];
557 /* scale the co-located motion vector according to its temporal span */
558 pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
559 pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
561 pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
562 pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
565 static inline void mv_pred_sym(AVSContext *h, vector_t *src, enum block_t size) {
566 vector_t *dst = src + MV_BWD_OFFS;
568 /* backward mv is the scaled and negated forward mv */
569 dst->x = -((src->x * h->sym_factor + 256) >> 9);
570 dst->y = -((src->y * h->sym_factor + 256) >> 9);
572 dst->dist = h->dist[0];
576 static void mv_pred(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC,
577 enum mv_pred_t mode, enum block_t size, int ref) {
578 vector_t *mvP = &h->mv[nP];
579 vector_t *mvA = &h->mv[nP-1];
580 vector_t *mvB = &h->mv[nP-4];
581 vector_t *mvC = &h->mv[nC];
582 const vector_t *mvP2 = NULL;
585 mvP->dist = h->dist[mvP->ref];
586 if(mvC->ref == NOT_AVAIL)
587 mvC = &h->mv[nP-5]; // set to top-left (mvD)
588 if((mode == MV_PRED_PSKIP) &&
589 ((mvA->ref == NOT_AVAIL) || (mvB->ref == NOT_AVAIL) ||
590 ((mvA->x | mvA->y | mvA->ref) == 0) ||
591 ((mvB->x | mvB->y | mvB->ref) == 0) )) {
593 /* if there is only one suitable candidate, take it */
594 } else if((mvA->ref >= 0) && (mvB->ref < 0) && (mvC->ref < 0)) {
596 } else if((mvA->ref < 0) && (mvB->ref >= 0) && (mvC->ref < 0)) {
598 } else if((mvA->ref < 0) && (mvB->ref < 0) && (mvC->ref >= 0)) {
600 } else if(mode == MV_PRED_LEFT && mvA->ref == ref){
602 } else if(mode == MV_PRED_TOP && mvB->ref == ref){
604 } else if(mode == MV_PRED_TOPRIGHT && mvC->ref == ref){
611 mv_pred_median(h, mvP, mvA, mvB, mvC);
613 if(mode < MV_PRED_PSKIP) {
614 mvP->x += get_se_golomb(&h->s.gb);
615 mvP->y += get_se_golomb(&h->s.gb);
620 /*****************************************************************************
622 * residual data decoding
624 ****************************************************************************/
626 /** kth-order exponential golomb code */
627 static inline int get_ue_code(GetBitContext *gb, int order) {
629 int ret = get_ue_golomb(gb) << order;
630 return ret + get_bits(gb,order);
632 return get_ue_golomb(gb);
636 * decode coefficients from one 8x8 block, dequantize, inverse transform
637 * and add them to sample block
638 * @param r pointer to 2D VLC table
639 * @param esc_golomb_order escape codes are k-golomb with this order k
640 * @param qp quantizer
641 * @param dst location of sample block
642 * @param stride line stride in frame buffer
644 static int decode_residual_block(AVSContext *h, GetBitContext *gb,
645 const residual_vlc_t *r, int esc_golomb_order,
646 int qp, uint8_t *dst, int stride) {
648 int level_code, esc_code, level, run, mask;
651 int dqm = dequant_mul[qp];
652 int dqs = dequant_shift[qp];
653 int dqa = 1 << (dqs - 1);
654 const uint8_t *scantab = h->scantable.permutated;
655 DCTELEM *block = h->block;
658 level_code = get_ue_code(gb,r->golomb_order);
659 if(level_code >= ESCAPE_CODE) {
660 run = ((level_code - ESCAPE_CODE) >> 1) + 1;
661 esc_code = get_ue_code(gb,esc_golomb_order);
662 level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
663 while(level > r->inc_limit)
665 mask = -(level_code & 1);
666 level = (level^mask) - mask;
668 level = r->rltab[level_code][0];
669 if(!level) //end of block signal
671 run = r->rltab[level_code][1];
672 r += r->rltab[level_code][2];
674 level_buf[i] = level;
677 /* inverse scan and dequantization */
681 av_log(h->s.avctx, AV_LOG_ERROR,
682 "position out of block bounds at pic %d MB(%d,%d)\n",
683 h->picture.poc, h->mbx, h->mby);
686 block[scantab[pos]] = (level_buf[i]*dqm + dqa) >> dqs;
688 h->s.dsp.cavs_idct8_add(dst,block,stride);
693 static inline void decode_residual_chroma(AVSContext *h) {
695 decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
698 decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
702 static inline int decode_residual_inter(AVSContext *h) {
705 /* get coded block pattern */
706 int cbp= get_ue_golomb(&h->s.gb);
708 av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
711 h->cbp = cbp_tab[cbp][1];
714 if(h->cbp && !h->qp_fixed)
715 h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
716 for(block=0;block<4;block++)
717 if(h->cbp & (1<<block))
718 decode_residual_block(h,&h->s.gb,inter_2dvlc,0,h->qp,
719 h->cy + h->luma_scan[block], h->l_stride);
720 decode_residual_chroma(h);
725 /*****************************************************************************
729 ****************************************************************************/
732 * initialise predictors for motion vectors and intra prediction
734 static inline void init_mb(AVSContext *h) {
737 /* copy predictors from top line (MB B and C) into cache */
739 h->mv[MV_FWD_B2+i] = h->top_mv[0][h->mbx*2+i];
740 h->mv[MV_BWD_B2+i] = h->top_mv[1][h->mbx*2+i];
742 h->pred_mode_Y[1] = h->top_pred_Y[h->mbx*2+0];
743 h->pred_mode_Y[2] = h->top_pred_Y[h->mbx*2+1];
744 /* clear top predictors if MB B is not available */
745 if(!(h->flags & B_AVAIL)) {
746 h->mv[MV_FWD_B2] = un_mv;
747 h->mv[MV_FWD_B3] = un_mv;
748 h->mv[MV_BWD_B2] = un_mv;
749 h->mv[MV_BWD_B3] = un_mv;
750 h->pred_mode_Y[1] = h->pred_mode_Y[2] = NOT_AVAIL;
751 h->flags &= ~(C_AVAIL|D_AVAIL);
755 if(h->mbx == h->mb_width-1) //MB C not available
756 h->flags &= ~C_AVAIL;
757 /* clear top-right predictors if MB C is not available */
758 if(!(h->flags & C_AVAIL)) {
759 h->mv[MV_FWD_C2] = un_mv;
760 h->mv[MV_BWD_C2] = un_mv;
762 /* clear top-left predictors if MB D is not available */
763 if(!(h->flags & D_AVAIL)) {
764 h->mv[MV_FWD_D3] = un_mv;
765 h->mv[MV_BWD_D3] = un_mv;
767 /* set pointer for co-located macroblock type */
768 h->col_type = &h->col_type_base[h->mby*h->mb_width + h->mbx];
771 static inline void check_for_slice(AVSContext *h);
774 * save predictors for later macroblocks and increase
776 * @returns 0 if end of frame is reached, 1 otherwise
778 static inline int next_mb(AVSContext *h) {
785 /* copy mvs as predictors to the left */
787 h->mv[i] = h->mv[i+2];
788 /* copy bottom mvs from cache to top line */
789 h->top_mv[0][h->mbx*2+0] = h->mv[MV_FWD_X2];
790 h->top_mv[0][h->mbx*2+1] = h->mv[MV_FWD_X3];
791 h->top_mv[1][h->mbx*2+0] = h->mv[MV_BWD_X2];
792 h->top_mv[1][h->mbx*2+1] = h->mv[MV_BWD_X3];
793 /* next MB address */
795 if(h->mbx == h->mb_width) { //new mb line
796 h->flags = B_AVAIL|C_AVAIL;
797 /* clear left pred_modes */
798 h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
799 /* clear left mv predictors */
804 /* re-calculate sample pointers */
805 h->cy = h->picture.data[0] + h->mby*16*h->l_stride;
806 h->cu = h->picture.data[1] + h->mby*8*h->c_stride;
807 h->cv = h->picture.data[2] + h->mby*8*h->c_stride;
808 if(h->mby == h->mb_height) { //frame end
811 //check_for_slice(h);
817 static int decode_mb_i(AVSContext *h, int cbp_code) {
818 GetBitContext *gb = &h->s.gb;
819 int block, pred_mode_uv;
821 uint8_t *left = NULL;
826 /* get intra prediction modes from stream */
827 for(block=0;block<4;block++) {
829 int pos = scan3x3[block];
831 nA = h->pred_mode_Y[pos-1];
832 nB = h->pred_mode_Y[pos-3];
833 predpred = FFMIN(nA,nB);
834 if(predpred == NOT_AVAIL) // if either is not available
835 predpred = INTRA_L_LP;
837 int rem_mode= get_bits(gb, 2);
838 predpred = rem_mode + (rem_mode >= predpred);
840 h->pred_mode_Y[pos] = predpred;
842 pred_mode_uv = get_ue_golomb(gb);
843 if(pred_mode_uv > 6) {
844 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
848 /* save pred modes before they get modified */
849 h->pred_mode_Y[3] = h->pred_mode_Y[5];
850 h->pred_mode_Y[6] = h->pred_mode_Y[8];
851 h->top_pred_Y[h->mbx*2+0] = h->pred_mode_Y[7];
852 h->top_pred_Y[h->mbx*2+1] = h->pred_mode_Y[8];
854 /* modify pred modes according to availability of neighbour samples */
855 if(!(h->flags & A_AVAIL)) {
856 modify_pred(left_modifier_l, &h->pred_mode_Y[4] );
857 modify_pred(left_modifier_l, &h->pred_mode_Y[7] );
858 modify_pred(left_modifier_c, &pred_mode_uv );
860 if(!(h->flags & B_AVAIL)) {
861 modify_pred(top_modifier_l, &h->pred_mode_Y[4] );
862 modify_pred(top_modifier_l, &h->pred_mode_Y[5] );
863 modify_pred(top_modifier_c, &pred_mode_uv );
866 /* get coded block pattern */
867 if(h->pic_type == FF_I_TYPE)
868 cbp_code = get_ue_golomb(gb);
870 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
873 h->cbp = cbp_tab[cbp_code][0];
874 if(h->cbp && !h->qp_fixed)
875 h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
877 /* luma intra prediction interleaved with residual decode/transform/add */
878 for(block=0;block<4;block++) {
879 d = h->cy + h->luma_scan[block];
880 load_intra_pred_luma(h, top, &left, block);
881 h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
882 (d, top, left, h->l_stride);
883 if(h->cbp & (1<<block))
884 decode_residual_block(h,gb,intra_2dvlc,1,h->qp,d,h->l_stride);
887 /* chroma intra prediction */
888 /* extend borders by one pixel */
889 h->left_border_u[9] = h->left_border_u[8];
890 h->left_border_v[9] = h->left_border_v[8];
891 h->top_border_u[h->mbx*10+9] = h->top_border_u[h->mbx*10+8];
892 h->top_border_v[h->mbx*10+9] = h->top_border_v[h->mbx*10+8];
893 if(h->mbx && h->mby) {
894 h->top_border_u[h->mbx*10] = h->left_border_u[0] = h->topleft_border_u;
895 h->top_border_v[h->mbx*10] = h->left_border_v[0] = h->topleft_border_v;
897 h->left_border_u[0] = h->left_border_u[1];
898 h->left_border_v[0] = h->left_border_v[1];
899 h->top_border_u[h->mbx*10] = h->top_border_u[h->mbx*10+1];
900 h->top_border_v[h->mbx*10] = h->top_border_v[h->mbx*10+1];
902 h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
903 h->left_border_u, h->c_stride);
904 h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
905 h->left_border_v, h->c_stride);
907 decode_residual_chroma(h);
910 /* mark motion vectors as intra */
911 h->mv[MV_FWD_X0] = intra_mv;
912 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
913 h->mv[MV_BWD_X0] = intra_mv;
914 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
915 if(h->pic_type != FF_B_TYPE)
916 *h->col_type = I_8X8;
921 static void decode_mb_p(AVSContext *h, enum mb_t mb_type) {
922 GetBitContext *gb = &h->s.gb;
928 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
931 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
932 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
935 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
936 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
937 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
938 mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
941 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
942 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
943 mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
944 mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
947 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
948 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
949 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
950 ref[3] = h->ref_flag ? 0 : get_bits1(gb);
951 mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
952 mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
953 mv_pred(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
954 mv_pred(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
956 inter_pred(h, mb_type);
958 if(mb_type != P_SKIP)
959 decode_residual_inter(h);
960 filter_mb(h,mb_type);
961 *h->col_type = mb_type;
964 static void decode_mb_b(AVSContext *h, enum mb_t mb_type) {
966 enum sub_mb_t sub_type[4];
972 h->mv[MV_FWD_X0] = dir_mv;
973 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
974 h->mv[MV_BWD_X0] = dir_mv;
975 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
979 if(!(*h->col_type)) {
980 /* intra MB at co-location, do in-plane prediction */
981 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
982 mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
984 /* direct prediction from co-located P MB, block-wise */
985 for(block=0;block<4;block++)
986 mv_pred_direct(h,&h->mv[mv_scan[block]],
987 &h->col_mv[(h->mby*h->mb_width+h->mbx)*4 + block]);
990 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
993 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
994 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
997 mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
1000 for(block=0;block<4;block++)
1001 sub_type[block] = get_bits(&h->s.gb,2);
1002 for(block=0;block<4;block++) {
1003 switch(sub_type[block]) {
1005 if(!(*h->col_type)) {
1006 /* intra MB at co-location, do in-plane prediction */
1007 mv_pred(h, mv_scan[block], mv_scan[block]-3,
1008 MV_PRED_BSKIP, BLK_8X8, 1);
1009 mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
1010 mv_scan[block]-3+MV_BWD_OFFS,
1011 MV_PRED_BSKIP, BLK_8X8, 0);
1013 mv_pred_direct(h,&h->mv[mv_scan[block]],
1014 &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + block]);
1017 mv_pred(h, mv_scan[block], mv_scan[block]-3,
1018 MV_PRED_MEDIAN, BLK_8X8, 1);
1021 mv_pred(h, mv_scan[block], mv_scan[block]-3,
1022 MV_PRED_MEDIAN, BLK_8X8, 1);
1023 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
1027 for(block=0;block<4;block++) {
1028 if(sub_type[block] == B_SUB_BWD)
1029 mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
1030 mv_scan[block]+MV_BWD_OFFS-3,
1031 MV_PRED_MEDIAN, BLK_8X8, 0);
1035 assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
1036 flags = partition_flags[mb_type];
1037 if(mb_type & 1) { /* 16x8 macroblock types */
1039 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
1041 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
1043 mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
1045 mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
1047 mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
1049 mv_pred(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
1050 } else { /* 8x16 macroblock types */
1052 mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
1054 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
1056 mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 1);
1058 mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
1060 mv_pred(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
1062 mv_pred(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 0);
1065 inter_pred(h, mb_type);
1066 if(mb_type != B_SKIP)
1067 decode_residual_inter(h);
1068 filter_mb(h,mb_type);
1071 /*****************************************************************************
1075 ****************************************************************************/
1077 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
1079 av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
1081 if((h->mby == 0) && (!h->qp_fixed)){
1082 h->qp_fixed = get_bits1(gb);
1083 h->qp = get_bits(gb,6);
1085 /* inter frame or second slice can have weighting params */
1086 if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
1087 if(get_bits1(gb)) { //slice_weighting_flag
1088 av_log(h->s.avctx, AV_LOG_ERROR,
1089 "weighted prediction not yet supported\n");
1094 static inline void check_for_slice(AVSContext *h) {
1095 GetBitContext *gb = &h->s.gb;
1097 align = (-get_bits_count(gb)) & 7;
1098 if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
1099 get_bits_long(gb,24+align);
1100 h->stc = get_bits(gb,8);
1101 decode_slice_header(h,gb);
1105 /*****************************************************************************
1109 ****************************************************************************/
1111 static void init_pic(AVSContext *h) {
1114 /* clear some predictors */
1117 h->mv[MV_BWD_X0] = dir_mv;
1118 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
1119 h->mv[MV_FWD_X0] = dir_mv;
1120 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
1121 h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
1122 h->cy = h->picture.data[0];
1123 h->cu = h->picture.data[1];
1124 h->cv = h->picture.data[2];
1125 h->l_stride = h->picture.linesize[0];
1126 h->c_stride = h->picture.linesize[1];
1127 h->luma_scan[2] = 8*h->l_stride;
1128 h->luma_scan[3] = 8*h->l_stride+8;
1129 h->mbx = h->mby = 0;
1133 static int decode_pic(AVSContext *h) {
1134 MpegEncContext *s = &h->s;
1138 if (!s->context_initialized) {
1139 s->avctx->idct_algo = FF_IDCT_CAVS;
1140 if (MPV_common_init(s) < 0)
1142 ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
1144 get_bits(&s->gb,16);//bbv_dwlay
1145 if(h->stc == PIC_PB_START_CODE) {
1146 h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
1147 if(h->pic_type > FF_B_TYPE) {
1148 av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
1151 /* make sure we have the reference frames we need */
1152 if(!h->DPB[0].data[0] ||
1153 (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
1156 h->pic_type = FF_I_TYPE;
1157 if(get_bits1(&s->gb))
1158 get_bits(&s->gb,16);//time_code
1160 /* release last B frame */
1161 if(h->picture.data[0])
1162 s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
1164 s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
1166 h->picture.poc = get_bits(&s->gb,8)*2;
1168 /* get temporal distances and MV scaling factors */
1169 if(h->pic_type != FF_B_TYPE) {
1170 h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
1172 h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
1174 h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
1175 h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
1176 h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
1177 if(h->pic_type == FF_B_TYPE) {
1178 h->sym_factor = h->dist[0]*h->scale_den[1];
1180 h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
1181 h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
1185 get_ue_golomb(&s->gb); //bbv_check_times
1186 h->progressive = get_bits1(&s->gb);
1188 h->pic_structure = 1;
1189 else if(!(h->pic_structure = get_bits1(&s->gb) && (h->stc == PIC_PB_START_CODE)) )
1190 get_bits1(&s->gb); //advanced_pred_mode_disable
1191 skip_bits1(&s->gb); //top_field_first
1192 skip_bits1(&s->gb); //repeat_first_field
1193 h->qp_fixed = get_bits1(&s->gb);
1194 h->qp = get_bits(&s->gb,6);
1195 if(h->pic_type == FF_I_TYPE) {
1196 if(!h->progressive && !h->pic_structure)
1197 skip_bits1(&s->gb);//what is this?
1198 skip_bits(&s->gb,4); //reserved bits
1200 if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
1201 h->ref_flag = get_bits1(&s->gb);
1202 skip_bits(&s->gb,4); //reserved bits
1203 h->skip_mode_flag = get_bits1(&s->gb);
1205 h->loop_filter_disable = get_bits1(&s->gb);
1206 if(!h->loop_filter_disable && get_bits1(&s->gb)) {
1207 h->alpha_offset = get_se_golomb(&s->gb);
1208 h->beta_offset = get_se_golomb(&s->gb);
1210 h->alpha_offset = h->beta_offset = 0;
1213 if(h->pic_type == FF_I_TYPE) {
1216 } while(next_mb(h));
1217 } else if(h->pic_type == FF_P_TYPE) {
1219 if(h->skip_mode_flag) {
1220 skip_count = get_ue_golomb(&s->gb);
1221 while(skip_count--) {
1222 decode_mb_p(h,P_SKIP);
1226 mb_type = get_ue_golomb(&s->gb) + P_16X16;
1228 mb_type = get_ue_golomb(&s->gb) + P_SKIP;
1229 if(mb_type > P_8X8) {
1230 decode_mb_i(h, mb_type - P_8X8 - 1);
1232 decode_mb_p(h,mb_type);
1233 } while(next_mb(h));
1234 } else { /* FF_B_TYPE */
1236 if(h->skip_mode_flag) {
1237 skip_count = get_ue_golomb(&s->gb);
1238 while(skip_count--) {
1239 decode_mb_b(h,B_SKIP);
1243 mb_type = get_ue_golomb(&s->gb) + B_DIRECT;
1245 mb_type = get_ue_golomb(&s->gb) + B_SKIP;
1246 if(mb_type > B_8X8) {
1247 decode_mb_i(h, mb_type - B_8X8 - 1);
1249 decode_mb_b(h,mb_type);
1250 } while(next_mb(h));
1253 if(h->pic_type != FF_B_TYPE) {
1254 if(h->DPB[1].data[0])
1255 s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
1256 memcpy(&h->DPB[1], &h->DPB[0], sizeof(Picture));
1257 memcpy(&h->DPB[0], &h->picture, sizeof(Picture));
1258 memset(&h->picture,0,sizeof(Picture));
1263 /*****************************************************************************
1265 * headers and interface
1267 ****************************************************************************/
1270 * some predictions require data from the top-neighbouring macroblock.
1271 * this data has to be stored for one complete row of macroblocks
1272 * and this storage space is allocated here
1274 static void init_top_lines(AVSContext *h) {
1275 /* alloc top line of predictors */
1276 h->top_qp = av_malloc( h->mb_width);
1277 h->top_mv[0] = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
1278 h->top_mv[1] = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
1279 h->top_pred_Y = av_malloc( h->mb_width*2*sizeof(*h->top_pred_Y));
1280 h->top_border_y = av_malloc((h->mb_width+1)*16);
1281 h->top_border_u = av_malloc((h->mb_width)*10);
1282 h->top_border_v = av_malloc((h->mb_width)*10);
1284 /* alloc space for co-located MVs and types */
1285 h->col_mv = av_malloc( h->mb_width*h->mb_height*4*sizeof(vector_t));
1286 h->col_type_base = av_malloc(h->mb_width*h->mb_height);
1287 h->block = av_mallocz(64*sizeof(DCTELEM));
1290 static int decode_seq_header(AVSContext *h) {
1291 MpegEncContext *s = &h->s;
1292 extern const AVRational ff_frame_rate_tab[];
1293 int frame_rate_code;
1295 h->profile = get_bits(&s->gb,8);
1296 h->level = get_bits(&s->gb,8);
1297 skip_bits1(&s->gb); //progressive sequence
1298 s->width = get_bits(&s->gb,14);
1299 s->height = get_bits(&s->gb,14);
1300 skip_bits(&s->gb,2); //chroma format
1301 skip_bits(&s->gb,3); //sample_precision
1302 h->aspect_ratio = get_bits(&s->gb,4);
1303 frame_rate_code = get_bits(&s->gb,4);
1304 skip_bits(&s->gb,18);//bit_rate_lower
1305 skip_bits1(&s->gb); //marker_bit
1306 skip_bits(&s->gb,12);//bit_rate_upper
1307 s->low_delay = get_bits1(&s->gb);
1308 h->mb_width = (s->width + 15) >> 4;
1309 h->mb_height = (s->height + 15) >> 4;
1310 h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num;
1311 h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den;
1312 h->s.avctx->width = s->width;
1313 h->s.avctx->height = s->height;
1320 * finds the end of the current frame in the bitstream.
1321 * @return the position of the first byte of the next frame, or -1
1323 int ff_cavs_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size) {
1327 pic_found= pc->frame_start_found;
1332 for(i=0; i<buf_size; i++){
1333 state= (state<<8) | buf[i];
1334 if(state == PIC_I_START_CODE || state == PIC_PB_START_CODE){
1343 /* EOF considered as end of frame */
1346 for(; i<buf_size; i++){
1347 state= (state<<8) | buf[i];
1348 if((state&0xFFFFFF00) == 0x100){
1349 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
1350 pc->frame_start_found=0;
1357 pc->frame_start_found= pic_found;
1359 return END_NOT_FOUND;
1362 void ff_cavs_flush(AVCodecContext * avctx) {
1363 AVSContext *h = avctx->priv_data;
1364 h->got_keyframe = 0;
1367 static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
1368 uint8_t * buf, int buf_size) {
1369 AVSContext *h = avctx->priv_data;
1370 MpegEncContext *s = &h->s;
1372 const uint8_t *buf_end;
1373 const uint8_t *buf_ptr;
1374 AVFrame *picture = data;
1379 if (buf_size == 0) {
1380 if(!s->low_delay && h->DPB[0].data[0]) {
1381 *data_size = sizeof(AVPicture);
1382 *picture = *(AVFrame *) &h->DPB[0];
1388 buf_end = buf + buf_size;
1390 buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
1391 if(stc & 0xFFFFFE00)
1392 return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
1393 input_size = (buf_end - buf_ptr)*8;
1395 case SEQ_START_CODE:
1396 init_get_bits(&s->gb, buf_ptr, input_size);
1397 decode_seq_header(h);
1399 case PIC_I_START_CODE:
1400 if(!h->got_keyframe) {
1401 if(h->DPB[0].data[0])
1402 avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
1403 if(h->DPB[1].data[0])
1404 avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
1405 h->got_keyframe = 1;
1407 case PIC_PB_START_CODE:
1409 if(!h->got_keyframe)
1411 init_get_bits(&s->gb, buf_ptr, input_size);
1415 *data_size = sizeof(AVPicture);
1416 if(h->pic_type != FF_B_TYPE) {
1417 if(h->DPB[1].data[0]) {
1418 *picture = *(AVFrame *) &h->DPB[1];
1423 *picture = *(AVFrame *) &h->picture;
1425 case EXT_START_CODE:
1426 //mpeg_decode_extension(avctx,buf_ptr, input_size);
1428 case USER_START_CODE:
1429 //mpeg_decode_user_data(avctx,buf_ptr, input_size);
1432 if (stc >= SLICE_MIN_START_CODE &&
1433 stc <= SLICE_MAX_START_CODE) {
1434 init_get_bits(&s->gb, buf_ptr, input_size);
1435 decode_slice_header(h, &s->gb);
1442 static int cavs_decode_init(AVCodecContext * avctx) {
1443 AVSContext *h = avctx->priv_data;
1444 MpegEncContext * const s = &h->s;
1446 MPV_decode_defaults(s);
1449 avctx->pix_fmt= PIX_FMT_YUV420P;
1451 h->luma_scan[0] = 0;
1452 h->luma_scan[1] = 8;
1453 h->intra_pred_l[ INTRA_L_VERT] = intra_pred_vert;
1454 h->intra_pred_l[ INTRA_L_HORIZ] = intra_pred_horiz;
1455 h->intra_pred_l[ INTRA_L_LP] = intra_pred_lp;
1456 h->intra_pred_l[ INTRA_L_DOWN_LEFT] = intra_pred_down_left;
1457 h->intra_pred_l[INTRA_L_DOWN_RIGHT] = intra_pred_down_right;
1458 h->intra_pred_l[ INTRA_L_LP_LEFT] = intra_pred_lp_left;
1459 h->intra_pred_l[ INTRA_L_LP_TOP] = intra_pred_lp_top;
1460 h->intra_pred_l[ INTRA_L_DC_128] = intra_pred_dc_128;
1461 h->intra_pred_c[ INTRA_C_LP] = intra_pred_lp;
1462 h->intra_pred_c[ INTRA_C_HORIZ] = intra_pred_horiz;
1463 h->intra_pred_c[ INTRA_C_VERT] = intra_pred_vert;
1464 h->intra_pred_c[ INTRA_C_PLANE] = intra_pred_plane;
1465 h->intra_pred_c[ INTRA_C_LP_LEFT] = intra_pred_lp_left;
1466 h->intra_pred_c[ INTRA_C_LP_TOP] = intra_pred_lp_top;
1467 h->intra_pred_c[ INTRA_C_DC_128] = intra_pred_dc_128;
1473 static int cavs_decode_end(AVCodecContext * avctx) {
1474 AVSContext *h = avctx->priv_data;
1477 av_free(h->top_mv[0]);
1478 av_free(h->top_mv[1]);
1479 av_free(h->top_pred_Y);
1480 av_free(h->top_border_y);
1481 av_free(h->top_border_u);
1482 av_free(h->top_border_v);
1484 av_free(h->col_type_base);
1489 AVCodec cavs_decoder = {
1498 CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1499 .flush= ff_cavs_flush,