ffplay代码分析(2)数据读取及处理

ffplay代码分析(2)数据读取及处理

一 准备工作

初始化 复制代码
VideoState *is = arg;
    AVFormatContext *ic = NULL;
    int err, i, ret;
    int st_index[AVMEDIA_TYPE_NB];
    AVPacket pkt1, *pkt = &pkt1;
    int64_t stream_start_time;
    int pkt_in_play_range = 0;
    AVDictionaryEntry *t;
    SDL_mutex *wait_mutex = SDL_CreateMutex();
    int scan_all_pmts_set = 0;
    int64_t pkt_ts;
  1. avformat_alloc_context 创建上下文
scss 复制代码
// 为AVFormatContext分配内存
AVFormatContext *ic = avformat_alloc_context();
if (!ic) {
    // 处理错误,例如内存不足
}
  1. ic->interrupt_callback
    interrupt_callback是一个用于处理IO操作中断的回调机制。特别是在avformat_open_input或其他可能阻塞的函数中,它可以被用来提前结束或中断这些函数的执行。其中包含两个成员:

    callback: 一个函数指针,它会被周期性地调用来检查是否应该中断当前的IO操作。
    opaque: 一个指针,指向用户数据或上下文,它会被传递给上面的回调函数。

ffplay.c的上下文中,interrupt_callback被设置为一个函数,该函数检查用户是否已经请求停止播放或加载。

ini 复制代码
// 设置某些自定义的回调或参数
ic->interrupt_callback.callback = decode_interrupt_cb;
ic->interrupt_callback.opaque = is;
  1. avformat_open_input() 打开媒体文件
    打开指定的文件,设置相关选项,并准备获取文件中的流信息。
ini 复制代码
/*
使用avformat_open_input函数尝试打开指定的媒体文件(由`is->filename`给出),使用指定的输入格式(由`is->iformat`给出)并返回结果到`err`变量。`&format_opts`是一个可能包含打开文件时要使用的选项的字典。
*/
err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
    if (err < 0) {
        print_error(is->filename, err);
        ret = -1;
        goto fail;
    }
// 如果scan_all_pmts_set为真,则在format_opts字典中设置scan_all_pmts选项。这与解复用(demuxing)流有关。
    if (scan_all_pmts_set)
        av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
// 使用av_dict_get函数尝试从format_opts字典中获取一个键。如果获取到,执行括号内的代码。
    if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
        av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
        ret = AVERROR_OPTION_NOT_FOUND;
        goto fail;
    }
    is->ic = ic;    // videoState的ic指向分配的ic
// 如果genpts为真,则在ic->flags中设置AVFMT_FLAG_GENPTS标志。这是与生成时间戳有关的选项。
    if (genpts)
        ic->flags |= AVFMT_FLAG_GENPTS;
// 调将全局边际数据(例如元数据)注入到AVFormatContext中。
    av_format_inject_global_side_data(ic);

    if (find_stream_info) {
// 使用setup_find_stream_info_opts函数为给定的AVFormatContext和编解码器选项codec_opts设置流信息选项。
        AVDictionary **opts = setup_find_stream_info_opts(ic, codec_opts);
//获取AVFormatContext中的流数量并存储在orig_nb_streams变量中。
        int orig_nb_streams = ic->nb_streams;
  1. avformat_find_stream_info()
    获取多媒体文件的流信息,并进行一些基于获取到的信息的设置和配置。探测媒体类型并获得文件的封装格式、音视频编码参数等详细信息。调用这个函数可以得到更详细的信息,而不仅仅是通过avformat_open_input得到的。
ini 复制代码
// 调用avformat_find_stream_info函数为给定的AVFormatContext(ic)获取流信息
        err = avformat_find_stream_info(ic, opts);
// 释放前面为每个流创建的字典中的所有项。
        for (i = 0; i < orig_nb_streams; i++)
            av_dict_free(&opts[i]);
// 使用av_freep函数释放之前分配的opts数组
        av_freep(&opts);

        if (err < 0) {
            av_log(NULL, AV_LOG_WARNING,
                   "%s: could not find codec parameters\n", is->filename);
            ret = -1;
            goto fail;
        }

// 检查AVFormatContext的pb字段(一个AVIOContext指针)。如果它存在将其eof_reached字段设为0。
    if (ic->pb)
        ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use avio_feof() to test for the end
// 检查seek_by_bytes的值。如果小于0,根据ic->iformat的标志和名称设置其值。
    if (seek_by_bytes < 0)
        seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT) && strcmp("ogg", ic->iformat->name);

    is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;

