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.

253 lines
7.2KB

  1. /*
  2. * Copyright (c) 2015 Matthieu Bouron <matthieu.bouron stupeflix.com>
  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. #include <stdio.h>
  23. #include "libavformat/avformat.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "libavcodec/internal.h"
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/opt.h"
  28. static int try_decode_video_frame(AVCodecContext *codec_ctx, AVPacket *pkt, int decode)
  29. {
  30. int ret = 0;
  31. int got_frame = 0;
  32. AVFrame *frame = NULL;
  33. int skip_frame = codec_ctx->skip_frame;
  34. if (!avcodec_is_open(codec_ctx)) {
  35. const AVCodec *codec = avcodec_find_decoder(codec_ctx->codec_id);
  36. ret = avcodec_open2(codec_ctx, codec, NULL);
  37. if (ret < 0) {
  38. av_log(codec_ctx, AV_LOG_ERROR, "Failed to open codec\n");
  39. goto end;
  40. }
  41. }
  42. frame = av_frame_alloc();
  43. if (!frame) {
  44. av_log(NULL, AV_LOG_ERROR, "Failed to allocate frame\n");
  45. goto end;
  46. }
  47. if (!decode && codec_ctx->codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM) {
  48. codec_ctx->skip_frame = AVDISCARD_ALL;
  49. }
  50. do {
  51. ret = avcodec_decode_video2(codec_ctx, frame, &got_frame, pkt);
  52. av_assert0(decode || (!decode && !got_frame));
  53. if (ret < 0)
  54. break;
  55. pkt->data += ret;
  56. pkt->size -= ret;
  57. if (got_frame) {
  58. break;
  59. }
  60. } while (pkt->size > 0);
  61. end:
  62. codec_ctx->skip_frame = skip_frame;
  63. av_frame_free(&frame);
  64. return ret;
  65. }
  66. static int find_video_stream_info(AVFormatContext *fmt_ctx, int decode)
  67. {
  68. int ret = 0;
  69. int i, done = 0;
  70. AVPacket pkt;
  71. av_init_packet(&pkt);
  72. while (!done) {
  73. AVCodecContext *codec_ctx = NULL;
  74. AVStream *st;
  75. if ((ret = av_read_frame(fmt_ctx, &pkt)) < 0) {
  76. av_log(fmt_ctx, AV_LOG_ERROR, "Failed to read frame\n");
  77. goto end;
  78. }
  79. st = fmt_ctx->streams[pkt.stream_index];
  80. codec_ctx = st->codec;
  81. /* Writing to AVStream.codec_info_nb_frames must not be done by
  82. * user applications. It is done here for testing purposing as
  83. * find_video_stream_info tries to mimic avformat_find_stream_info
  84. * which writes to this field.
  85. * */
  86. if (codec_ctx->codec_type != AVMEDIA_TYPE_VIDEO ||
  87. st->codec_info_nb_frames++ > 0) {
  88. av_packet_unref(&pkt);
  89. continue;
  90. }
  91. ret = try_decode_video_frame(codec_ctx, &pkt, decode);
  92. if (ret < 0) {
  93. av_log(fmt_ctx, AV_LOG_ERROR, "Failed to decode video frame\n");
  94. goto end;
  95. }
  96. av_packet_unref(&pkt);
  97. /* check if all video streams have demuxed a packet */
  98. done = 1;
  99. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  100. st = fmt_ctx->streams[i];
  101. codec_ctx = st->codec;
  102. if (codec_ctx->codec_type != AVMEDIA_TYPE_VIDEO)
  103. continue;
  104. done &= st->codec_info_nb_frames > 0;
  105. }
  106. }
  107. end:
  108. av_packet_unref(&pkt);
  109. return ret < 0;
  110. }
  111. static void dump_video_streams(const AVFormatContext *fmt_ctx, int decode)
  112. {
  113. int i;
  114. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  115. const AVOption *opt = NULL;
  116. const AVStream *st = fmt_ctx->streams[i];
  117. AVCodecContext *codec_ctx = st->codec;
  118. printf("stream=%d, decode=%d\n", i, decode);
  119. while (opt = av_opt_next(codec_ctx, opt)) {
  120. uint8_t *str;
  121. if (opt->type == AV_OPT_TYPE_CONST)
  122. continue;
  123. if (!strcmp(opt->name, "frame_number"))
  124. continue;
  125. if (av_opt_get(codec_ctx, opt->name, 0, &str) >= 0) {
  126. printf(" %s=%s\n", opt->name, str);
  127. av_free(str);
  128. }
  129. }
  130. }
  131. }
  132. static int open_and_probe_video_streams(AVFormatContext **fmt_ctx, const char *filename, int decode)
  133. {
  134. int ret = 0;
  135. ret = avformat_open_input(fmt_ctx, filename, NULL, NULL);
  136. if (ret < 0) {
  137. av_log(NULL, AV_LOG_ERROR, "Failed to open input '%s'", filename);
  138. goto end;
  139. }
  140. ret = find_video_stream_info(*fmt_ctx, decode);
  141. if (ret < 0) {
  142. goto end;
  143. }
  144. dump_video_streams(*fmt_ctx, decode);
  145. end:
  146. return ret;
  147. }
  148. static int check_video_streams(const AVFormatContext *fmt_ctx1, const AVFormatContext *fmt_ctx2)
  149. {
  150. int i;
  151. int ret = 0;
  152. av_assert0(fmt_ctx1->nb_streams == fmt_ctx2->nb_streams);
  153. for (i = 0; i < fmt_ctx1->nb_streams; i++) {
  154. const AVOption *opt = NULL;
  155. const AVStream *st1 = fmt_ctx1->streams[i];
  156. const AVStream *st2 = fmt_ctx2->streams[i];
  157. AVCodecContext *codec_ctx1 = st1->codec;
  158. AVCodecContext *codec_ctx2 = st2->codec;
  159. if (codec_ctx1->codec_type != AVMEDIA_TYPE_VIDEO)
  160. continue;
  161. while (opt = av_opt_next(codec_ctx1, opt)) {
  162. uint8_t *str1 = NULL, *str2 = NULL;
  163. if (opt->type == AV_OPT_TYPE_CONST)
  164. continue;
  165. if (!strcmp(opt->name, "frame_number"))
  166. continue;
  167. av_assert0(av_opt_get(codec_ctx1, opt->name, 0, &str1) >= 0);
  168. av_assert0(av_opt_get(codec_ctx2, opt->name, 0, &str2) >= 0);
  169. if (strcmp(str1, str2)) {
  170. av_log(NULL, AV_LOG_ERROR, "Field %s differs: %s %s", opt->name, str1, str2);
  171. ret = AVERROR(EINVAL);
  172. }
  173. av_free(str1);
  174. av_free(str2);
  175. }
  176. }
  177. return ret;
  178. }
  179. int main(int argc, char* argv[])
  180. {
  181. int ret = 0;
  182. AVFormatContext *fmt_ctx = NULL;
  183. AVFormatContext *fmt_ctx_no_decode = NULL;
  184. av_register_all();
  185. if (argc < 2) {
  186. av_log(NULL, AV_LOG_ERROR, "Usage: %s <input>\n", argv[0]);
  187. return -1;
  188. }
  189. if ((ret = open_and_probe_video_streams(&fmt_ctx_no_decode, argv[1], 0)) < 0) {
  190. av_log(NULL, AV_LOG_ERROR, "Failed to probe '%s' without frame decoding\n", argv[1]);
  191. goto end;
  192. }
  193. if ((ret = open_and_probe_video_streams(&fmt_ctx, argv[1], 1)) < 0) {
  194. av_log(NULL, AV_LOG_ERROR, "Failed to probe '%s' with frame decoding\n", argv[1]);
  195. goto end;
  196. }
  197. ret = check_video_streams(fmt_ctx, fmt_ctx_no_decode);
  198. end:
  199. avformat_close_input(&fmt_ctx);
  200. avformat_close_input(&fmt_ctx_no_decode);
  201. return ret;
  202. }