demuxing.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (c) 2012 Stefano Sabatini
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file
  24. * libavformat demuxing API use example.
  25. *
  26. * Show how to use the libavformat and libavcodec API to demux and
  27. * decode audio and video data.
  28. * @example doc/examples/demuxing.c
  29. */
  30. #include <libavutil/imgutils.h>
  31. #include <libavutil/samplefmt.h>
  32. #include <libavutil/timestamp.h>
  33. #include <libavformat/avformat.h>
  34. static AVFormatContext *fmt_ctx = NULL;
  35. static AVCodecContext *video_dec_ctx = NULL, *audio_dec_ctx;
  36. static AVStream *video_stream = NULL, *audio_stream = NULL;
  37. static const char *src_filename = NULL;
  38. static const char *video_dst_filename = NULL;
  39. static const char *audio_dst_filename = NULL;
  40. static FILE *video_dst_file = NULL;
  41. static FILE *audio_dst_file = NULL;
  42. static uint8_t *video_dst_data[4] = {NULL};
  43. static int video_dst_linesize[4];
  44. static int video_dst_bufsize;
  45. static uint8_t **audio_dst_data = NULL;
  46. static int audio_dst_linesize;
  47. static int audio_dst_bufsize;
  48. static int video_stream_idx = -1, audio_stream_idx = -1;
  49. static AVFrame *frame = NULL;
  50. static AVPacket pkt;
  51. static int video_frame_count = 0;
  52. static int audio_frame_count = 0;
  53. static int decode_packet(int *got_frame, int cached)
  54. {
  55. int ret = 0;
  56. if (pkt.stream_index == video_stream_idx) {
  57. /* decode video frame */
  58. ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
  59. if (ret < 0) {
  60. fprintf(stderr, "Error decoding video frame\n");
  61. return ret;
  62. }
  63. if (*got_frame) {
  64. printf("video_frame%s n:%d coded_n:%d pts:%s\n",
  65. cached ? "(cached)" : "",
  66. video_frame_count++, frame->coded_picture_number,
  67. av_ts2timestr(frame->pts, &video_dec_ctx->time_base));
  68. /* copy decoded frame to destination buffer:
  69. * this is required since rawvideo expects non aligned data */
  70. av_image_copy(video_dst_data, video_dst_linesize,
  71. (const uint8_t **)(frame->data), frame->linesize,
  72. video_dec_ctx->pix_fmt, video_dec_ctx->width, video_dec_ctx->height);
  73. /* write to rawvideo file */
  74. fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file);
  75. }
  76. } else if (pkt.stream_index == audio_stream_idx) {
  77. /* decode audio frame */
  78. ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &pkt);
  79. if (ret < 0) {
  80. fprintf(stderr, "Error decoding audio frame\n");
  81. return ret;
  82. }
  83. if (*got_frame) {
  84. printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",
  85. cached ? "(cached)" : "",
  86. audio_frame_count++, frame->nb_samples,
  87. av_ts2timestr(frame->pts, &audio_dec_ctx->time_base));
  88. ret = av_samples_alloc(audio_dst_data, &audio_dst_linesize, av_frame_get_channels(frame),
  89. frame->nb_samples, frame->format, 1);
  90. if (ret < 0) {
  91. fprintf(stderr, "Could not allocate audio buffer\n");
  92. return AVERROR(ENOMEM);
  93. }
  94. /* TODO: extend return code of the av_samples_* functions so that this call is not needed */
  95. audio_dst_bufsize =
  96. av_samples_get_buffer_size(NULL, av_frame_get_channels(frame),
  97. frame->nb_samples, frame->format, 1);
  98. /* copy audio data to destination buffer:
  99. * this is required since rawaudio expects non aligned data */
  100. av_samples_copy(audio_dst_data, frame->data, 0, 0,
  101. frame->nb_samples, av_frame_get_channels(frame), frame->format);
  102. /* write to rawaudio file */
  103. fwrite(audio_dst_data[0], 1, audio_dst_bufsize, audio_dst_file);
  104. av_freep(&audio_dst_data[0]);
  105. }
  106. }
  107. return ret;
  108. }
  109. static int open_codec_context(int *stream_idx,
  110. AVFormatContext *fmt_ctx, enum AVMediaType type)
  111. {
  112. int ret;
  113. AVStream *st;
  114. AVCodecContext *dec_ctx = NULL;
  115. AVCodec *dec = NULL;
  116. ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
  117. if (ret < 0) {
  118. fprintf(stderr, "Could not find %s stream in input file '%s'\n",
  119. av_get_media_type_string(type), src_filename);
  120. return ret;
  121. } else {
  122. *stream_idx = ret;
  123. st = fmt_ctx->streams[*stream_idx];
  124. /* find decoder for the stream */
  125. dec_ctx = st->codec;
  126. dec = avcodec_find_decoder(dec_ctx->codec_id);
  127. if (!dec) {
  128. fprintf(stderr, "Failed to find %s codec\n",
  129. av_get_media_type_string(type));
  130. return ret;
  131. }
  132. if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
  133. fprintf(stderr, "Failed to open %s codec\n",
  134. av_get_media_type_string(type));
  135. return ret;
  136. }
  137. }
  138. return 0;
  139. }
  140. static int get_format_from_sample_fmt(const char **fmt,
  141. enum AVSampleFormat sample_fmt)
  142. {
  143. int i;
  144. struct sample_fmt_entry {
  145. enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
  146. } sample_fmt_entries[] = {
  147. { AV_SAMPLE_FMT_U8, "u8", "u8" },
  148. { AV_SAMPLE_FMT_S16, "s16be", "s16le" },
  149. { AV_SAMPLE_FMT_S32, "s32be", "s32le" },
  150. { AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
  151. { AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
  152. };
  153. *fmt = NULL;
  154. for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
  155. struct sample_fmt_entry *entry = &sample_fmt_entries[i];
  156. if (sample_fmt == entry->sample_fmt) {
  157. *fmt = AV_NE(entry->fmt_be, entry->fmt_le);
  158. return 0;
  159. }
  160. }
  161. fprintf(stderr,
  162. "sample format %s is not supported as output format\n",
  163. av_get_sample_fmt_name(sample_fmt));
  164. return -1;
  165. }
  166. int main (int argc, char **argv)
  167. {
  168. int ret = 0, got_frame;
  169. if (argc != 4) {
  170. fprintf(stderr, "usage: %s input_file video_output_file audio_output_file\n"
  171. "API example program to show how to read frames from an input file.\n"
  172. "This program reads frames from a file, decodes them, and writes decoded\n"
  173. "video frames to a rawvideo file named video_output_file, and decoded\n"
  174. "audio frames to a rawaudio file named audio_output_file.\n"
  175. "\n", argv[0]);
  176. exit(1);
  177. }
  178. src_filename = argv[1];
  179. video_dst_filename = argv[2];
  180. audio_dst_filename = argv[3];
  181. /* register all formats and codecs */
  182. av_register_all();
  183. /* open input file, and allocate format context */
  184. if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
  185. fprintf(stderr, "Could not open source file %s\n", src_filename);
  186. exit(1);
  187. }
  188. /* retrieve stream information */
  189. if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
  190. fprintf(stderr, "Could not find stream information\n");
  191. exit(1);
  192. }
  193. if (open_codec_context(&video_stream_idx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) {
  194. video_stream = fmt_ctx->streams[video_stream_idx];
  195. video_dec_ctx = video_stream->codec;
  196. video_dst_file = fopen(video_dst_filename, "wb");
  197. if (!video_dst_file) {
  198. fprintf(stderr, "Could not open destination file %s\n", video_dst_filename);
  199. ret = 1;
  200. goto end;
  201. }
  202. /* allocate image where the decoded image will be put */
  203. ret = av_image_alloc(video_dst_data, video_dst_linesize,
  204. video_dec_ctx->width, video_dec_ctx->height,
  205. video_dec_ctx->pix_fmt, 1);
  206. if (ret < 0) {
  207. fprintf(stderr, "Could not allocate raw video buffer\n");
  208. goto end;
  209. }
  210. video_dst_bufsize = ret;
  211. }
  212. if (open_codec_context(&audio_stream_idx, fmt_ctx, AVMEDIA_TYPE_AUDIO) >= 0) {
  213. int nb_planes;
  214. audio_stream = fmt_ctx->streams[audio_stream_idx];
  215. audio_dec_ctx = audio_stream->codec;
  216. audio_dst_file = fopen(audio_dst_filename, "wb");
  217. if (!audio_dst_file) {
  218. fprintf(stderr, "Could not open destination file %s\n", video_dst_filename);
  219. ret = 1;
  220. goto end;
  221. }
  222. nb_planes = av_sample_fmt_is_planar(audio_dec_ctx->sample_fmt) ?
  223. audio_dec_ctx->channels : 1;
  224. audio_dst_data = av_mallocz(sizeof(uint8_t *) * nb_planes);
  225. if (!audio_dst_data) {
  226. fprintf(stderr, "Could not allocate audio data buffers\n");
  227. ret = AVERROR(ENOMEM);
  228. goto end;
  229. }
  230. }
  231. /* dump input information to stderr */
  232. av_dump_format(fmt_ctx, 0, src_filename, 0);
  233. if (!audio_stream && !video_stream) {
  234. fprintf(stderr, "Could not find audio or video stream in the input, aborting\n");
  235. ret = 1;
  236. goto end;
  237. }
  238. frame = avcodec_alloc_frame();
  239. if (!frame) {
  240. fprintf(stderr, "Could not allocate frame\n");
  241. ret = AVERROR(ENOMEM);
  242. goto end;
  243. }
  244. /* initialize packet, set data to NULL, let the demuxer fill it */
  245. av_init_packet(&pkt);
  246. pkt.data = NULL;
  247. pkt.size = 0;
  248. if (video_stream)
  249. printf("Demuxing video from file '%s' into '%s'\n", src_filename, video_dst_filename);
  250. if (audio_stream)
  251. printf("Demuxing audio from file '%s' into '%s'\n", src_filename, audio_dst_filename);
  252. /* read frames from the file */
  253. while (av_read_frame(fmt_ctx, &pkt) >= 0) {
  254. decode_packet(&got_frame, 0);
  255. av_free_packet(&pkt);
  256. }
  257. /* flush cached frames */
  258. pkt.data = NULL;
  259. pkt.size = 0;
  260. do {
  261. decode_packet(&got_frame, 1);
  262. } while (got_frame);
  263. printf("Demuxing succeeded.\n");
  264. if (video_stream) {
  265. printf("Play the output video file with the command:\n"
  266. "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
  267. av_get_pix_fmt_name(video_dec_ctx->pix_fmt), video_dec_ctx->width, video_dec_ctx->height,
  268. video_dst_filename);
  269. }
  270. if (audio_stream) {
  271. const char *fmt;
  272. if ((ret = get_format_from_sample_fmt(&fmt, audio_dec_ctx->sample_fmt)) < 0)
  273. goto end;
  274. printf("Play the output audio file with the command:\n"
  275. "ffplay -f %s -ac %d -ar %d %s\n",
  276. fmt, audio_dec_ctx->channels, audio_dec_ctx->sample_rate,
  277. audio_dst_filename);
  278. }
  279. end:
  280. if (video_dec_ctx)
  281. avcodec_close(video_dec_ctx);
  282. if (audio_dec_ctx)
  283. avcodec_close(audio_dec_ctx);
  284. avformat_close_input(&fmt_ctx);
  285. if (video_dst_file)
  286. fclose(video_dst_file);
  287. if (audio_dst_file)
  288. fclose(audio_dst_file);
  289. av_free(frame);
  290. av_free(video_dst_data[0]);
  291. av_free(audio_dst_data);
  292. return ret < 0;
  293. }