// 使用av_asprintf函数格式化标题,并将结果赋给window_title。这个标题用于显示窗口的标题。
    if (!window_title && (t = av_dict_get(ic->metadata, "title", NULL, 0)))
        window_title = av_asprintf("%s - %s", t->value, input_filename);
  1. 检测是否指定播放起始时间
    首先尝试将播放位置设置为用户指定的起始时间,然后检查流是否为实时流,并根据需要显示流的详细信息。
scss 复制代码
// 检查start_time是否不等于AV_NOPTS_VALUE(表示时间戳的特殊值,通常表示未知或不适用的时间戳)
    if (start_time != AV_NOPTS_VALUE) {
        int64_t timestamp;    // 存储时间戳

        timestamp = start_time;
        /* add the stream start time */
        if (ic->start_time != AV_NOPTS_VALUE)
            timestamp += ic->start_time;
        // seek的指定的位置开始播放
// 使用avformat_seek_file函数在多媒体文件中查找(seek)到指定的时间戳。此函数试图在指定的时间戳附近查找最近的关键帧。
        ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
        if (ret < 0) {
            av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
                    is->filename, (double)timestamp / AV_TIME_BASE);
        }
    }

    /* 是否为实时流媒体 */
    is->realtime = is_realtime(ic);

    if (show_status)
        av_dump_format(ic, 0, is->filename, 0);
  1. 查找AVStream
    首先尝试匹配用户指定的流,如果没有匹配的流,记录一个错误消息。接着会自动选择最佳的视频、音频和字幕流,最后设置显示模式。
ini 复制代码
// 6. 查找AVStream
    // 6.1 根据用户指定来查找流,
    for (i = 0; i < ic->nb_streams; i++) {
        AVStream *st = ic->streams[i];
        
        // 获取流的媒体类型(例如,音频、视频或字幕)。
        enum AVMediaType type = st->codecpar->codec_type;
        // 设置流的默认状态为丢弃,意味着在默认情况下不播放此流。
        st->discard = AVDISCARD_ALL;
        
        // 检查用户是否指定了某种类型的流,如果是,则使用
        if (type >= 0 && wanted_stream_spec[type] && st_index[type] == -1)
            if (avformat_match_stream_specifier(ic, st, wanted_stream_spec[type]) > 0)
                st_index[type] = i;
    }
    for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
        if (wanted_stream_spec[i] && st_index[i] == -1) {
            av_log(NULL, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n",
                   wanted_stream_spec[i], av_get_media_type_string(i));
//            st_index[i] = INT_MAX;
            st_index[i] = -1;
        }
    }
    // 6.2 利用av_find_best_stream选择流,基于流的属性和质量。
    if (!video_disable)
        st_index[AVMEDIA_TYPE_VIDEO] =
            av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
                                st_index[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
    if (!audio_disable)
        st_index[AVMEDIA_TYPE_AUDIO] =
            av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
                                st_index[AVMEDIA_TYPE_AUDIO],
                                st_index[AVMEDIA_TYPE_VIDEO],
                                NULL, 0);
    if (!video_disable && !subtitle_disable)
        st_index[AVMEDIA_TYPE_SUBTITLE] =
            av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
                                st_index[AVMEDIA_TYPE_SUBTITLE],
                                (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
                                 st_index[AVMEDIA_TYPE_AUDIO] :
                                 st_index[AVMEDIA_TYPE_VIDEO]),
                                NULL, 0);
   // 设置显示模式为之前定义的show_mode
    is->show_mode = show_mode;
  1. 通过AVCodecParametersav_guess_sample_aspect_ratio计算出显示图像的宽、高
    从待处理流中获取相关参数,设置显示窗口的宽度、高度及宽高比。这里实质只是获取了宽高,并没有进行设置,真正调整窗口大小是在视频显示调用video_open()函数进行设置。
