You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.9KB

  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 video data.
  28. */
  29. #include <libavutil/imgutils.h>
  30. #include <libavutil/timestamp.h>
  31. #include <libavformat/avformat.h>
  32. static AVFormatContext *fmt_ctx = NULL;
  33. static AVCodecContext *dec_ctx = NULL;
  34. static AVCodec *dec = NULL;
  35. static AVStream *stream = NULL;
  36. static const char *src_filename = NULL;
  37. static const char *dst_filename = NULL;
  38. static FILE *dst_file = NULL;
  39. static uint8_t *dst_data[4] = {NULL};
  40. static int dst_linesize[4];
  41. static int dst_bufsize;
  42. static int stream_idx;
  43. static AVFrame *frame = NULL;
  44. static AVPacket pkt;
  45. static int frame_count = 0;
  46. static int decode_packet(int *got_frame, int cached)
  47. {
  48. int ret;
  49. if (pkt.stream_index != stream_idx)
  50. return 0;
  51. /* decode video frame */
  52. ret = avcodec_decode_video2(dec_ctx, frame, got_frame, &pkt);
  53. if (ret < 0) {
  54. fprintf(stderr, "Error decoding video frame\n");
  55. return ret;
  56. }
  57. if (*got_frame) {
  58. printf("frame%s n:%d coded_n:%d pts:%s\n",
  59. cached ? "(cached)" : "",
  60. frame_count++, frame->coded_picture_number,
  61. av_ts2timestr(frame->pts, &dec_ctx->time_base));
  62. /* copy decoded frame to destination buffer:
  63. * this is required since rawvideo expect non aligned data */
  64. av_image_copy(dst_data, dst_linesize,
  65. (const uint8_t **)(frame->data), frame->linesize,
  66. dec_ctx->pix_fmt, dec_ctx->width, dec_ctx->height);
  67. /* write to rawvideo file */
  68. fwrite(dst_data[0], 1, dst_bufsize, dst_file);
  69. }
  70. return ret;
  71. }
  72. int main (int argc, char **argv)
  73. {
  74. int ret, got_frame;
  75. if (argc != 3) {
  76. fprintf(stderr, "usage: %s input_file output_file\n"
  77. "API example program to show how to read frames from an input file.\n"
  78. "This program reads frames from a file, decode them, and write them "
  79. "to a rawvideo file named like output_file."
  80. "\n", argv[0]);
  81. exit(1);
  82. }
  83. src_filename = argv[1];
  84. dst_filename = argv[2];
  85. /* register all formats and codecs */
  86. av_register_all();
  87. /* open input file, and allocated format context */
  88. if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
  89. fprintf(stderr, "Could not open source file %s\n", src_filename);
  90. exit(1);
  91. }
  92. /* retrieve stream information */
  93. if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
  94. fprintf(stderr, "Could not find stream information\n");
  95. exit(1);
  96. }
  97. ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  98. if (ret < 0) {
  99. fprintf(stderr, "Could not find video stream in file\n");
  100. goto end;
  101. }
  102. stream_idx = ret;
  103. stream = fmt_ctx->streams[stream_idx];
  104. /* find decoder for the stream */
  105. dec_ctx = stream->codec;
  106. dec = avcodec_find_decoder(dec_ctx->codec_id);
  107. if (!dec) {
  108. fprintf(stderr, "Failed to find any codec\n");
  109. ret = 1;
  110. goto end;
  111. }
  112. if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
  113. fprintf(stderr, "Failed to open codec\n");
  114. goto end;
  115. }
  116. /* dump input information to stderr */
  117. av_dump_format(fmt_ctx, 0, src_filename, 0);
  118. dst_file = fopen(dst_filename, "wb");
  119. if (!dst_file) {
  120. fprintf(stderr, "Could not open destination file %s\n", dst_filename);
  121. ret = 1;
  122. goto end;
  123. }
  124. frame = avcodec_alloc_frame();
  125. if (!frame) {
  126. fprintf(stderr, "Could not allocate video frame\n");
  127. ret = 1;
  128. goto end;
  129. }
  130. /* allocate image where the decoded image will be put */
  131. ret = av_image_alloc(dst_data, dst_linesize,
  132. dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt, 1);
  133. if (ret < 0) {
  134. fprintf(stderr, "Could not alloc raw video buffer\n");
  135. goto end;
  136. }
  137. dst_bufsize = ret;
  138. /* initialize packet, set data to NULL, let the demuxer fill it */
  139. av_init_packet(&pkt);
  140. pkt.size = 0;
  141. pkt.data = NULL;
  142. printf("Demuxing file '%s' to '%s'\n", src_filename, dst_filename);
  143. /* read frames from the file */
  144. while (av_read_frame(fmt_ctx, &pkt) >= 0)
  145. decode_packet(&got_frame, 0);
  146. /* flush cached frames */
  147. pkt.data = NULL;
  148. pkt.size = 0;
  149. do {
  150. decode_packet(&got_frame, 1);
  151. } while (got_frame);
  152. printf("Demuxing succeeded. Play the output file with the command:\n"
  153. "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
  154. av_get_pix_fmt_name(dec_ctx->pix_fmt), dec_ctx->width, dec_ctx->height,
  155. dst_filename);
  156. end:
  157. avcodec_close(dec_ctx);
  158. avformat_close_input(&fmt_ctx);
  159. if (dst_file)
  160. fclose(dst_file);
  161. av_free(frame);
  162. av_free(dst_data[0]);
  163. return ret < 0;
  164. }