123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #include "demux.h"
- #include "packet.h"
- static int decode_interrupt_cb(void *ctx)
- {
- player_stat_t *is = (player_stat_t*)ctx;
- return is->abort_request;
- }
- static int demux_init(player_stat_t *is)
- {
- AVFormatContext *p_fmt_ctx = NULL;
- int err, i, ret;
- int a_idx;
- int v_idx;
- p_fmt_ctx = avformat_alloc_context();
- if (!p_fmt_ctx)
- {
- //is->rvc_log("Could not allocate context.");
- ret = AVERROR(ENOMEM);
- goto fail;
- }
- // 中断回调机制。为底层I/O层提供一个处理接口,比如中止IO操作。
- p_fmt_ctx->interrupt_callback.callback = decode_interrupt_cb;
- p_fmt_ctx->interrupt_callback.opaque = is;
- // 1. 构建AVFormatContext
- // 1.1 打开视频文件:读取文件头,将文件格式信息存储在"fmt context"中
- err = avformat_open_input(&p_fmt_ctx, is->filename, NULL, NULL);
- if (err < 0)
- {
- //is->rvc_log("avformat_open_input() failed %d.", err);
- ret = -1;
- goto fail;
- }
- is->p_fmt_ctx = p_fmt_ctx;
- // 1.2 搜索流信息:读取一段视频文件数据,尝试解码,将取到的流信息填入p_fmt_ctx->streams
- // ic->streams是一个指针数组,数组大小是pFormatCtx->nb_streams
- err = avformat_find_stream_info(p_fmt_ctx, NULL);
- if (err < 0)
- {
- //is->rvc_log("avformat_find_stream_info() failed %d.", err);
- ret = -1;
- goto fail;
- }
- // 2. 查找第一个音频流/视频流
- a_idx = -1;
- v_idx = -1;
- for (i=0; i<(int)p_fmt_ctx->nb_streams; i++)
- {
- if ((p_fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) &&
- (a_idx == -1))
- {
- a_idx = i;
- //is->rvc_log("Find a audio stream, index %d.", a_idx);
- }
- if ((p_fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
- (v_idx == -1))
- {
- v_idx = i;
- //is->rvc_log("Find a video stream, index %d.", v_idx);
- }
- if (a_idx != -1 && v_idx != -1)
- {
- break;
- }
- }
- if (a_idx == -1 && v_idx == -1)
- {
- //is->rvc_log("Cann't find any audio/video stream.");
- ret = -1;
- fail:
- if (p_fmt_ctx != NULL)
- {
- avformat_close_input(&p_fmt_ctx);
- }
- return ret;
- }
- is->audio_idx = a_idx;
- is->video_idx = v_idx;
- is->p_audio_stream = p_fmt_ctx->streams[a_idx];
- is->p_video_stream = p_fmt_ctx->streams[v_idx];
- return 0;
- }
- int demux_deinit()
- {
- return 0;
- }
- static int stream_has_enough_packets(AVStream *st, int stream_id, packet_queue_t *queue)
- {
- return stream_id < 0 ||
- queue->abort_request ||
- (st->disposition & AV_DISPOSITION_ATTACHED_PIC) ||
- queue->nb_packets > MIN_FRAMES && (!queue->duration || av_q2d(st->time_base) * queue->duration > 1.0);
- }
- /* this thread gets the stream from the disk or the network */
- static int demux_thread(void *arg)
- {
- player_stat_t *is = (player_stat_t *)arg;
- AVFormatContext *p_fmt_ctx = is->p_fmt_ctx;
- int ret = 0;
- AVPacket pkt1, *pkt = &pkt1;
- SDL_mutex *wait_mutex = SDL_CreateMutex();
- //is->rvc_log("demux_thread running, is->abort_request == %d.", is->abort_request);
- // 4. 解复用处理
- while (0 == is->abort_request)
- {
- //is->rvc_log("demux_thread is->abort_request is %d.", is->abort_request);
- // if (is->abort_request)
- // {
- // break;
- // }
- if (0 != ret)
- {
- SDL_Event event;
- event.type = FF_QUIT_EVENT;
- event.user.data1 = is;
- SDL_PushEvent(&event);
- }
-
- /* if the queue are full, no need to read more */
- if (is->audio_pkt_queue.size + is->video_pkt_queue.size > MAX_QUEUE_SIZE ||
- (stream_has_enough_packets(is->p_audio_stream, is->audio_idx, &is->audio_pkt_queue) &&
- stream_has_enough_packets(is->p_video_stream, is->video_idx, &is->video_pkt_queue)))
- {
- /* wait 10 ms */
- SDL_LockMutex(wait_mutex);
- SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
- SDL_UnlockMutex(wait_mutex);
- continue;
- }
- // 4.1 从输入文件中读取一个packet
- ret = av_read_frame(is->p_fmt_ctx, pkt);
- if (ret < 0)
- {
- if ((ret == AVERROR_EOF))// || avio_feof(ic->pb)) && !is->eof)
- {
- // 输入文件已读完,则往packet队列中发送NULL packet,以冲洗(flush)解码器,否则解码器中缓存的帧取不出来
- if (is->video_idx >= 0)
- {
- packet_queue_put_nullpacket(&is->video_pkt_queue, is->video_idx, is->rvc_log);
- }
- if (is->audio_idx >= 0)
- {
- packet_queue_put_nullpacket(&is->audio_pkt_queue, is->audio_idx, is->rvc_log);
- }
- is->rvc_log("av_read_frame ret is AVERROR_EOF.");
- }
- SDL_LockMutex(wait_mutex);
- SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
- SDL_UnlockMutex(wait_mutex);
- continue;
- }
-
- // 4.3 根据当前packet类型(音频、视频、字幕),将其存入对应的packet队列
- if (pkt->stream_index == is->audio_idx)
- {
- packet_queue_put(&is->audio_pkt_queue, pkt, is->rvc_log);
- }
- else if (pkt->stream_index == is->video_idx)
- {
- packet_queue_put(&is->video_pkt_queue, pkt, is->rvc_log);
- }
- else
- {
- av_packet_unref(pkt);
- }
- }
- SDL_DestroyMutex(wait_mutex);
- return 0;
- }
- int open_demux(player_stat_t *is)
- {
- if (demux_init(is) != 0)
- {
- //is->rvc_log("demux_init() failed.");
- return -1;
- }
- is->read_tid = SDL_CreateThread(demux_thread, "demux_thread", is);
- if (is->read_tid == NULL)
- {
- //is->rvc_log("SDL_CreateThread() failed: %s.", SDL_GetError());
- return -1;
- }
- return 0;
- }
|