ini 复制代码
// st_index[AVMEDIA_TYPE_VIDEO]存储了视频流的索引,如果值为-1,说明没有视频流。
    if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
        // 从多媒体文件中获取视频流的指针
        AVStream *st = ic->streams[st_index[AVMEDIA_TYPE_VIDEO]];  
        // 获取视频流的编解码器参数,它包含了该流的详细信息,如宽度、高度、编解码器类型等。
        AVCodecParameters *codecpar = st->codecpar;
        /*
        根据流和帧宽高比猜测帧的样本宽高比(SAR)。SAR是像素的宽度和高度之间的比率。
        这通常是为了确保在不同的显示器或播放设备上播放时,视频会正确地显示。该值只是一个参考。
        */
        AVRational sar = av_guess_sample_aspect_ratio(ic, st, NULL);
        if (codecpar->width) {
            // 设置显示窗口的大小和宽高比
            set_default_window_size(codecpar->width, codecpar->height, sar);
        }
    }
  1. stream_component_open()
    初始化和配置多媒体文件中的音频、视频和字幕流。对于每种流,都会检查其是否存在,然后使用stream_component_open函数进行初始化和配置。此外,根据流的成功打开状态,设置合适的显示模式。
ini 复制代码
/* open the streams */
    /* 8. 打开视频、音频解码器。在此会打开相应解码器,并创建相应的解码线程。 */
    if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {// 如果有音频流则打开音频流
        stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
    }

    ret = -1;
    if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) { // 如果有视频流则打开视频流
        ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
    }
    if (is->show_mode == SHOW_MODE_NONE) {
        //选择怎么显示,如果视频打开成功,就显示视频画面,否则,显示音频对应的频谱图
        is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
    }

    if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) { // 如果有字幕流则打开字幕流
        stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
    }

    if (is->video_stream < 0 && is->audio_stream < 0) {
        av_log(NULL, AV_LOG_FATAL, "Failed to open file '%s' or configure filtergraph\n",
               is->filename);
        ret = -1;
        goto fail;
    }

    if (infinite_buffer < 0 && is->realtime)
        infinite_buffer = 1;    // 如果是实时流

二 For循环流程

  1. 检测是否退出
    • 使用is->abort_request来判断是否退出。
kotlin 复制代码
if (is->abort_request)
          break;
  1. 检测是否暂停/继续
    • 根据is->pausedis->last_paused来判断暂停状态。
    • 如果暂停,利用av_read_pause(ic);否则,使用av_read_play(ic)
csharp 复制代码
if (is->paused != is->last_paused) {
          is->last_paused = is->paused;
          if (is->paused)
              is->read_pause_return = av_read_pause(ic); // 网络流的时候有用
          else
              av_read_play(ic);
      }
  1. 处理RTSP或MMSH协议
    • 等待10ms,以避免立即尝试获取另一个数据包。
