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.

196 lines
5.7KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdio.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include "libavutil/common.h"
  22. #include "libavutil/dict.h"
  23. #include "libavutil/error.h"
  24. #include "libavutil/video_enc_params.h"
  25. #include "libavformat/avformat.h"
  26. #include "libavcodec/avcodec.h"
  27. static int decode_read(AVCodecContext *decoder, AVFrame *frame, int flush, int max_frames)
  28. {
  29. const int ret_done = flush ? AVERROR_EOF : AVERROR(EAGAIN);
  30. int ret = 0;
  31. while (ret >= 0 &&
  32. (max_frames == 0 || decoder->frame_number < max_frames)) {
  33. AVFrameSideData *sd;
  34. ret = avcodec_receive_frame(decoder, frame);
  35. if (ret < 0)
  36. return (ret == ret_done) ? 0 : ret;
  37. fprintf(stdout, "frame %d\n", decoder->frame_number - 1);
  38. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIDEO_ENC_PARAMS);
  39. if (sd) {
  40. AVVideoEncParams *par = (AVVideoEncParams*)sd->data;
  41. fprintf(stdout, "AVVideoEncParams %d\n", par->type);
  42. fprintf(stdout, "qp %d\n", par->qp);
  43. for (int i = 0; i < FF_ARRAY_ELEMS(par->delta_qp); i++)
  44. for (int j = 0; j < FF_ARRAY_ELEMS(par->delta_qp[i]); j++) {
  45. if (par->delta_qp[i][j])
  46. fprintf(stdout, "delta_qp[%d][%d] %"PRId32"\n", i, j, par->delta_qp[i][j]);
  47. }
  48. if (par->nb_blocks) {
  49. fprintf(stdout, "nb_blocks %d\n", par->nb_blocks);
  50. for (int i = 0; i < par->nb_blocks; i++) {
  51. AVVideoBlockParams *b = av_video_enc_params_block(par, i);
  52. fprintf(stdout, "block %d %d:%d %dx%d %"PRId32"\n",
  53. i, b->src_x, b->src_y, b->w, b->h, b->delta_qp);
  54. }
  55. }
  56. }
  57. av_frame_unref(frame);
  58. if (max_frames && decoder->frame_number == max_frames)
  59. return 1;
  60. }
  61. return (max_frames == 0 || decoder->frame_number < max_frames) ? 0 : 1;
  62. }
  63. static int decoder_init(AVFormatContext *demuxer, int stream_idx,
  64. AVCodecContext **dec, AVDictionary **opts)
  65. {
  66. const AVCodec *codec;
  67. int ret;
  68. if (stream_idx < 0 || stream_idx >= demuxer->nb_streams)
  69. return AVERROR(EINVAL);
  70. codec = avcodec_find_decoder(demuxer->streams[stream_idx]->codecpar->codec_id);
  71. if (!codec)
  72. return AVERROR_DECODER_NOT_FOUND;
  73. *dec = avcodec_alloc_context3(codec);
  74. if (!*dec)
  75. return AVERROR(ENOMEM);
  76. ret = avcodec_open2(*dec, NULL, opts);
  77. if (ret < 0)
  78. return ret;
  79. return 0;
  80. }
  81. int main(int argc, char **argv)
  82. {
  83. AVFormatContext *demuxer = NULL;
  84. AVCodecContext *decoder = NULL;
  85. AVDictionary *opts = NULL;
  86. AVPacket *pkt = NULL;
  87. AVFrame *frame = NULL;
  88. unsigned int stream_idx, max_frames;
  89. const char *filename, *thread_type = NULL, *nb_threads = NULL;
  90. int ret = 0;
  91. if (argc <= 3) {
  92. fprintf(stderr, "Usage: %s <input file> <stream index> <max frame count> [<thread count> <thread type>]\n", argv[0]);
  93. return 0;
  94. }
  95. filename = argv[1];
  96. stream_idx = strtol(argv[2], NULL, 0);
  97. max_frames = strtol(argv[3], NULL, 0);
  98. if (argc > 5) {
  99. nb_threads = argv[4];
  100. thread_type = argv[5];
  101. }
  102. ret = av_dict_set(&opts, "threads", nb_threads, 0);
  103. ret |= av_dict_set(&opts, "thread_type", thread_type, 0);
  104. ret |= av_dict_set(&opts, "export_side_data", "venc_params", 0);
  105. ret = avformat_open_input(&demuxer, filename, NULL, NULL);
  106. if (ret < 0) {
  107. fprintf(stderr, "Error opening input file: %d\n", ret);
  108. return ret;
  109. }
  110. ret = decoder_init(demuxer, stream_idx, &decoder, &opts);
  111. if (ret < 0) {
  112. fprintf(stderr, "Error initializing decoder\n");
  113. goto finish;
  114. }
  115. pkt = av_packet_alloc();
  116. frame = av_frame_alloc();
  117. if (!pkt || !frame) {
  118. ret = AVERROR(ENOMEM);
  119. goto finish;
  120. }
  121. while (ret >= 0) {
  122. ret = av_read_frame(demuxer, pkt);
  123. if (ret < 0)
  124. goto flush;
  125. if (pkt->stream_index != stream_idx) {
  126. av_packet_unref(pkt);
  127. continue;
  128. }
  129. ret = avcodec_send_packet(decoder, pkt);
  130. if (ret < 0) {
  131. fprintf(stderr, "Error decoding: %d\n", ret);
  132. goto finish;
  133. }
  134. av_packet_unref(pkt);
  135. ret = decode_read(decoder, frame, 0, max_frames);
  136. if (ret < 0) {
  137. fprintf(stderr, "Error decoding: %d\n", ret);
  138. goto finish;
  139. } else if (ret > 0) {
  140. ret = 0;
  141. goto finish;
  142. }
  143. }
  144. flush:
  145. avcodec_send_packet(decoder, NULL);
  146. ret = decode_read(decoder, frame, 1, max_frames);
  147. if (ret < 0) {
  148. fprintf(stderr, "Error flushing: %d\n", ret);
  149. goto finish;
  150. }
  151. ret = 0;
  152. finish:
  153. av_dict_free(&opts);
  154. av_packet_free(&pkt);
  155. av_frame_free(&frame);
  156. avcodec_free_context(&decoder);
  157. avformat_close_input(&demuxer);
  158. return ret;
  159. }