ffmpeg 源代碼簡單分析 : avcodec_decode_video2()

此前寫了好幾篇ffmpeg源代碼分析文章,列表如下:

圖解FFMPEG打開媒體的函數avformat_open_input
ffmpeg 源代碼簡單分析 : av_register_all()
ffmpeg 源代碼簡單分析 : avcodec_register_all()
ffmpeg 源代碼簡單分析 : av_read_frame()
ffmpeg 源代碼簡單分析 : avcodec_decode_video2()

============================


ffmpeg中的avcodec_decode_video2()的作用是解碼一幀視頻數據。輸入一個壓縮編碼的結構體AVPacket,輸出一個解碼後的結構體AVFrame。


查看源代碼之後發現,這個函數竟然十分的簡單,源代碼如下:

  1. int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,  
  2.                          int *got_picture_ptr,  
  3.                          const AVPacket *avpkt)  
  4. {  
  5.     int ret;  
  6.     // copy to ensure we do not change avpkt  
  7.     AVPacket tmp = *avpkt;  
  8.   
  9.     *got_picture_ptr= 0;  
  10.     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))  
  11.         return -1;  
  12.   
  13.     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){  
  14.         int did_split = av_packet_split_side_data(&tmp);  
  15.         apply_param_change(avctx, &tmp);  
  16.         avctx->pkt = &tmp;  
  17.         if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)  
  18.              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,  
  19.                                           &tmp);  
  20.         else {  
  21.             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,  
  22.                               &tmp);  
  23.             picture->pkt_dts= avpkt->dts;  
  24.   
  25.             if(!avctx->has_b_frames){  
  26.             picture->pkt_pos= avpkt->pos;  
  27.             }  
  28.             //FIXME these should be under if(!avctx->has_b_frames)  
  29.             if (!picture->sample_aspect_ratio.num)  
  30.                 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;  
  31.             if (!picture->width)  
  32.                 picture->width = avctx->width;  
  33.             if (!picture->height)  
  34.                 picture->height = avctx->height;  
  35.             if (picture->format == PIX_FMT_NONE)  
  36.                 picture->format = avctx->pix_fmt;  
  37.         }  
  38.   
  39.         emms_c(); //needed to avoid an emms_c() call before every return;  
  40.   
  41.         avctx->pkt = NULL;  
  42.         if (did_split)  
  43.             ff_packet_free_side_data(&tmp);  
  44.   
  45.         if (*got_picture_ptr){  
  46.             avctx->frame_number++;  
  47.             picture->best_effort_timestamp = guess_correct_pts(avctx,  
  48.                                                             picture->pkt_pts,  
  49.                                                             picture->pkt_dts);  
  50.         }  
  51.     }else  
  52.         ret= 0;  
  53.   
  54.     return ret;  
  55. }  

從代碼中可以看出,通過ret = avctx->codec->decode(avctx, picture, got_picture_ptr,&tmp)這句代碼,調用了相應解碼器的decode()函數,完成了解碼操作。


原文地址:http://blog.csdn.net/leixiaohua1020/article/details/12679719

發佈了4 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章