api-codec-param-test.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 && avpriv_codec_get_cap_skip_frame_fill_param(codec_ctx->codec)) {
  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. /* close all codecs opened in try_decode_video_frame */
  110. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  111. AVStream *st = fmt_ctx->streams[i];
  112. avcodec_close(st->codec);
  113. }
  114. return ret < 0;
  115. }
  116. static void dump_video_streams(const AVFormatContext *fmt_ctx, int decode)
  117. {
  118. int i;
  119. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  120. const AVOption *opt = NULL;
  121. const AVStream *st = fmt_ctx->streams[i];
  122. AVCodecContext *codec_ctx = st->codec;
  123. printf("stream=%d, decode=%d\n", i, decode);
  124. while (opt = av_opt_next(codec_ctx, opt)) {
  125. uint8_t *str;
  126. if (opt->type == AV_OPT_TYPE_CONST)
  127. continue;
  128. if (!strcmp(opt->name, "frame_number"))
  129. continue;
  130. if (av_opt_get(codec_ctx, opt->name, 0, &str) >= 0) {
  131. printf(" %s=%s\n", opt->name, str);
  132. av_free(str);
  133. }
  134. }
  135. }
  136. }
  137. static int open_and_probe_video_streams(AVFormatContext **fmt_ctx, const char *filename, int decode)
  138. {
  139. int ret = 0;
  140. ret = avformat_open_input(fmt_ctx, filename, NULL, NULL);
  141. if (ret < 0) {
  142. av_log(NULL, AV_LOG_ERROR, "Failed to open input '%s'", filename);
  143. goto end;
  144. }
  145. ret = find_video_stream_info(*fmt_ctx, decode);
  146. if (ret < 0) {
  147. goto end;
  148. }
  149. dump_video_streams(*fmt_ctx, decode);
  150. end:
  151. return ret;
  152. }
  153. static int check_video_streams(const AVFormatContext *fmt_ctx1, const AVFormatContext *fmt_ctx2)
  154. {
  155. int i;
  156. int ret = 0;
  157. av_assert0(fmt_ctx1->nb_streams == fmt_ctx2->nb_streams);
  158. for (i = 0; i < fmt_ctx1->nb_streams; i++) {
  159. const AVOption *opt = NULL;
  160. const AVStream *st1 = fmt_ctx1->streams[i];
  161. const AVStream *st2 = fmt_ctx2->streams[i];
  162. AVCodecContext *codec_ctx1 = st1->codec;
  163. AVCodecContext *codec_ctx2 = st2->codec;
  164. if (codec_ctx1->codec_type != AVMEDIA_TYPE_VIDEO)
  165. continue;
  166. while (opt = av_opt_next(codec_ctx1, opt)) {
  167. uint8_t *str1 = NULL, *str2 = NULL;
  168. if (opt->type == AV_OPT_TYPE_CONST)
  169. continue;
  170. if (!strcmp(opt->name, "frame_number"))
  171. continue;
  172. av_assert0(av_opt_get(codec_ctx1, opt->name, 0, &str1) >= 0);
  173. av_assert0(av_opt_get(codec_ctx2, opt->name, 0, &str2) >= 0);
  174. if (strcmp(str1, str2)) {
  175. av_log(NULL, AV_LOG_ERROR, "Field %s differs: %s %s", opt->name, str1, str2);
  176. ret = AVERROR(EINVAL);
  177. }
  178. av_free(str1);
  179. av_free(str2);
  180. }
  181. }
  182. return ret;
  183. }
  184. int main(int argc, char* argv[])
  185. {
  186. int ret = 0;
  187. AVFormatContext *fmt_ctx = NULL;
  188. AVFormatContext *fmt_ctx_no_decode = NULL;
  189. av_register_all();
  190. if (argc < 2) {
  191. av_log(NULL, AV_LOG_ERROR, "Usage: %s <input>\n", argv[0]);
  192. return -1;
  193. }
  194. if ((ret = open_and_probe_video_streams(&fmt_ctx_no_decode, argv[1], 0)) < 0) {
  195. av_log(NULL, AV_LOG_ERROR, "Failed to probe '%s' without frame decoding\n", argv[1]);
  196. goto end;
  197. }
  198. if ((ret = open_and_probe_video_streams(&fmt_ctx, argv[1], 1)) < 0) {
  199. av_log(NULL, AV_LOG_ERROR, "Failed to probe '%s' with frame decoding\n", argv[1]);
  200. goto end;
  201. }
  202. ret = check_video_streams(fmt_ctx, fmt_ctx_no_decode);
  203. end:
  204. avformat_close_input(&fmt_ctx);
  205. avformat_close_input(&fmt_ctx_no_decode);
  206. return ret;
  207. }