2 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
3 * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
25 * @author Stefan Gehrer <stefan.gehrer@gmx.de>
28 #include "libavutil/avassert.h"
34 static const uint8_t mv_scan[4] = {
39 static const uint8_t cbp_tab[64][2] = {
40 {63, 0},{15,15},{31,63},{47,31},{ 0,16},{14,32},{13,47},{11,13},
41 { 7,14},{ 5,11},{10,12},{ 8, 5},{12,10},{61, 7},{ 4,48},{55, 3},
42 { 1, 2},{ 2, 8},{59, 4},{ 3, 1},{62,61},{ 9,55},{ 6,59},{29,62},
43 {45,29},{51,27},{23,23},{39,19},{27,30},{46,28},{53, 9},{30, 6},
44 {43,60},{37,21},{60,44},{16,26},{21,51},{28,35},{19,18},{35,20},
45 {42,24},{26,53},{44,17},{32,37},{58,39},{24,45},{20,58},{17,43},
46 {18,42},{48,46},{22,36},{33,33},{25,34},{49,40},{40,52},{36,49},
47 {34,50},{50,56},{52,25},{54,22},{41,54},{56,57},{38,41},{57,38}
50 /*****************************************************************************
52 * motion vector prediction
54 ****************************************************************************/
56 static inline void store_mvs(AVSContext *h) {
57 h->col_mv[h->mbidx*4 + 0] = h->mv[MV_FWD_X0];
58 h->col_mv[h->mbidx*4 + 1] = h->mv[MV_FWD_X1];
59 h->col_mv[h->mbidx*4 + 2] = h->mv[MV_FWD_X2];
60 h->col_mv[h->mbidx*4 + 3] = h->mv[MV_FWD_X3];
63 static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
64 cavs_vector *col_mv) {
65 cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
66 int den = h->direct_den[col_mv->ref];
67 int m = col_mv->x >> 31;
69 pmv_fw->dist = h->dist[1];
70 pmv_bw->dist = h->dist[0];
73 /* scale the co-located motion vector according to its temporal span */
74 pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
75 pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
77 pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
78 pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
81 static inline void mv_pred_sym(AVSContext *h, cavs_vector *src, enum cavs_block size) {
82 cavs_vector *dst = src + MV_BWD_OFFS;
84 /* backward mv is the scaled and negated forward mv */
85 dst->x = -((src->x * h->sym_factor + 256) >> 9);
86 dst->y = -((src->y * h->sym_factor + 256) >> 9);
88 dst->dist = h->dist[0];
92 /*****************************************************************************
94 * residual data decoding
96 ****************************************************************************/
98 /** kth-order exponential golomb code */
99 static inline int get_ue_code(GetBitContext *gb, int order) {
101 int ret = get_ue_golomb(gb) << order;
102 return ret + get_bits(gb,order);
104 return get_ue_golomb(gb);
108 * decode coefficients from one 8x8 block, dequantize, inverse transform
109 * and add them to sample block
110 * @param r pointer to 2D VLC table
111 * @param esc_golomb_order escape codes are k-golomb with this order k
112 * @param qp quantizer
113 * @param dst location of sample block
114 * @param stride line stride in frame buffer
116 static int decode_residual_block(AVSContext *h, GetBitContext *gb,
117 const struct dec_2dvlc *r, int esc_golomb_order,
118 int qp, uint8_t *dst, int stride) {
119 int i, esc_code, level, mask;
120 unsigned int level_code, run;
121 DCTELEM level_buf[65];
123 DCTELEM *block = h->block;
126 level_code = get_ue_code(gb,r->golomb_order);
127 if(level_code >= ESCAPE_CODE) {
128 run = ((level_code - ESCAPE_CODE) >> 1) + 1;
131 esc_code = get_ue_code(gb,esc_golomb_order);
132 level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
133 while(level > r->inc_limit)
135 mask = -(level_code & 1);
136 level = (level^mask) - mask;
138 level = r->rltab[level_code][0];
139 if(!level) //end of block signal
141 run = r->rltab[level_code][1];
142 r += r->rltab[level_code][2];
144 level_buf[i] = level;
147 if(dequant(h,level_buf, run_buf, block, ff_cavs_dequant_mul[qp],
148 ff_cavs_dequant_shift[qp], i))
150 h->cdsp.cavs_idct8_add(dst,block,stride);
151 h->s.dsp.clear_block(block);
156 static inline void decode_residual_chroma(AVSContext *h) {
158 decode_residual_block(h,&h->s.gb,ff_cavs_chroma_dec,0,
159 ff_cavs_chroma_qp[h->qp],h->cu,h->c_stride);
161 decode_residual_block(h,&h->s.gb,ff_cavs_chroma_dec,0,
162 ff_cavs_chroma_qp[h->qp],h->cv,h->c_stride);
165 static inline int decode_residual_inter(AVSContext *h) {
168 /* get coded block pattern */
169 int cbp= get_ue_golomb(&h->s.gb);
171 av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
174 h->cbp = cbp_tab[cbp][1];
177 if(h->cbp && !h->qp_fixed)
178 h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
179 for(block=0;block<4;block++)
180 if(h->cbp & (1<<block))
181 decode_residual_block(h,&h->s.gb,ff_cavs_inter_dec,0,h->qp,
182 h->cy + h->luma_scan[block], h->l_stride);
183 decode_residual_chroma(h);
188 /*****************************************************************************
192 ****************************************************************************/
194 static int decode_mb_i(AVSContext *h, int cbp_code) {
195 GetBitContext *gb = &h->s.gb;
196 unsigned pred_mode_uv;
199 uint8_t *left = NULL;
204 /* get intra prediction modes from stream */
205 for(block=0;block<4;block++) {
207 int pos = ff_cavs_scan3x3[block];
209 nA = h->pred_mode_Y[pos-1];
210 nB = h->pred_mode_Y[pos-3];
211 predpred = FFMIN(nA,nB);
212 if(predpred == NOT_AVAIL) // if either is not available
213 predpred = INTRA_L_LP;
215 int rem_mode= get_bits(gb, 2);
216 predpred = rem_mode + (rem_mode >= predpred);
218 h->pred_mode_Y[pos] = predpred;
220 pred_mode_uv = get_ue_golomb(gb);
221 if(pred_mode_uv > 6) {
222 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
225 ff_cavs_modify_mb_i(h, &pred_mode_uv);
227 /* get coded block pattern */
228 if(h->pic_type == AV_PICTURE_TYPE_I)
229 cbp_code = get_ue_golomb(gb);
231 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
234 h->cbp = cbp_tab[cbp_code][0];
235 if(h->cbp && !h->qp_fixed)
236 h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
238 /* luma intra prediction interleaved with residual decode/transform/add */
239 for(block=0;block<4;block++) {
240 d = h->cy + h->luma_scan[block];
241 ff_cavs_load_intra_pred_luma(h, top, &left, block);
242 h->intra_pred_l[h->pred_mode_Y[ff_cavs_scan3x3[block]]]
243 (d, top, left, h->l_stride);
244 if(h->cbp & (1<<block))
245 decode_residual_block(h,gb,ff_cavs_intra_dec,1,h->qp,d,h->l_stride);
248 /* chroma intra prediction */
249 ff_cavs_load_intra_pred_chroma(h);
250 h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
251 h->left_border_u, h->c_stride);
252 h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
253 h->left_border_v, h->c_stride);
255 decode_residual_chroma(h);
256 ff_cavs_filter(h,I_8X8);
261 static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type) {
262 GetBitContext *gb = &h->s.gb;
268 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
271 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
272 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
275 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
276 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
277 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
278 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
281 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
282 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
283 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
284 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, ref[1]);
287 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
288 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
289 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
290 ref[3] = h->ref_flag ? 0 : get_bits1(gb);
291 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
292 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
293 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
294 ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
296 ff_cavs_inter(h, mb_type);
297 set_intra_mode_default(h);
299 if(mb_type != P_SKIP)
300 decode_residual_inter(h);
301 ff_cavs_filter(h,mb_type);
302 h->col_type_base[h->mbidx] = mb_type;
305 static void decode_mb_b(AVSContext *h, enum cavs_mb mb_type) {
307 enum cavs_sub_mb sub_type[4];
313 h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
314 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
315 h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
316 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
320 if(!h->col_type_base[h->mbidx]) {
321 /* intra MB at co-location, do in-plane prediction */
322 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
323 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
325 /* direct prediction from co-located P MB, block-wise */
326 for(block=0;block<4;block++)
327 mv_pred_direct(h,&h->mv[mv_scan[block]],
328 &h->col_mv[h->mbidx*4 + block]);
331 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
334 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
335 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
338 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
341 for(block=0;block<4;block++)
342 sub_type[block] = get_bits(&h->s.gb,2);
343 for(block=0;block<4;block++) {
344 switch(sub_type[block]) {
346 if(!h->col_type_base[h->mbidx]) {
347 /* intra MB at co-location, do in-plane prediction */
348 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
349 MV_PRED_BSKIP, BLK_8X8, 1);
350 ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
351 mv_scan[block]-3+MV_BWD_OFFS,
352 MV_PRED_BSKIP, BLK_8X8, 0);
354 mv_pred_direct(h,&h->mv[mv_scan[block]],
355 &h->col_mv[h->mbidx*4 + block]);
358 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
359 MV_PRED_MEDIAN, BLK_8X8, 1);
362 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
363 MV_PRED_MEDIAN, BLK_8X8, 1);
364 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
368 for(block=0;block<4;block++) {
369 if(sub_type[block] == B_SUB_BWD)
370 ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
371 mv_scan[block]+MV_BWD_OFFS-3,
372 MV_PRED_MEDIAN, BLK_8X8, 0);
376 av_assert2((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
377 flags = ff_cavs_partition_flags[mb_type];
378 if(mb_type & 1) { /* 16x8 macroblock types */
380 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
382 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
384 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
386 mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
388 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
390 ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
391 } else { /* 8x16 macroblock types */
393 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
395 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
397 ff_cavs_mv(h,MV_FWD_X1,MV_FWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,1);
399 mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
401 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
403 ff_cavs_mv(h,MV_BWD_X1,MV_BWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,0);
406 ff_cavs_inter(h, mb_type);
407 set_intra_mode_default(h);
408 if(mb_type != B_SKIP)
409 decode_residual_inter(h);
410 ff_cavs_filter(h,mb_type);
413 /*****************************************************************************
417 ****************************************************************************/
419 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
421 av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
423 if (h->stc >= h->mb_height)
427 h->mbidx = h->mby*h->mb_width;
429 /* mark top macroblocks as unavailable */
430 h->flags &= ~(B_AVAIL|C_AVAIL);
431 if((h->mby == 0) && (!h->qp_fixed)){
432 h->qp_fixed = get_bits1(gb);
433 h->qp = get_bits(gb,6);
435 /* inter frame or second slice can have weighting params */
436 if((h->pic_type != AV_PICTURE_TYPE_I) || (!h->pic_structure && h->mby >= h->mb_width/2))
437 if(get_bits1(gb)) { //slice_weighting_flag
438 av_log(h->s.avctx, AV_LOG_ERROR,
439 "weighted prediction not yet supported\n");
444 static inline int check_for_slice(AVSContext *h) {
445 GetBitContext *gb = &h->s.gb;
450 align = (-get_bits_count(gb)) & 7;
451 /* check for stuffing byte */
452 if(!align && (show_bits(gb,8) == 0x80))
454 if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
455 skip_bits_long(gb,24+align);
456 h->stc = get_bits(gb,8);
457 if (h->stc >= h->mb_height)
459 decode_slice_header(h,gb);
465 /*****************************************************************************
469 ****************************************************************************/
471 static int decode_pic(AVSContext *h) {
472 MpegEncContext *s = &h->s;
474 enum cavs_mb mb_type;
476 if (!s->context_initialized) {
477 s->avctx->idct_algo = FF_IDCT_CAVS;
478 if (ff_MPV_common_init(s) < 0)
480 ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
482 skip_bits(&s->gb,16);//bbv_dwlay
483 if(h->stc == PIC_PB_START_CODE) {
484 h->pic_type = get_bits(&s->gb,2) + AV_PICTURE_TYPE_I;
485 if(h->pic_type > AV_PICTURE_TYPE_B) {
486 av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
489 /* make sure we have the reference frames we need */
490 if(!h->DPB[0].f.data[0] ||
491 (!h->DPB[1].f.data[0] && h->pic_type == AV_PICTURE_TYPE_B))
494 h->pic_type = AV_PICTURE_TYPE_I;
495 if(get_bits1(&s->gb))
496 skip_bits(&s->gb,24);//time_code
497 /* old sample clips were all progressive and no low_delay,
498 bump stream revision if detected otherwise */
499 if (s->low_delay || !(show_bits(&s->gb,9) & 1))
500 h->stream_revision = 1;
501 /* similarly test top_field_first and repeat_first_field */
502 else if(show_bits(&s->gb,11) & 3)
503 h->stream_revision = 1;
504 if(h->stream_revision > 0)
505 skip_bits(&s->gb,1); //marker_bit
507 /* release last B frame */
508 if(h->picture.f.data[0])
509 s->avctx->release_buffer(s->avctx, &h->picture.f);
511 s->avctx->get_buffer(s->avctx, &h->picture.f);
513 h->picture.poc = get_bits(&s->gb,8)*2;
515 /* get temporal distances and MV scaling factors */
516 if(h->pic_type != AV_PICTURE_TYPE_B) {
517 h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
519 h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
521 h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
522 h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
523 h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
524 if(h->pic_type == AV_PICTURE_TYPE_B) {
525 h->sym_factor = h->dist[0]*h->scale_den[1];
527 h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
528 h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
532 get_ue_golomb(&s->gb); //bbv_check_times
533 h->progressive = get_bits1(&s->gb);
534 h->pic_structure = 1;
536 h->pic_structure = get_bits1(&s->gb);
537 if(!h->pic_structure && h->stc == PIC_PB_START_CODE)
538 skip_bits1(&s->gb); //advanced_pred_mode_disable
539 skip_bits1(&s->gb); //top_field_first
540 skip_bits1(&s->gb); //repeat_first_field
541 h->qp_fixed = get_bits1(&s->gb);
542 h->qp = get_bits(&s->gb,6);
543 if(h->pic_type == AV_PICTURE_TYPE_I) {
544 if(!h->progressive && !h->pic_structure)
545 skip_bits1(&s->gb);//what is this?
546 skip_bits(&s->gb,4); //reserved bits
548 if(!(h->pic_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
549 h->ref_flag = get_bits1(&s->gb);
550 skip_bits(&s->gb,4); //reserved bits
551 h->skip_mode_flag = get_bits1(&s->gb);
553 h->loop_filter_disable = get_bits1(&s->gb);
554 if(!h->loop_filter_disable && get_bits1(&s->gb)) {
555 h->alpha_offset = get_se_golomb(&s->gb);
556 h->beta_offset = get_se_golomb(&s->gb);
558 h->alpha_offset = h->beta_offset = 0;
560 if(h->pic_type == AV_PICTURE_TYPE_I) {
564 } while(ff_cavs_next_mb(h));
565 } else if(h->pic_type == AV_PICTURE_TYPE_P) {
567 if(check_for_slice(h))
569 if(h->skip_mode_flag && (skip_count < 0))
570 skip_count = get_ue_golomb(&s->gb);
571 if(h->skip_mode_flag && skip_count--) {
572 decode_mb_p(h,P_SKIP);
574 mb_type = get_ue_golomb(&s->gb) + P_SKIP + h->skip_mode_flag;
576 decode_mb_i(h, mb_type - P_8X8 - 1);
578 decode_mb_p(h,mb_type);
580 } while(ff_cavs_next_mb(h));
581 } else { /* AV_PICTURE_TYPE_B */
583 if(check_for_slice(h))
585 if(h->skip_mode_flag && (skip_count < 0))
586 skip_count = get_ue_golomb(&s->gb);
587 if(h->skip_mode_flag && skip_count--) {
588 decode_mb_b(h,B_SKIP);
590 mb_type = get_ue_golomb(&s->gb) + B_SKIP + h->skip_mode_flag;
592 decode_mb_i(h, mb_type - B_8X8 - 1);
594 decode_mb_b(h,mb_type);
596 } while(ff_cavs_next_mb(h));
598 if(h->pic_type != AV_PICTURE_TYPE_B) {
599 if(h->DPB[1].f.data[0])
600 s->avctx->release_buffer(s->avctx, &h->DPB[1].f);
601 h->DPB[1] = h->DPB[0];
602 h->DPB[0] = h->picture;
603 memset(&h->picture,0,sizeof(Picture));
608 /*****************************************************************************
610 * headers and interface
612 ****************************************************************************/
614 static int decode_seq_header(AVSContext *h) {
615 MpegEncContext *s = &h->s;
619 h->profile = get_bits(&s->gb,8);
620 h->level = get_bits(&s->gb,8);
621 skip_bits1(&s->gb); //progressive sequence
622 width = get_bits(&s->gb,14);
623 height = get_bits(&s->gb,14);
624 if ((s->width || s->height) && (s->width != width || s->height != height)) {
625 av_log_missing_feature(s, "Width/height changing in CAVS is", 0);
630 skip_bits(&s->gb,2); //chroma format
631 skip_bits(&s->gb,3); //sample_precision
632 h->aspect_ratio = get_bits(&s->gb,4);
633 frame_rate_code = get_bits(&s->gb,4);
634 skip_bits(&s->gb,18);//bit_rate_lower
635 skip_bits1(&s->gb); //marker_bit
636 skip_bits(&s->gb,12);//bit_rate_upper
637 s->low_delay = get_bits1(&s->gb);
638 h->mb_width = (s->width + 15) >> 4;
639 h->mb_height = (s->height + 15) >> 4;
640 h->s.avctx->time_base.den = avpriv_frame_rate_tab[frame_rate_code].num;
641 h->s.avctx->time_base.num = avpriv_frame_rate_tab[frame_rate_code].den;
642 h->s.avctx->width = s->width;
643 h->s.avctx->height = s->height;
645 ff_cavs_init_top_lines(h);
649 static void cavs_flush(AVCodecContext * avctx) {
650 AVSContext *h = avctx->priv_data;
654 static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
656 const uint8_t *buf = avpkt->data;
657 int buf_size = avpkt->size;
658 AVSContext *h = avctx->priv_data;
659 MpegEncContext *s = &h->s;
661 const uint8_t *buf_end;
662 const uint8_t *buf_ptr;
663 AVFrame *picture = data;
669 if (!s->low_delay && h->DPB[0].f.data[0]) {
670 *data_size = sizeof(AVPicture);
671 *picture = h->DPB[0].f;
672 memset(&h->DPB[0], 0, sizeof(h->DPB[0]));
678 buf_end = buf + buf_size;
680 buf_ptr = avpriv_mpv_find_start_code(buf_ptr,buf_end, &stc);
681 if((stc & 0xFFFFFE00) || buf_ptr == buf_end)
682 return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
683 input_size = (buf_end - buf_ptr)*8;
685 case CAVS_START_CODE:
686 init_get_bits(&s->gb, buf_ptr, input_size);
687 decode_seq_header(h);
689 case PIC_I_START_CODE:
690 if(!h->got_keyframe) {
691 if(h->DPB[0].f.data[0])
692 avctx->release_buffer(avctx, &h->DPB[0].f);
693 if(h->DPB[1].f.data[0])
694 avctx->release_buffer(avctx, &h->DPB[1].f);
697 case PIC_PB_START_CODE:
703 init_get_bits(&s->gb, buf_ptr, input_size);
707 *data_size = sizeof(AVPicture);
708 if(h->pic_type != AV_PICTURE_TYPE_B) {
709 if(h->DPB[1].f.data[0]) {
710 *picture = h->DPB[1].f;
715 *picture = h->picture.f;
718 //mpeg_decode_extension(avctx,buf_ptr, input_size);
720 case USER_START_CODE:
721 //mpeg_decode_user_data(avctx,buf_ptr, input_size);
724 if (stc <= SLICE_MAX_START_CODE) {
725 init_get_bits(&s->gb, buf_ptr, input_size);
726 decode_slice_header(h, &s->gb);
733 AVCodec ff_cavs_decoder = {
735 .type = AVMEDIA_TYPE_VIDEO,
736 .id = AV_CODEC_ID_CAVS,
737 .priv_data_size = sizeof(AVSContext),
738 .init = ff_cavs_init,
739 .close = ff_cavs_end,
740 .decode = cavs_decode_frame,
741 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
743 .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),