demux.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "demux.h"
  2. #include "packet.h"
  3. static int decode_interrupt_cb(void *ctx)
  4. {
  5. player_stat_t *is = (player_stat_t*)ctx;
  6. return is->abort_request;
  7. }
  8. static int demux_init(player_stat_t *is)
  9. {
  10. AVFormatContext *p_fmt_ctx = NULL;
  11. int err, i, ret;
  12. int a_idx;
  13. int v_idx;
  14. p_fmt_ctx = avformat_alloc_context();
  15. if (!p_fmt_ctx)
  16. {
  17. //is->rvc_log("Could not allocate context.");
  18. ret = AVERROR(ENOMEM);
  19. goto fail;
  20. }
  21. // 中断回调机制。为底层I/O层提供一个处理接口,比如中止IO操作。
  22. p_fmt_ctx->interrupt_callback.callback = decode_interrupt_cb;
  23. p_fmt_ctx->interrupt_callback.opaque = is;
  24. // 1. 构建AVFormatContext
  25. // 1.1 打开视频文件:读取文件头,将文件格式信息存储在"fmt context"中
  26. err = avformat_open_input(&p_fmt_ctx, is->filename, NULL, NULL);
  27. if (err < 0)
  28. {
  29. //is->rvc_log("avformat_open_input() failed %d.", err);
  30. ret = -1;
  31. goto fail;
  32. }
  33. is->p_fmt_ctx = p_fmt_ctx;
  34. // 1.2 搜索流信息:读取一段视频文件数据,尝试解码,将取到的流信息填入p_fmt_ctx->streams
  35. // ic->streams是一个指针数组,数组大小是pFormatCtx->nb_streams
  36. err = avformat_find_stream_info(p_fmt_ctx, NULL);
  37. if (err < 0)
  38. {
  39. //is->rvc_log("avformat_find_stream_info() failed %d.", err);
  40. ret = -1;
  41. goto fail;
  42. }
  43. // 2. 查找第一个音频流/视频流
  44. a_idx = -1;
  45. v_idx = -1;
  46. for (i=0; i<(int)p_fmt_ctx->nb_streams; i++)
  47. {
  48. if ((p_fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) &&
  49. (a_idx == -1))
  50. {
  51. a_idx = i;
  52. //is->rvc_log("Find a audio stream, index %d.", a_idx);
  53. }
  54. if ((p_fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
  55. (v_idx == -1))
  56. {
  57. v_idx = i;
  58. //is->rvc_log("Find a video stream, index %d.", v_idx);
  59. }
  60. if (a_idx != -1 && v_idx != -1)
  61. {
  62. break;
  63. }
  64. }
  65. if (a_idx == -1 && v_idx == -1)
  66. {
  67. //is->rvc_log("Cann't find any audio/video stream.");
  68. ret = -1;
  69. fail:
  70. if (p_fmt_ctx != NULL)
  71. {
  72. avformat_close_input(&p_fmt_ctx);
  73. }
  74. return ret;
  75. }
  76. is->audio_idx = a_idx;
  77. is->video_idx = v_idx;
  78. is->p_audio_stream = p_fmt_ctx->streams[a_idx];
  79. is->p_video_stream = p_fmt_ctx->streams[v_idx];
  80. return 0;
  81. }
  82. int demux_deinit()
  83. {
  84. return 0;
  85. }
  86. static int stream_has_enough_packets(AVStream *st, int stream_id, packet_queue_t *queue)
  87. {
  88. return stream_id < 0 ||
  89. queue->abort_request ||
  90. (st->disposition & AV_DISPOSITION_ATTACHED_PIC) ||
  91. queue->nb_packets > MIN_FRAMES && (!queue->duration || av_q2d(st->time_base) * queue->duration > 1.0);
  92. }
  93. /* this thread gets the stream from the disk or the network */
  94. static int demux_thread(void *arg)
  95. {
  96. player_stat_t *is = (player_stat_t *)arg;
  97. AVFormatContext *p_fmt_ctx = is->p_fmt_ctx;
  98. int ret = 0;
  99. AVPacket pkt1, *pkt = &pkt1;
  100. SDL_mutex *wait_mutex = SDL_CreateMutex();
  101. //is->rvc_log("demux_thread running, is->abort_request == %d.", is->abort_request);
  102. // 4. 解复用处理
  103. while (0 == is->abort_request)
  104. {
  105. //is->rvc_log("demux_thread is->abort_request is %d.", is->abort_request);
  106. // if (is->abort_request)
  107. // {
  108. // break;
  109. // }
  110. if (0 != ret)
  111. {
  112. SDL_Event event;
  113. event.type = FF_QUIT_EVENT;
  114. event.user.data1 = is;
  115. SDL_PushEvent(&event);
  116. }
  117. /* if the queue are full, no need to read more */
  118. if (is->audio_pkt_queue.size + is->video_pkt_queue.size > MAX_QUEUE_SIZE ||
  119. (stream_has_enough_packets(is->p_audio_stream, is->audio_idx, &is->audio_pkt_queue) &&
  120. stream_has_enough_packets(is->p_video_stream, is->video_idx, &is->video_pkt_queue)))
  121. {
  122. /* wait 10 ms */
  123. SDL_LockMutex(wait_mutex);
  124. SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
  125. SDL_UnlockMutex(wait_mutex);
  126. continue;
  127. }
  128. // 4.1 从输入文件中读取一个packet
  129. ret = av_read_frame(is->p_fmt_ctx, pkt);
  130. if (ret < 0)
  131. {
  132. if ((ret == AVERROR_EOF))// || avio_feof(ic->pb)) && !is->eof)
  133. {
  134. // 输入文件已读完,则往packet队列中发送NULL packet,以冲洗(flush)解码器,否则解码器中缓存的帧取不出来
  135. if (is->video_idx >= 0)
  136. {
  137. packet_queue_put_nullpacket(&is->video_pkt_queue, is->video_idx, is->rvc_log);
  138. }
  139. if (is->audio_idx >= 0)
  140. {
  141. packet_queue_put_nullpacket(&is->audio_pkt_queue, is->audio_idx, is->rvc_log);
  142. }
  143. is->rvc_log("av_read_frame ret is AVERROR_EOF.");
  144. }
  145. SDL_LockMutex(wait_mutex);
  146. SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
  147. SDL_UnlockMutex(wait_mutex);
  148. continue;
  149. }
  150. // 4.3 根据当前packet类型(音频、视频、字幕),将其存入对应的packet队列
  151. if (pkt->stream_index == is->audio_idx)
  152. {
  153. packet_queue_put(&is->audio_pkt_queue, pkt, is->rvc_log);
  154. }
  155. else if (pkt->stream_index == is->video_idx)
  156. {
  157. packet_queue_put(&is->video_pkt_queue, pkt, is->rvc_log);
  158. }
  159. else
  160. {
  161. av_packet_unref(pkt);
  162. }
  163. }
  164. SDL_DestroyMutex(wait_mutex);
  165. return 0;
  166. }
  167. int open_demux(player_stat_t *is)
  168. {
  169. if (demux_init(is) != 0)
  170. {
  171. //is->rvc_log("demux_init() failed.");
  172. return -1;
  173. }
  174. is->read_tid = SDL_CreateThread(demux_thread, "demux_thread", is);
  175. if (is->read_tid == NULL)
  176. {
  177. //is->rvc_log("SDL_CreateThread() failed: %s.", SDL_GetError());
  178. return -1;
  179. }
  180. return 0;
  181. }