FFmpeg
venc_data_dump.c
Go to the documentation of this file.
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 
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 
23 #include "libavutil/common.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/error.h"
27 
28 #include "libavformat/avformat.h"
29 
30 #include "libavcodec/avcodec.h"
31 
32 static int decode_read(AVCodecContext *decoder, AVFrame *frame, int flush, int max_frames)
33 {
34  const int ret_done = flush ? AVERROR_EOF : AVERROR(EAGAIN);
35  int ret = 0;
36 
37  while (ret >= 0 &&
38  (max_frames == 0 || decoder->frame_number < max_frames)) {
39  AVFrameSideData *sd;
40 
42  if (ret < 0)
43  return (ret == ret_done) ? 0 : ret;
44 
45  fprintf(stdout, "frame %d\n", decoder->frame_number - 1);
46 
48  if (sd) {
50 
51  fprintf(stdout, "AVVideoEncParams %d\n", par->type);
52  fprintf(stdout, "qp %d\n", par->qp);
53  for (int i = 0; i < FF_ARRAY_ELEMS(par->delta_qp); i++)
54  for (int j = 0; j < FF_ARRAY_ELEMS(par->delta_qp[i]); j++) {
55  if (par->delta_qp[i][j])
56  fprintf(stdout, "delta_qp[%d][%d] %"PRId32"\n", i, j, par->delta_qp[i][j]);
57  }
58 
59  if (par->nb_blocks) {
60  fprintf(stdout, "nb_blocks %d\n", par->nb_blocks);
61  for (int i = 0; i < par->nb_blocks; i++) {
63 
64  fprintf(stdout, "block %d %d:%d %dx%d %"PRId32"\n",
65  i, b->src_x, b->src_y, b->w, b->h, b->delta_qp);
66  }
67  }
68  }
69 
71 
72  if (max_frames && decoder->frame_number == max_frames)
73  return 1;
74  }
75 
76  return (max_frames == 0 || decoder->frame_number < max_frames) ? 0 : 1;
77 }
78 
79 static int decoder_init(AVFormatContext *demuxer, int stream_idx,
81 {
82  const AVCodec *codec;
83  int ret;
84 
85  if (stream_idx < 0 || stream_idx >= demuxer->nb_streams)
86  return AVERROR(EINVAL);
87 
88  codec = avcodec_find_decoder(demuxer->streams[stream_idx]->codecpar->codec_id);
89  if (!codec)
91 
92  *dec = avcodec_alloc_context3(codec);
93  if (!*dec)
94  return AVERROR(ENOMEM);
95 
96  ret = avcodec_open2(*dec, NULL, opts);
97  if (ret < 0)
98  return ret;
99 
100  return 0;
101 }
102 
103 int main(int argc, char **argv)
104 {
105  AVFormatContext *demuxer = NULL;
108 
109  AVPacket *pkt = NULL;
110  AVFrame *frame = NULL;
111 
112  unsigned int stream_idx, max_frames;
113  const char *filename, *thread_type = NULL, *nb_threads = NULL;
114  int ret = 0;
115 
116  if (argc <= 3) {
117  fprintf(stderr, "Usage: %s <input file> <stream index> <max frame count> [<thread count> <thread type>]\n", argv[0]);
118  return 0;
119  }
120 
121  filename = argv[1];
122  stream_idx = strtol(argv[2], NULL, 0);
123  max_frames = strtol(argv[3], NULL, 0);
124  if (argc > 5) {
125  nb_threads = argv[4];
126  thread_type = argv[5];
127  }
128 
129  ret = av_dict_set(&opts, "threads", nb_threads, 0);
130  ret |= av_dict_set(&opts, "thread_type", thread_type, 0);
131  ret |= av_dict_set(&opts, "export_side_data", "venc_params", 0);
132 
133  ret = avformat_open_input(&demuxer, filename, NULL, NULL);
134  if (ret < 0) {
135  fprintf(stderr, "Error opening input file: %d\n", ret);
136  return ret;
137  }
138 
139  ret = decoder_init(demuxer, stream_idx, &decoder, &opts);
140  if (ret < 0) {
141  fprintf(stderr, "Error initializing decoder\n");
142  goto finish;
143  }
144 
145  pkt = av_packet_alloc();
146  frame = av_frame_alloc();
147  if (!pkt || !frame) {
148  ret = AVERROR(ENOMEM);
149  goto finish;
150  }
151 
152  while (ret >= 0) {
153  ret = av_read_frame(demuxer, pkt);
154  if (ret < 0)
155  goto flush;
156  if (pkt->stream_index != stream_idx) {
158  continue;
159  }
160 
162  if (ret < 0) {
163  fprintf(stderr, "Error decoding: %d\n", ret);
164  goto finish;
165  }
167 
168  ret = decode_read(decoder, frame, 0, max_frames);
169  if (ret < 0) {
170  fprintf(stderr, "Error decoding: %d\n", ret);
171  goto finish;
172  } else if (ret > 0) {
173  ret = 0;
174  goto finish;
175  }
176  }
177 
178 flush:
180  ret = decode_read(decoder, frame, 1, max_frames);
181  if (ret < 0) {
182  fprintf(stderr, "Error flushing: %d\n", ret);
183  goto finish;
184  }
185  ret = 0;
186 
187 finish:
188  av_dict_free(&opts);
192  avformat_close_input(&demuxer);
193 
194  return ret;
195 }
AVVideoEncParams::qp
int32_t qp
Base quantisation parameter for the frame.
Definition: video_enc_params.h:103
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
AVCodec
AVCodec.
Definition: codec.h:197
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:738
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1300
b
#define b
Definition: input.c:41
AVDictionary
Definition: dict.c:30
av_read_frame
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1741
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
avformat_close_input
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4481
decoder
static const chunk_decoder decoder[8]
Definition: dfa.c:330
finish
static void finish(void)
Definition: movenc.c:342
AVVideoEncParams::delta_qp
int32_t delta_qp[4][2]
Quantisation parameter offset from the base (per-frame) qp for a given plane (first index) and AC/DC ...
Definition: video_enc_params.h:109
AVVideoEncParams
Video encoding parameters for a given frame.
Definition: video_enc_params.h:73
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
pkt
AVPacket * pkt
Definition: movenc.c:59
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:173
avcodec_receive_frame
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder.
Definition: decode.c:652
AVVideoEncParams::type
enum AVVideoEncParamsType type
Type of the parameters (the codec they are used with).
Definition: video_enc_params.h:95
AVFormatContext
Format I/O context.
Definition: avformat.h:1232
opts
AVDictionary * opts
Definition: movenc.c:50
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
NULL
#define NULL
Definition: coverity.c:32
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:592
decoder_init
static int decoder_init(AVFormatContext *demuxer, int stream_idx, AVCodecContext **dec, AVDictionary **opts)
Definition: venc_data_dump.c:79
avcodec_free_context
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition: options.c:188
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:144
error.h
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1288
AVFrameSideData::data
uint8_t * data
Definition: frame.h:222
AVVideoEncParams::nb_blocks
unsigned int nb_blocks
Number of blocks in the array.
Definition: video_enc_params.h:81
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:203
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:589
i
int i
Definition: input.c:407
avformat_open_input
int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:512
common.h
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
AVVideoBlockParams
Data structure for storing block-level encoding information.
Definition: video_enc_params.h:120
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
avcodec_find_decoder
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:946
avformat.h
dict.h
AVCodecContext
main external API structure.
Definition: avcodec.h:536
AV_FRAME_DATA_VIDEO_ENC_PARAMS
@ AV_FRAME_DATA_VIDEO_ENC_PARAMS
Encoding parameters for a video frame, as described by AVVideoEncParams.
Definition: frame.h:186
AVPacket::stream_index
int stream_index
Definition: packet.h:371
AVERROR_DECODER_NOT_FOUND
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
Definition: error.h:52
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:220
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:346
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
av_video_enc_params_block
static av_always_inline AVVideoBlockParams * av_video_enc_params_block(AVVideoEncParams *par, unsigned int idx)
Definition: video_enc_params.h:143
decode_read
static int decode_read(AVCodecContext *decoder, AVFrame *frame, int flush, int max_frames)
Definition: venc_data_dump.c:32
main
int main(int argc, char **argv)
Definition: venc_data_dump.c:103
video_enc_params.h