Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2116841
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2016-05-19 19:06:16

1. av_find_best_stream
a. 就是要获取音视频及字幕的stream_index
b.以前没有函数av_find_best_stream时,获取索引可以通过如下
  1. for(i=0; i<is->pFormatCtx->nb_streams; i++)
  2.     {
  3.         if(is->pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  4.         {
  5.             is->videoindex= i;
  6.         }
  7.         if(is->pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  8.         {
  9.             is->sndindex= i;
  10.         }
  11.     }
2.video及audio的调用如下:
  1.                        ic     type              wanted_stream_nb            related_stream             decoder_ret flag
  2. st_index[AVMEDIA_TYPE_VIDEO] =
  3.    av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, st_index[AVMEDIA_TYPE_VIDEO],  -1,                         NULL,      0);
  4. st_index[AVMEDIA_TYPE_AUDIO] =
  5.    av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, st_index[AVMEDIA_TYPE_AUDIO],st_index[AVMEDIA_TYPE_VIDEO], NULL,      0);
3.说明
  1. int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream,
  2.                         AVCodec **decoder_ret, int flags)
  3. {
  4.     int i, nb_streams = ic->nb_streams;
  5.     int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1, best_bitrate = -1, best_multiframe = -1, count, bitrate, multiframe;
  6.     unsigned *program = NULL;
  7.     const AVCodec *decoder = NULL, *best_decoder = NULL;

  8.     if (related_stream >= 0 && wanted_stream_nb < 0) {                  //没看到有什么作用,即参数related_stream在这儿没有用
  9.         AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream);
  10.         if (p) {
  11.             program = p->stream_index;
  12.             nb_streams = p->nb_stream_indexes;
  13.         }
  14.     }
  15.     for (i = 0; i < nb_streams; i++) {                                   //对于只有音频与视频流的媒体文件来说nb_streams=2
  16.         int real_stream_index = program ? program[i] : i;                //program=NULL,所以real_stream_index=i;
  17.         AVStream *st = ic->streams[real_stream_index];
  18.         AVCodecContext *avctx = st->codec;
  19.         if (avctx->codec_type != type)                                    //以下三个if是过滤条件
  20.             continue;
  21.         if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
  22.             continue;
  23.         if (wanted_stream_nb != real_stream_index &&
  24.             st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED |
  25.                                AV_DISPOSITION_VISUAL_IMPAIRED))
  26.             continue;
  27.         if (type == AVMEDIA_TYPE_AUDIO && !(avctx->channels && avctx->sample_rate))
  28.             continue;
  29.         if (decoder_ret) {                                          //decoder_ret=NULL,所以下面这个find_decoder也没有调用
  30.             decoder = find_decoder(ic, st, st->codec->codec_id);
  31.             if (!decoder) {
  32.                 if (ret < 0)
  33.                     ret = AVERROR_DECODER_NOT_FOUND;
  34.                 continue;
  35.             }
  36.         }
  37.         count = st->codec_info_nb_frames;
  38.         bitrate = avctx->bit_rate;
  39.         if (!bitrate)
  40.             bitrate = avctx->rc_max_rate;
  41.         multiframe = FFMIN(5, count);
  42.         if ((best_multiframe > multiframe) ||
  43.             (best_multiframe == multiframe && best_bitrate > bitrate) ||
  44.             (best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count))
  45.             continue;
  46.         best_count = count;
  47.         best_bitrate = bitrate;
  48.         best_multiframe = multiframe;
  49.         ret = real_stream_index;                                  //到这儿real_stream_index就是匹配了三个if的index了,不匹配的都continue了
  50.         best_decoder = decoder;
  51.         if (program && i == nb_streams - 1 && ret < 0) {
  52.             program = NULL;
  53.             nb_streams = ic->nb_streams;
  54.             /* no related stream found, try again with everything */
  55.             i = 0;
  56.         }
  57.     }
  58.     if (decoder_ret)
  59.         *decoder_ret = (AVCodec*)best_decoder;
  60.     return ret;                                                 //返回就完事了
  61. }
av_find_best_stream函数其实跟以前实现思路是一样的,只不过这儿的条件更多一点而己。
阅读(4585) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~