arduino 复制代码
#if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
      if (is->paused &&
              (!strcmp(ic->iformat->name, "rtsp") ||
               (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
          /* wait 10 ms to avoid trying to get another packet */
          /* XXX: horrible */
          // 等待10ms,避免立马尝试下一个Packet
          SDL_Delay(10);
          continue;
      }
#endif
  1. 检测是否seek
    • 根据is->seek_req判断是否有seek请求。
    • 设置seek的目标、最小和最大值。
    • 执行seek操作,并根据结果决定是否刷新队列和解码器。
rust 复制代码
        if (is->seek_req) { // 是否有seek请求
          int64_t seek_target = is->seek_pos;
          int64_t seek_min    = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
          int64_t seek_max    = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
  // FIXME the +-2 is due to rounding being not done in the correct direction in generation
  //      of the seek_pos/seek_rel variables
          // 修复由于四舍五入,没有再seek_pos/seek_rel变量的正确方向上进行
          ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
          if (ret < 0) {
              av_log(NULL, AV_LOG_ERROR,
                     "%s: error while seeking\n", is->ic->url);
          } else {
              /* seek的时候,要把原先的数据情况,并重启解码器,put flush_pkt的目的是告知解码线程需要
               * reset decoder
               */
              if (is->audio_stream >= 0) { // 如果有音频流
                  packet_queue_flush(&is->audioq);    // 清空packet队列数据
                  // 放入flush pkt, 用来开起新的一个播放序列, 解码器读取到flush_pkt也清空解码器
                  packet_queue_put(&is->audioq, &flush_pkt);
              }
              if (is->subtitle_stream >= 0) { // 如果有字幕流
                  packet_queue_flush(&is->subtitleq); // 和上同理
                  packet_queue_put(&is->subtitleq, &flush_pkt);
              }
              if (is->video_stream >= 0) {    // 如果有视频流
                  packet_queue_flush(&is->videoq);    // 和上同理
                  packet_queue_put(&is->videoq, &flush_pkt);
              }
              if (is->seek_flags & AVSEEK_FLAG_BYTE) {
                 set_clock(&is->extclk, NAN, 0);
              } else {
                 set_clock(&is->extclk, seek_target / (double)AV_TIME_BASE, 0);
              }
          }
          is->seek_req = 0;
          is->queue_attachments_req = 1;
          is->eof = 0;
          if (is->paused)
              step_to_next_frame(is);
      }
  1. 检测video是否为attached_pic
    • 如果视频流附带有图片,如MP3或AAC的专辑封面,则将其放入队列。
ini 复制代码
        if (is->queue_attachments_req) {
          // attached_pic 附带的图片。比如说一些MP3,AAC音频文件附带的专辑封面,所以需要注意的是音频文件不一定只存在音频流本身
          if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
              AVPacket copy = { 0 };
              if ((ret = av_packet_ref(&copy, &is->video_st->attached_pic)) < 0)
                  goto fail;
              packet_queue_put(&is->videoq, &copy);
              packet_queue_put_nullpacket(&is->videoq, is->video_stream);
          }
          is->queue_attachments_req = 0;
      }
  1. 检测队列是否已有足够数据
    • 如果三个队列(音频、视频和字幕)的总大小是否超过了一个定义的最大值MAX_QUEUE_SIZE。或者缓存队列中有足够的数据包,则不需要继续读取数据。
rust 复制代码
/* if the queue are full, no need to read more */
/* 缓存队列有足够的包,不需要继续读取数据 */
if (infinite_buffer<1 &&
(is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
|| (stream_has_enough_packets(is->audio_st, is->audio_stream, &is->audioq) &&
  stream_has_enough_packets(is->video_st, is->video_stream, &is->videoq) &&
stream_has_enough_packets(is->subtitle_st, is->subtitle_stream, &is->subtitleq)))) {
 /* wait 10 ms */
 SDL_LockMutex(wait_mutex);
 // 如果没有唤醒则超时10ms退出,比如在seek操作时这里会被唤醒
 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
 SDL_UnlockMutex(wait_mutex);
 continue;
}
  1. 检测码流是否已播放结束
    • 根据是否有音频、视频流以及播放是否完成来判断。
rust 复制代码
        if (!is->paused // 非暂停
              && // 这里的执行是因为码流读取完毕后 插入空包所致
              (!is->audio_st // 没有音频流
                  || (is->auddec.finished == is->audioq.serial // 或者音频播放完毕
                          && frame_queue_nb_remaining(&is->sampq) == 0))
              && (!is->video_st // 没有视频流
                  || (is->viddec.finished == is->videoq.serial // 或者视频播放完毕
                          && frame_queue_nb_remaining(&is->pictq) == 0))) {
          if (loop != 1           // a 是否循环播放
                  && (!loop || --loop)) {
              // stream_seek不是ffmpeg的函数,是ffplay封装的,每次seek的时候会调用
              stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
          } else if (autoexit) {  // b 是否自动退出
              ret = AVERROR_EOF;
              goto fail;
          }
      }
  1. 读取媒体数据
    • 使用av_read_frame(ic, pkt)读取音视频分离后、解码前的数据。
ini 复制代码
ret = av_read_frame(ic, pkt); // 调用不会释放pkt的数据,需要我们自己去释放packet的数据
  1. 检测数据是否读取完毕
    • 如果数据读取完毕,插入一个空数据包以确保从解码器中读出所有帧。

这里使用了SDL的互斥锁和条件变量:

  • 首先,程序锁定一个互斥锁,确保在等待时不会有其他线程干扰。
  • 使用 SDL_CondWaitTimeout,程序等待一个信号来继续执行或者等待10毫秒。这意味着如果在10毫秒内没有其他部分的代码发送信号唤醒该线程,那么线程会在10毫秒后自动继续执行。
  • 最后,释放互斥锁并继续循环。
rust 复制代码
    if (ret < 0) {
       if ((ret == AVERROR_EOF || avio_feof(ic->pb))
             && !is->eof)
       {
           // 插入空包说明码流数据读取完毕了,之前讲解码的时候说过刷空包是为了从解码器把所有帧都读出来
         if (is->video_stream >= 0)
              packet_queue_put_nullpacket(&is->videoq, is->video_stream);
         if (is->audio_stream >= 0)
            packet_queue_put_nullpacket(&is->audioq, is->audio_stream);
         if (is->subtitle_stream >= 0)
              packet_queue_put_nullpacket(&is->subtitleq, is->subtitle_stream);
              is->eof = 1;        // 文件读取完毕
       }
       if (ic->pb && ic->pb->error)
           break;
       SDL_LockMutex(wait_mutex);
       SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
       SDL_UnlockMutex(wait_mutex);
       continue;		// 继续循环
      } else {
          is->eof = 0;
   }
  1. 检测是否在播放范围内
    • 根据播放范围决定是否丢弃或队列数据包。
rust 复制代码
/* check if packet is in play range specified by user, then queue, otherwise discard */
stream_start_time = ic->streams[pkt->stream_index]->start_time; // 获取流的起始时间
pkt_ts = pkt->pts == AV_NOPTS_VALUE ? pkt->dts : pkt->pts; // 获取packet的时间戳
// 这里的duration是在命令行时用来指定播放长度
pkt_in_play_range = duration == AV_NOPTS_VALUE ||
(pkt_ts - (stream_start_time != AV_NOPTS_VALUE ? stream_start_time : 0)) *
av_q2d(ic->streams[pkt->stream_index]->time_base) -
(double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
<= ((double)duration / 1000000);
  1. 将音视频数据分别送入相应的队列
    • 根据数据包的流索引将其放入相应的音频、视频或字幕队列。
rust 复制代码
      if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
          packet_queue_put(&is->audioq, pkt);
      } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
                 && !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
          //printf("pkt pts:%ld, dts:%ld\n", pkt->pts, pkt->dts);
          packet_queue_put(&is->videoq, pkt);
      } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
          packet_queue_put(&is->subtitleq, pkt);
      } else {
          av_packet_unref(pkt);// // 不入队列则直接释放数据
      }
  }

