Mercurial > libavcodec.hg
comparison cavs.c @ 5250:2a3d31a8c66f libavcodec
split decoder-specific parts into their own file
author | stefang |
---|---|
date | Sun, 08 Jul 2007 07:37:30 +0000 |
parents | dc2579bede07 |
children | cb5d5d2ee6fd |
comparison
equal
deleted
inserted
replaced
5249:dc2579bede07 | 5250:2a3d31a8c66f |
---|---|
358 * | 358 * |
359 * motion vector prediction | 359 * motion vector prediction |
360 * | 360 * |
361 ****************************************************************************/ | 361 ****************************************************************************/ |
362 | 362 |
363 static inline void store_mvs(AVSContext *h) { | |
364 h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 0] = h->mv[MV_FWD_X0]; | |
365 h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 1] = h->mv[MV_FWD_X1]; | |
366 h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 2] = h->mv[MV_FWD_X2]; | |
367 h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 3] = h->mv[MV_FWD_X3]; | |
368 } | |
369 | |
370 static inline void scale_mv(AVSContext *h, int *d_x, int *d_y, vector_t *src, int distp) { | 363 static inline void scale_mv(AVSContext *h, int *d_x, int *d_y, vector_t *src, int distp) { |
371 int den = h->scale_den[src->ref]; | 364 int den = h->scale_den[src->ref]; |
372 | 365 |
373 *d_x = (src->x*distp*den + 256 + (src->x>>31)) >> 9; | 366 *d_x = (src->x*distp*den + 256 + (src->x>>31)) >> 9; |
374 *d_y = (src->y*distp*den + 256 + (src->y>>31)) >> 9; | 367 *d_y = (src->y*distp*den + 256 + (src->y>>31)) >> 9; |
395 mvP->y = ay; | 388 mvP->y = ay; |
396 } else { | 389 } else { |
397 mvP->x = bx; | 390 mvP->x = bx; |
398 mvP->y = by; | 391 mvP->y = by; |
399 } | 392 } |
400 } | |
401 | |
402 static inline void mv_pred_direct(AVSContext *h, vector_t *pmv_fw, | |
403 vector_t *col_mv) { | |
404 vector_t *pmv_bw = pmv_fw + MV_BWD_OFFS; | |
405 int den = h->direct_den[col_mv->ref]; | |
406 int m = col_mv->x >> 31; | |
407 | |
408 pmv_fw->dist = h->dist[1]; | |
409 pmv_bw->dist = h->dist[0]; | |
410 pmv_fw->ref = 1; | |
411 pmv_bw->ref = 0; | |
412 /* scale the co-located motion vector according to its temporal span */ | |
413 pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m; | |
414 pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m); | |
415 m = col_mv->y >> 31; | |
416 pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m; | |
417 pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m); | |
418 } | |
419 | |
420 static inline void mv_pred_sym(AVSContext *h, vector_t *src, enum block_t size) { | |
421 vector_t *dst = src + MV_BWD_OFFS; | |
422 | |
423 /* backward mv is the scaled and negated forward mv */ | |
424 dst->x = -((src->x * h->sym_factor + 256) >> 9); | |
425 dst->y = -((src->y * h->sym_factor + 256) >> 9); | |
426 dst->ref = 0; | |
427 dst->dist = h->dist[0]; | |
428 set_mvs(dst, size); | |
429 } | 393 } |
430 | 394 |
431 void ff_cavs_mv(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC, | 395 void ff_cavs_mv(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC, |
432 enum mv_pred_t mode, enum block_t size, int ref) { | 396 enum mv_pred_t mode, enum block_t size, int ref) { |
433 vector_t *mvP = &h->mv[nP]; | 397 vector_t *mvP = &h->mv[nP]; |
472 set_mvs(mvP,size); | 436 set_mvs(mvP,size); |
473 } | 437 } |
474 | 438 |
475 /***************************************************************************** | 439 /***************************************************************************** |
476 * | 440 * |
477 * residual data decoding | |
478 * | |
479 ****************************************************************************/ | |
480 | |
481 /** kth-order exponential golomb code */ | |
482 static inline int get_ue_code(GetBitContext *gb, int order) { | |
483 if(order) { | |
484 int ret = get_ue_golomb(gb) << order; | |
485 return ret + get_bits(gb,order); | |
486 } | |
487 return get_ue_golomb(gb); | |
488 } | |
489 | |
490 /** | |
491 * decode coefficients from one 8x8 block, dequantize, inverse transform | |
492 * and add them to sample block | |
493 * @param r pointer to 2D VLC table | |
494 * @param esc_golomb_order escape codes are k-golomb with this order k | |
495 * @param qp quantizer | |
496 * @param dst location of sample block | |
497 * @param stride line stride in frame buffer | |
498 */ | |
499 static int decode_residual_block(AVSContext *h, GetBitContext *gb, | |
500 const dec_2dvlc_t *r, int esc_golomb_order, | |
501 int qp, uint8_t *dst, int stride) { | |
502 int i, level_code, esc_code, level, run, mask; | |
503 DCTELEM level_buf[64]; | |
504 uint8_t run_buf[64]; | |
505 DCTELEM *block = h->block; | |
506 | |
507 for(i=0;i<65;i++) { | |
508 level_code = get_ue_code(gb,r->golomb_order); | |
509 if(level_code >= ESCAPE_CODE) { | |
510 run = ((level_code - ESCAPE_CODE) >> 1) + 1; | |
511 esc_code = get_ue_code(gb,esc_golomb_order); | |
512 level = esc_code + (run > r->max_run ? 1 : r->level_add[run]); | |
513 while(level > r->inc_limit) | |
514 r++; | |
515 mask = -(level_code & 1); | |
516 level = (level^mask) - mask; | |
517 } else { | |
518 level = r->rltab[level_code][0]; | |
519 if(!level) //end of block signal | |
520 break; | |
521 run = r->rltab[level_code][1]; | |
522 r += r->rltab[level_code][2]; | |
523 } | |
524 level_buf[i] = level; | |
525 run_buf[i] = run; | |
526 } | |
527 if(dequant(h,level_buf, run_buf, block, ff_cavs_dequant_mul[qp], | |
528 ff_cavs_dequant_shift[qp], i)) | |
529 return -1; | |
530 h->s.dsp.cavs_idct8_add(dst,block,stride); | |
531 return 0; | |
532 } | |
533 | |
534 | |
535 static inline void decode_residual_chroma(AVSContext *h) { | |
536 if(h->cbp & (1<<4)) | |
537 decode_residual_block(h,&h->s.gb,ff_cavs_chroma_dec,0, | |
538 ff_cavs_chroma_qp[h->qp],h->cu,h->c_stride); | |
539 if(h->cbp & (1<<5)) | |
540 decode_residual_block(h,&h->s.gb,ff_cavs_chroma_dec,0, | |
541 ff_cavs_chroma_qp[h->qp],h->cv,h->c_stride); | |
542 } | |
543 | |
544 static inline int decode_residual_inter(AVSContext *h) { | |
545 int block; | |
546 | |
547 /* get coded block pattern */ | |
548 int cbp= get_ue_golomb(&h->s.gb); | |
549 if(cbp > 63){ | |
550 av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n"); | |
551 return -1; | |
552 } | |
553 h->cbp = cbp_tab[cbp][1]; | |
554 | |
555 /* get quantizer */ | |
556 if(h->cbp && !h->qp_fixed) | |
557 h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63; | |
558 for(block=0;block<4;block++) | |
559 if(h->cbp & (1<<block)) | |
560 decode_residual_block(h,&h->s.gb,ff_cavs_inter_dec,0,h->qp, | |
561 h->cy + h->luma_scan[block], h->l_stride); | |
562 decode_residual_chroma(h); | |
563 | |
564 return 0; | |
565 } | |
566 | |
567 /***************************************************************************** | |
568 * | |
569 * macroblock level | |
570 * | |
571 ****************************************************************************/ | |
572 | |
573 static int decode_mb_i(AVSContext *h, int cbp_code) { | |
574 GetBitContext *gb = &h->s.gb; | |
575 int block, pred_mode_uv; | |
576 uint8_t top[18]; | |
577 uint8_t *left = NULL; | |
578 uint8_t *d; | |
579 | |
580 init_mb(h); | |
581 | |
582 /* get intra prediction modes from stream */ | |
583 for(block=0;block<4;block++) { | |
584 int nA,nB,predpred; | |
585 int pos = ff_cavs_scan3x3[block]; | |
586 | |
587 nA = h->pred_mode_Y[pos-1]; | |
588 nB = h->pred_mode_Y[pos-3]; | |
589 predpred = FFMIN(nA,nB); | |
590 if(predpred == NOT_AVAIL) // if either is not available | |
591 predpred = INTRA_L_LP; | |
592 if(!get_bits1(gb)){ | |
593 int rem_mode= get_bits(gb, 2); | |
594 predpred = rem_mode + (rem_mode >= predpred); | |
595 } | |
596 h->pred_mode_Y[pos] = predpred; | |
597 } | |
598 pred_mode_uv = get_ue_golomb(gb); | |
599 if(pred_mode_uv > 6) { | |
600 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n"); | |
601 return -1; | |
602 } | |
603 modify_mb_i(h, &pred_mode_uv); | |
604 | |
605 /* get coded block pattern */ | |
606 if(h->pic_type == FF_I_TYPE) | |
607 cbp_code = get_ue_golomb(gb); | |
608 if(cbp_code > 63){ | |
609 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n"); | |
610 return -1; | |
611 } | |
612 h->cbp = cbp_tab[cbp_code][0]; | |
613 if(h->cbp && !h->qp_fixed) | |
614 h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta | |
615 | |
616 /* luma intra prediction interleaved with residual decode/transform/add */ | |
617 for(block=0;block<4;block++) { | |
618 d = h->cy + h->luma_scan[block]; | |
619 load_intra_pred_luma(h, top, &left, block); | |
620 h->intra_pred_l[h->pred_mode_Y[ff_cavs_scan3x3[block]]] | |
621 (d, top, left, h->l_stride); | |
622 if(h->cbp & (1<<block)) | |
623 decode_residual_block(h,gb,ff_cavs_intra_dec,1,h->qp,d,h->l_stride); | |
624 } | |
625 | |
626 /* chroma intra prediction */ | |
627 load_intra_pred_chroma(h); | |
628 h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10], | |
629 h->left_border_u, h->c_stride); | |
630 h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10], | |
631 h->left_border_v, h->c_stride); | |
632 | |
633 decode_residual_chroma(h); | |
634 ff_cavs_filter(h,I_8X8); | |
635 set_mv_intra(h); | |
636 return 0; | |
637 } | |
638 | |
639 static void decode_mb_p(AVSContext *h, enum mb_t mb_type) { | |
640 GetBitContext *gb = &h->s.gb; | |
641 int ref[4]; | |
642 | |
643 init_mb(h); | |
644 switch(mb_type) { | |
645 case P_SKIP: | |
646 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0); | |
647 break; | |
648 case P_16X16: | |
649 ref[0] = h->ref_flag ? 0 : get_bits1(gb); | |
650 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]); | |
651 break; | |
652 case P_16X8: | |
653 ref[0] = h->ref_flag ? 0 : get_bits1(gb); | |
654 ref[2] = h->ref_flag ? 0 : get_bits1(gb); | |
655 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]); | |
656 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]); | |
657 break; | |
658 case P_8X16: | |
659 ref[0] = h->ref_flag ? 0 : get_bits1(gb); | |
660 ref[1] = h->ref_flag ? 0 : get_bits1(gb); | |
661 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]); | |
662 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, ref[1]); | |
663 break; | |
664 case P_8X8: | |
665 ref[0] = h->ref_flag ? 0 : get_bits1(gb); | |
666 ref[1] = h->ref_flag ? 0 : get_bits1(gb); | |
667 ref[2] = h->ref_flag ? 0 : get_bits1(gb); | |
668 ref[3] = h->ref_flag ? 0 : get_bits1(gb); | |
669 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]); | |
670 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]); | |
671 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]); | |
672 ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]); | |
673 } | |
674 ff_cavs_inter(h, mb_type); | |
675 set_intra_mode_default(h); | |
676 store_mvs(h); | |
677 if(mb_type != P_SKIP) | |
678 decode_residual_inter(h); | |
679 ff_cavs_filter(h,mb_type); | |
680 *h->col_type = mb_type; | |
681 } | |
682 | |
683 static void decode_mb_b(AVSContext *h, enum mb_t mb_type) { | |
684 int block; | |
685 enum sub_mb_t sub_type[4]; | |
686 int flags; | |
687 | |
688 init_mb(h); | |
689 | |
690 /* reset all MVs */ | |
691 h->mv[MV_FWD_X0] = ff_cavs_dir_mv; | |
692 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16); | |
693 h->mv[MV_BWD_X0] = ff_cavs_dir_mv; | |
694 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16); | |
695 switch(mb_type) { | |
696 case B_SKIP: | |
697 case B_DIRECT: | |
698 if(!(*h->col_type)) { | |
699 /* intra MB at co-location, do in-plane prediction */ | |
700 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1); | |
701 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0); | |
702 } else | |
703 /* direct prediction from co-located P MB, block-wise */ | |
704 for(block=0;block<4;block++) | |
705 mv_pred_direct(h,&h->mv[mv_scan[block]], | |
706 &h->col_mv[(h->mby*h->mb_width+h->mbx)*4 + block]); | |
707 break; | |
708 case B_FWD_16X16: | |
709 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1); | |
710 break; | |
711 case B_SYM_16X16: | |
712 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1); | |
713 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16); | |
714 break; | |
715 case B_BWD_16X16: | |
716 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0); | |
717 break; | |
718 case B_8X8: | |
719 for(block=0;block<4;block++) | |
720 sub_type[block] = get_bits(&h->s.gb,2); | |
721 for(block=0;block<4;block++) { | |
722 switch(sub_type[block]) { | |
723 case B_SUB_DIRECT: | |
724 if(!(*h->col_type)) { | |
725 /* intra MB at co-location, do in-plane prediction */ | |
726 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3, | |
727 MV_PRED_BSKIP, BLK_8X8, 1); | |
728 ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS, | |
729 mv_scan[block]-3+MV_BWD_OFFS, | |
730 MV_PRED_BSKIP, BLK_8X8, 0); | |
731 } else | |
732 mv_pred_direct(h,&h->mv[mv_scan[block]], | |
733 &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + block]); | |
734 break; | |
735 case B_SUB_FWD: | |
736 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3, | |
737 MV_PRED_MEDIAN, BLK_8X8, 1); | |
738 break; | |
739 case B_SUB_SYM: | |
740 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3, | |
741 MV_PRED_MEDIAN, BLK_8X8, 1); | |
742 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8); | |
743 break; | |
744 } | |
745 } | |
746 for(block=0;block<4;block++) { | |
747 if(sub_type[block] == B_SUB_BWD) | |
748 ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS, | |
749 mv_scan[block]+MV_BWD_OFFS-3, | |
750 MV_PRED_MEDIAN, BLK_8X8, 0); | |
751 } | |
752 break; | |
753 default: | |
754 assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8)); | |
755 flags = ff_cavs_partition_flags[mb_type]; | |
756 if(mb_type & 1) { /* 16x8 macroblock types */ | |
757 if(flags & FWD0) | |
758 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1); | |
759 if(flags & SYM0) | |
760 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8); | |
761 if(flags & FWD1) | |
762 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1); | |
763 if(flags & SYM1) | |
764 mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8); | |
765 if(flags & BWD0) | |
766 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0); | |
767 if(flags & BWD1) | |
768 ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0); | |
769 } else { /* 8x16 macroblock types */ | |
770 if(flags & FWD0) | |
771 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1); | |
772 if(flags & SYM0) | |
773 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16); | |
774 if(flags & FWD1) | |
775 ff_cavs_mv(h,MV_FWD_X1,MV_FWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,1); | |
776 if(flags & SYM1) | |
777 mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16); | |
778 if(flags & BWD0) | |
779 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0); | |
780 if(flags & BWD1) | |
781 ff_cavs_mv(h,MV_BWD_X1,MV_BWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,0); | |
782 } | |
783 } | |
784 ff_cavs_inter(h, mb_type); | |
785 set_intra_mode_default(h); | |
786 if(mb_type != B_SKIP) | |
787 decode_residual_inter(h); | |
788 ff_cavs_filter(h,mb_type); | |
789 } | |
790 | |
791 /***************************************************************************** | |
792 * | |
793 * slice level | |
794 * | |
795 ****************************************************************************/ | |
796 | |
797 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) { | |
798 if(h->stc > 0xAF) | |
799 av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc); | |
800 h->mby = h->stc; | |
801 if((h->mby == 0) && (!h->qp_fixed)){ | |
802 h->qp_fixed = get_bits1(gb); | |
803 h->qp = get_bits(gb,6); | |
804 } | |
805 /* inter frame or second slice can have weighting params */ | |
806 if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2)) | |
807 if(get_bits1(gb)) { //slice_weighting_flag | |
808 av_log(h->s.avctx, AV_LOG_ERROR, | |
809 "weighted prediction not yet supported\n"); | |
810 } | |
811 return 0; | |
812 } | |
813 | |
814 static inline void check_for_slice(AVSContext *h) { | |
815 GetBitContext *gb = &h->s.gb; | |
816 int align; | |
817 align = (-get_bits_count(gb)) & 7; | |
818 if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) { | |
819 get_bits_long(gb,24+align); | |
820 h->stc = get_bits(gb,8); | |
821 decode_slice_header(h,gb); | |
822 } | |
823 } | |
824 | |
825 /***************************************************************************** | |
826 * | |
827 * frame level | 441 * frame level |
828 * | 442 * |
829 ****************************************************************************/ | 443 ****************************************************************************/ |
830 | 444 |
831 void ff_cavs_init_pic(AVSContext *h) { | 445 void ff_cavs_init_pic(AVSContext *h) { |
848 h->luma_scan[3] = 8*h->l_stride+8; | 462 h->luma_scan[3] = 8*h->l_stride+8; |
849 h->mbx = h->mby = 0; | 463 h->mbx = h->mby = 0; |
850 h->flags = 0; | 464 h->flags = 0; |
851 } | 465 } |
852 | 466 |
853 static int decode_pic(AVSContext *h) { | |
854 MpegEncContext *s = &h->s; | |
855 int skip_count; | |
856 enum mb_t mb_type; | |
857 | |
858 if (!s->context_initialized) { | |
859 s->avctx->idct_algo = FF_IDCT_CAVS; | |
860 if (MPV_common_init(s) < 0) | |
861 return -1; | |
862 ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct); | |
863 } | |
864 get_bits(&s->gb,16);//bbv_dwlay | |
865 if(h->stc == PIC_PB_START_CODE) { | |
866 h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE; | |
867 if(h->pic_type > FF_B_TYPE) { | |
868 av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n"); | |
869 return -1; | |
870 } | |
871 /* make sure we have the reference frames we need */ | |
872 if(!h->DPB[0].data[0] || | |
873 (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE)) | |
874 return -1; | |
875 } else { | |
876 h->pic_type = FF_I_TYPE; | |
877 if(get_bits1(&s->gb)) | |
878 get_bits(&s->gb,16);//time_code | |
879 } | |
880 /* release last B frame */ | |
881 if(h->picture.data[0]) | |
882 s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture); | |
883 | |
884 s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture); | |
885 ff_cavs_init_pic(h); | |
886 h->picture.poc = get_bits(&s->gb,8)*2; | |
887 | |
888 /* get temporal distances and MV scaling factors */ | |
889 if(h->pic_type != FF_B_TYPE) { | |
890 h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512; | |
891 } else { | |
892 h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512; | |
893 } | |
894 h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512; | |
895 h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0; | |
896 h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0; | |
897 if(h->pic_type == FF_B_TYPE) { | |
898 h->sym_factor = h->dist[0]*h->scale_den[1]; | |
899 } else { | |
900 h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0; | |
901 h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0; | |
902 } | |
903 | |
904 if(s->low_delay) | |
905 get_ue_golomb(&s->gb); //bbv_check_times | |
906 h->progressive = get_bits1(&s->gb); | |
907 if(h->progressive) | |
908 h->pic_structure = 1; | |
909 else if(!(h->pic_structure = get_bits1(&s->gb) && (h->stc == PIC_PB_START_CODE)) ) | |
910 get_bits1(&s->gb); //advanced_pred_mode_disable | |
911 skip_bits1(&s->gb); //top_field_first | |
912 skip_bits1(&s->gb); //repeat_first_field | |
913 h->qp_fixed = get_bits1(&s->gb); | |
914 h->qp = get_bits(&s->gb,6); | |
915 if(h->pic_type == FF_I_TYPE) { | |
916 if(!h->progressive && !h->pic_structure) | |
917 skip_bits1(&s->gb);//what is this? | |
918 skip_bits(&s->gb,4); //reserved bits | |
919 } else { | |
920 if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1)) | |
921 h->ref_flag = get_bits1(&s->gb); | |
922 skip_bits(&s->gb,4); //reserved bits | |
923 h->skip_mode_flag = get_bits1(&s->gb); | |
924 } | |
925 h->loop_filter_disable = get_bits1(&s->gb); | |
926 if(!h->loop_filter_disable && get_bits1(&s->gb)) { | |
927 h->alpha_offset = get_se_golomb(&s->gb); | |
928 h->beta_offset = get_se_golomb(&s->gb); | |
929 } else { | |
930 h->alpha_offset = h->beta_offset = 0; | |
931 } | |
932 check_for_slice(h); | |
933 if(h->pic_type == FF_I_TYPE) { | |
934 do { | |
935 decode_mb_i(h, 0); | |
936 } while(next_mb(h)); | |
937 } else if(h->pic_type == FF_P_TYPE) { | |
938 do { | |
939 if(h->skip_mode_flag) { | |
940 skip_count = get_ue_golomb(&s->gb); | |
941 while(skip_count--) { | |
942 decode_mb_p(h,P_SKIP); | |
943 if(!next_mb(h)) | |
944 goto done; | |
945 } | |
946 mb_type = get_ue_golomb(&s->gb) + P_16X16; | |
947 } else | |
948 mb_type = get_ue_golomb(&s->gb) + P_SKIP; | |
949 if(mb_type > P_8X8) { | |
950 decode_mb_i(h, mb_type - P_8X8 - 1); | |
951 } else | |
952 decode_mb_p(h,mb_type); | |
953 } while(next_mb(h)); | |
954 } else { /* FF_B_TYPE */ | |
955 do { | |
956 if(h->skip_mode_flag) { | |
957 skip_count = get_ue_golomb(&s->gb); | |
958 while(skip_count--) { | |
959 decode_mb_b(h,B_SKIP); | |
960 if(!next_mb(h)) | |
961 goto done; | |
962 } | |
963 mb_type = get_ue_golomb(&s->gb) + B_DIRECT; | |
964 } else | |
965 mb_type = get_ue_golomb(&s->gb) + B_SKIP; | |
966 if(mb_type > B_8X8) { | |
967 decode_mb_i(h, mb_type - B_8X8 - 1); | |
968 } else | |
969 decode_mb_b(h,mb_type); | |
970 } while(next_mb(h)); | |
971 } | |
972 done: | |
973 if(h->pic_type != FF_B_TYPE) { | |
974 if(h->DPB[1].data[0]) | |
975 s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]); | |
976 memcpy(&h->DPB[1], &h->DPB[0], sizeof(Picture)); | |
977 memcpy(&h->DPB[0], &h->picture, sizeof(Picture)); | |
978 memset(&h->picture,0,sizeof(Picture)); | |
979 } | |
980 return 0; | |
981 } | |
982 | |
983 /***************************************************************************** | 467 /***************************************************************************** |
984 * | 468 * |
985 * headers and interface | 469 * headers and interface |
986 * | 470 * |
987 ****************************************************************************/ | 471 ****************************************************************************/ |
1003 | 487 |
1004 /* alloc space for co-located MVs and types */ | 488 /* alloc space for co-located MVs and types */ |
1005 h->col_mv = av_malloc( h->mb_width*h->mb_height*4*sizeof(vector_t)); | 489 h->col_mv = av_malloc( h->mb_width*h->mb_height*4*sizeof(vector_t)); |
1006 h->col_type_base = av_malloc(h->mb_width*h->mb_height); | 490 h->col_type_base = av_malloc(h->mb_width*h->mb_height); |
1007 h->block = av_mallocz(64*sizeof(DCTELEM)); | 491 h->block = av_mallocz(64*sizeof(DCTELEM)); |
1008 } | |
1009 | |
1010 static int decode_seq_header(AVSContext *h) { | |
1011 MpegEncContext *s = &h->s; | |
1012 int frame_rate_code; | |
1013 | |
1014 h->profile = get_bits(&s->gb,8); | |
1015 h->level = get_bits(&s->gb,8); | |
1016 skip_bits1(&s->gb); //progressive sequence | |
1017 s->width = get_bits(&s->gb,14); | |
1018 s->height = get_bits(&s->gb,14); | |
1019 skip_bits(&s->gb,2); //chroma format | |
1020 skip_bits(&s->gb,3); //sample_precision | |
1021 h->aspect_ratio = get_bits(&s->gb,4); | |
1022 frame_rate_code = get_bits(&s->gb,4); | |
1023 skip_bits(&s->gb,18);//bit_rate_lower | |
1024 skip_bits1(&s->gb); //marker_bit | |
1025 skip_bits(&s->gb,12);//bit_rate_upper | |
1026 s->low_delay = get_bits1(&s->gb); | |
1027 h->mb_width = (s->width + 15) >> 4; | |
1028 h->mb_height = (s->height + 15) >> 4; | |
1029 h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num; | |
1030 h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den; | |
1031 h->s.avctx->width = s->width; | |
1032 h->s.avctx->height = s->height; | |
1033 if(!h->top_qp) | |
1034 ff_cavs_init_top_lines(h); | |
1035 return 0; | |
1036 } | |
1037 | |
1038 static void cavs_flush(AVCodecContext * avctx) { | |
1039 AVSContext *h = avctx->priv_data; | |
1040 h->got_keyframe = 0; | |
1041 } | |
1042 | |
1043 static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size, | |
1044 uint8_t * buf, int buf_size) { | |
1045 AVSContext *h = avctx->priv_data; | |
1046 MpegEncContext *s = &h->s; | |
1047 int input_size; | |
1048 const uint8_t *buf_end; | |
1049 const uint8_t *buf_ptr; | |
1050 AVFrame *picture = data; | |
1051 uint32_t stc; | |
1052 | |
1053 s->avctx = avctx; | |
1054 | |
1055 if (buf_size == 0) { | |
1056 if(!s->low_delay && h->DPB[0].data[0]) { | |
1057 *data_size = sizeof(AVPicture); | |
1058 *picture = *(AVFrame *) &h->DPB[0]; | |
1059 } | |
1060 return 0; | |
1061 } | |
1062 | |
1063 buf_ptr = buf; | |
1064 buf_end = buf + buf_size; | |
1065 for(;;) { | |
1066 buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc); | |
1067 if(stc & 0xFFFFFE00) | |
1068 return FFMAX(0, buf_ptr - buf - s->parse_context.last_index); | |
1069 input_size = (buf_end - buf_ptr)*8; | |
1070 switch(stc) { | |
1071 case CAVS_START_CODE: | |
1072 init_get_bits(&s->gb, buf_ptr, input_size); | |
1073 decode_seq_header(h); | |
1074 break; | |
1075 case PIC_I_START_CODE: | |
1076 if(!h->got_keyframe) { | |
1077 if(h->DPB[0].data[0]) | |
1078 avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]); | |
1079 if(h->DPB[1].data[0]) | |
1080 avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]); | |
1081 h->got_keyframe = 1; | |
1082 } | |
1083 case PIC_PB_START_CODE: | |
1084 *data_size = 0; | |
1085 if(!h->got_keyframe) | |
1086 break; | |
1087 init_get_bits(&s->gb, buf_ptr, input_size); | |
1088 h->stc = stc; | |
1089 if(decode_pic(h)) | |
1090 break; | |
1091 *data_size = sizeof(AVPicture); | |
1092 if(h->pic_type != FF_B_TYPE) { | |
1093 if(h->DPB[1].data[0]) { | |
1094 *picture = *(AVFrame *) &h->DPB[1]; | |
1095 } else { | |
1096 *data_size = 0; | |
1097 } | |
1098 } else | |
1099 *picture = *(AVFrame *) &h->picture; | |
1100 break; | |
1101 case EXT_START_CODE: | |
1102 //mpeg_decode_extension(avctx,buf_ptr, input_size); | |
1103 break; | |
1104 case USER_START_CODE: | |
1105 //mpeg_decode_user_data(avctx,buf_ptr, input_size); | |
1106 break; | |
1107 default: | |
1108 if (stc >= SLICE_MIN_START_CODE && | |
1109 stc <= SLICE_MAX_START_CODE) { | |
1110 init_get_bits(&s->gb, buf_ptr, input_size); | |
1111 decode_slice_header(h, &s->gb); | |
1112 } | |
1113 break; | |
1114 } | |
1115 } | |
1116 } | 492 } |
1117 | 493 |
1118 int ff_cavs_init(AVCodecContext *avctx) { | 494 int ff_cavs_init(AVCodecContext *avctx) { |
1119 AVSContext *h = avctx->priv_data; | 495 AVSContext *h = avctx->priv_data; |
1120 MpegEncContext * const s = &h->s; | 496 MpegEncContext * const s = &h->s; |
1159 av_free(h->col_mv); | 535 av_free(h->col_mv); |
1160 av_free(h->col_type_base); | 536 av_free(h->col_type_base); |
1161 av_free(h->block); | 537 av_free(h->block); |
1162 return 0; | 538 return 0; |
1163 } | 539 } |
1164 | |
1165 AVCodec cavs_decoder = { | |
1166 "cavs", | |
1167 CODEC_TYPE_VIDEO, | |
1168 CODEC_ID_CAVS, | |
1169 sizeof(AVSContext), | |
1170 ff_cavs_init, | |
1171 NULL, | |
1172 ff_cavs_end, | |
1173 cavs_decode_frame, | |
1174 CODEC_CAP_DR1 | CODEC_CAP_DELAY, | |
1175 .flush= cavs_flush, | |
1176 }; |