3 退出处理逻辑

ini 复制代码
    ret = 0;
 fail:
    if (ic && !is->ic)
        avformat_close_input(&ic);

    if (ret != 0) {
        SDL_Event event;

        event.type = FF_QUIT_EVENT;
        event.user.data1 = is;
        SDL_PushEvent(&event);
    }
    SDL_DestroyMutex(wait_mutex);  // 销毁互斥锁
    return 0;
}
相关推荐
x007xyz1 个月前
前端纯手工绘制音频波形图
前端·音视频开发·canvas
音视频牛哥1 个月前
Android摄像头采集选Camera1还是Camera2?
音视频开发·视频编码·直播
九酒2 个月前
【harmonyOS NEXT 下的前端开发者】WAV音频编码实现
前端·harmonyos·音视频开发
音视频牛哥2 个月前
结合GB/T28181规范探讨Android平台设备接入模块心跳实现
音视频开发·视频编码·直播
哔哩哔哩技术2 个月前
自研点直播转码核心
音视频开发
音视频牛哥2 个月前
Android平台轻量级RTSP服务模块二次封装版调用说明
音视频开发·视频编码·直播
音视频牛哥2 个月前
Android平台RTSP|RTMP直播播放器技术接入说明
音视频开发·视频编码·直播
山雨楼2 个月前
ExoPlayer架构详解与源码分析(15)——Renderer
android·架构·音视频开发
音视频牛哥2 个月前
Windows平台如何实现多路RTSP|RTMP流合成后录像或转发RTMP服务
音视频开发·视频编码·直播
音视频牛哥2 个月前
GB28181设备接入模块和轻量级RTSP服务有什么区别?
音视频开发·视频编码·直播