FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffprobe.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 1408 1972 71.4%
Branches: 784 1237 63.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2007-2010 Stefano Sabatini
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * simple media prober based on the FFmpeg libraries
24 */
25
26 #include "config.h"
27 #include "libavutil/ffversion.h"
28
29 #include <string.h>
30
31 #include "libavformat/avformat.h"
32 #include "libavcodec/avcodec.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/bprint.h"
36 #include "libavutil/channel_layout.h"
37 #include "libavutil/display.h"
38 #include "libavutil/hash.h"
39 #include "libavutil/hdr_dynamic_metadata.h"
40 #include "libavutil/mastering_display_metadata.h"
41 #include "libavutil/dovi_meta.h"
42 #include "libavutil/opt.h"
43 #include "libavutil/pixdesc.h"
44 #include "libavutil/spherical.h"
45 #include "libavutil/stereo3d.h"
46 #include "libavutil/dict.h"
47 #include "libavutil/intreadwrite.h"
48 #include "libavutil/libm.h"
49 #include "libavutil/parseutils.h"
50 #include "libavutil/timecode.h"
51 #include "libavutil/timestamp.h"
52 #include "libavdevice/avdevice.h"
53 #include "libswscale/swscale.h"
54 #include "libswresample/swresample.h"
55 #include "libpostproc/postprocess.h"
56 #include "cmdutils.h"
57
58 #include "libavutil/thread.h"
59
60 #if !HAVE_THREADS
61 # ifdef pthread_mutex_lock
62 # undef pthread_mutex_lock
63 # endif
64 # define pthread_mutex_lock(a) do{}while(0)
65 # ifdef pthread_mutex_unlock
66 # undef pthread_mutex_unlock
67 # endif
68 # define pthread_mutex_unlock(a) do{}while(0)
69 #endif
70
71 typedef struct InputStream {
72 AVStream *st;
73
74 AVCodecContext *dec_ctx;
75 } InputStream;
76
77 typedef struct InputFile {
78 AVFormatContext *fmt_ctx;
79
80 InputStream *streams;
81 int nb_streams;
82 } InputFile;
83
84 const char program_name[] = "ffprobe";
85 const int program_birth_year = 2007;
86
87 static int do_bitexact = 0;
88 static int do_count_frames = 0;
89 static int do_count_packets = 0;
90 static int do_read_frames = 0;
91 static int do_read_packets = 0;
92 static int do_show_chapters = 0;
93 static int do_show_error = 0;
94 static int do_show_format = 0;
95 static int do_show_frames = 0;
96 static int do_show_packets = 0;
97 static int do_show_programs = 0;
98 static int do_show_streams = 0;
99 static int do_show_stream_disposition = 0;
100 static int do_show_data = 0;
101 static int do_show_program_version = 0;
102 static int do_show_library_versions = 0;
103 static int do_show_pixel_formats = 0;
104 static int do_show_pixel_format_flags = 0;
105 static int do_show_pixel_format_components = 0;
106 static int do_show_log = 0;
107
108 static int do_show_chapter_tags = 0;
109 static int do_show_format_tags = 0;
110 static int do_show_frame_tags = 0;
111 static int do_show_program_tags = 0;
112 static int do_show_stream_tags = 0;
113 static int do_show_packet_tags = 0;
114
115 static int show_value_unit = 0;
116 static int use_value_prefix = 0;
117 static int use_byte_value_binary_prefix = 0;
118 static int use_value_sexagesimal_format = 0;
119 static int show_private_data = 1;
120
121 #define SHOW_OPTIONAL_FIELDS_AUTO -1
122 #define SHOW_OPTIONAL_FIELDS_NEVER 0
123 #define SHOW_OPTIONAL_FIELDS_ALWAYS 1
124 static int show_optional_fields = SHOW_OPTIONAL_FIELDS_AUTO;
125
126 static char *print_format;
127 static char *stream_specifier;
128 static char *show_data_hash;
129
130 typedef struct ReadInterval {
131 int id; ///< identifier
132 int64_t start, end; ///< start, end in second/AV_TIME_BASE units
133 int has_start, has_end;
134 int start_is_offset, end_is_offset;
135 int duration_frames;
136 } ReadInterval;
137
138 static ReadInterval *read_intervals;
139 static int read_intervals_nb = 0;
140
141 static int find_stream_info = 1;
142
143 /* section structure definition */
144
145 #define SECTION_MAX_NB_CHILDREN 10
146
147 struct section {
148 int id; ///< unique id identifying a section
149 const char *name;
150
151 #define SECTION_FLAG_IS_WRAPPER 1 ///< the section only contains other sections, but has no data at its own level
152 #define SECTION_FLAG_IS_ARRAY 2 ///< the section contains an array of elements of the same type
153 #define SECTION_FLAG_HAS_VARIABLE_FIELDS 4 ///< the section may contain a variable number of fields with variable keys.
154 /// For these sections the element_name field is mandatory.
155 int flags;
156 int children_ids[SECTION_MAX_NB_CHILDREN+1]; ///< list of children section IDS, terminated by -1
157 const char *element_name; ///< name of the contained element, if provided
158 const char *unique_name; ///< unique section name, in case the name is ambiguous
159 AVDictionary *entries_to_show;
160 int show_all_entries;
161 };
162
163 typedef enum {
164 SECTION_ID_NONE = -1,
165 SECTION_ID_CHAPTER,
166 SECTION_ID_CHAPTER_TAGS,
167 SECTION_ID_CHAPTERS,
168 SECTION_ID_ERROR,
169 SECTION_ID_FORMAT,
170 SECTION_ID_FORMAT_TAGS,
171 SECTION_ID_FRAME,
172 SECTION_ID_FRAMES,
173 SECTION_ID_FRAME_TAGS,
174 SECTION_ID_FRAME_SIDE_DATA_LIST,
175 SECTION_ID_FRAME_SIDE_DATA,
176 SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST,
177 SECTION_ID_FRAME_SIDE_DATA_TIMECODE,
178 SECTION_ID_FRAME_LOG,
179 SECTION_ID_FRAME_LOGS,
180 SECTION_ID_LIBRARY_VERSION,
181 SECTION_ID_LIBRARY_VERSIONS,
182 SECTION_ID_PACKET,
183 SECTION_ID_PACKET_TAGS,
184 SECTION_ID_PACKETS,
185 SECTION_ID_PACKETS_AND_FRAMES,
186 SECTION_ID_PACKET_SIDE_DATA_LIST,
187 SECTION_ID_PACKET_SIDE_DATA,
188 SECTION_ID_PIXEL_FORMAT,
189 SECTION_ID_PIXEL_FORMAT_FLAGS,
190 SECTION_ID_PIXEL_FORMAT_COMPONENT,
191 SECTION_ID_PIXEL_FORMAT_COMPONENTS,
192 SECTION_ID_PIXEL_FORMATS,
193 SECTION_ID_PROGRAM_STREAM_DISPOSITION,
194 SECTION_ID_PROGRAM_STREAM_TAGS,
195 SECTION_ID_PROGRAM,
196 SECTION_ID_PROGRAM_STREAMS,
197 SECTION_ID_PROGRAM_STREAM,
198 SECTION_ID_PROGRAM_TAGS,
199 SECTION_ID_PROGRAM_VERSION,
200 SECTION_ID_PROGRAMS,
201 SECTION_ID_ROOT,
202 SECTION_ID_STREAM,
203 SECTION_ID_STREAM_DISPOSITION,
204 SECTION_ID_STREAMS,
205 SECTION_ID_STREAM_TAGS,
206 SECTION_ID_STREAM_SIDE_DATA_LIST,
207 SECTION_ID_STREAM_SIDE_DATA,
208 SECTION_ID_SUBTITLE,
209 } SectionID;
210
211 static struct section sections[] = {
212 [SECTION_ID_CHAPTERS] = { SECTION_ID_CHAPTERS, "chapters", SECTION_FLAG_IS_ARRAY, { SECTION_ID_CHAPTER, -1 } },
213 [SECTION_ID_CHAPTER] = { SECTION_ID_CHAPTER, "chapter", 0, { SECTION_ID_CHAPTER_TAGS, -1 } },
214 [SECTION_ID_CHAPTER_TAGS] = { SECTION_ID_CHAPTER_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "chapter_tags" },
215 [SECTION_ID_ERROR] = { SECTION_ID_ERROR, "error", 0, { -1 } },
216 [SECTION_ID_FORMAT] = { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
217 [SECTION_ID_FORMAT_TAGS] = { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
218 [SECTION_ID_FRAMES] = { SECTION_ID_FRAMES, "frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME, SECTION_ID_SUBTITLE, -1 } },
219 [SECTION_ID_FRAME] = { SECTION_ID_FRAME, "frame", 0, { SECTION_ID_FRAME_TAGS, SECTION_ID_FRAME_SIDE_DATA_LIST, SECTION_ID_FRAME_LOGS, -1 } },
220 [SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
221 [SECTION_ID_FRAME_SIDE_DATA_LIST] ={ SECTION_ID_FRAME_SIDE_DATA_LIST, "side_data_list", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_SIDE_DATA, -1 }, .element_name = "side_data", .unique_name = "frame_side_data_list" },
222 [SECTION_ID_FRAME_SIDE_DATA] = { SECTION_ID_FRAME_SIDE_DATA, "side_data", 0, { SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST, -1 } },
223 [SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST] = { SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST, "timecodes", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_SIDE_DATA_TIMECODE, -1 } },
224 [SECTION_ID_FRAME_SIDE_DATA_TIMECODE] = { SECTION_ID_FRAME_SIDE_DATA_TIMECODE, "timecode", 0, { -1 } },
225 [SECTION_ID_FRAME_LOGS] = { SECTION_ID_FRAME_LOGS, "logs", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_LOG, -1 } },
226 [SECTION_ID_FRAME_LOG] = { SECTION_ID_FRAME_LOG, "log", 0, { -1 }, },
227 [SECTION_ID_LIBRARY_VERSIONS] = { SECTION_ID_LIBRARY_VERSIONS, "library_versions", SECTION_FLAG_IS_ARRAY, { SECTION_ID_LIBRARY_VERSION, -1 } },
228 [SECTION_ID_LIBRARY_VERSION] = { SECTION_ID_LIBRARY_VERSION, "library_version", 0, { -1 } },
229 [SECTION_ID_PACKETS] = { SECTION_ID_PACKETS, "packets", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
230 [SECTION_ID_PACKETS_AND_FRAMES] = { SECTION_ID_PACKETS_AND_FRAMES, "packets_and_frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
231 [SECTION_ID_PACKET] = { SECTION_ID_PACKET, "packet", 0, { SECTION_ID_PACKET_TAGS, SECTION_ID_PACKET_SIDE_DATA_LIST, -1 } },
232 [SECTION_ID_PACKET_TAGS] = { SECTION_ID_PACKET_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "packet_tags" },
233 [SECTION_ID_PACKET_SIDE_DATA_LIST] ={ SECTION_ID_PACKET_SIDE_DATA_LIST, "side_data_list", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET_SIDE_DATA, -1 }, .element_name = "side_data", .unique_name = "packet_side_data_list" },
234 [SECTION_ID_PACKET_SIDE_DATA] = { SECTION_ID_PACKET_SIDE_DATA, "side_data", 0, { -1 } },
235 [SECTION_ID_PIXEL_FORMATS] = { SECTION_ID_PIXEL_FORMATS, "pixel_formats", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PIXEL_FORMAT, -1 } },
236 [SECTION_ID_PIXEL_FORMAT] = { SECTION_ID_PIXEL_FORMAT, "pixel_format", 0, { SECTION_ID_PIXEL_FORMAT_FLAGS, SECTION_ID_PIXEL_FORMAT_COMPONENTS, -1 } },
237 [SECTION_ID_PIXEL_FORMAT_FLAGS] = { SECTION_ID_PIXEL_FORMAT_FLAGS, "flags", 0, { -1 }, .unique_name = "pixel_format_flags" },
238 [SECTION_ID_PIXEL_FORMAT_COMPONENTS] = { SECTION_ID_PIXEL_FORMAT_COMPONENTS, "components", SECTION_FLAG_IS_ARRAY, {SECTION_ID_PIXEL_FORMAT_COMPONENT, -1 }, .unique_name = "pixel_format_components" },
239 [SECTION_ID_PIXEL_FORMAT_COMPONENT] = { SECTION_ID_PIXEL_FORMAT_COMPONENT, "component", 0, { -1 } },
240 [SECTION_ID_PROGRAM_STREAM_DISPOSITION] = { SECTION_ID_PROGRAM_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "program_stream_disposition" },
241 [SECTION_ID_PROGRAM_STREAM_TAGS] = { SECTION_ID_PROGRAM_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_stream_tags" },
242 [SECTION_ID_PROGRAM] = { SECTION_ID_PROGRAM, "program", 0, { SECTION_ID_PROGRAM_TAGS, SECTION_ID_PROGRAM_STREAMS, -1 } },
243 [SECTION_ID_PROGRAM_STREAMS] = { SECTION_ID_PROGRAM_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM_STREAM, -1 }, .unique_name = "program_streams" },
244 [SECTION_ID_PROGRAM_STREAM] = { SECTION_ID_PROGRAM_STREAM, "stream", 0, { SECTION_ID_PROGRAM_STREAM_DISPOSITION, SECTION_ID_PROGRAM_STREAM_TAGS, -1 }, .unique_name = "program_stream" },
245 [SECTION_ID_PROGRAM_TAGS] = { SECTION_ID_PROGRAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_tags" },
246 [SECTION_ID_PROGRAM_VERSION] = { SECTION_ID_PROGRAM_VERSION, "program_version", 0, { -1 } },
247 [SECTION_ID_PROGRAMS] = { SECTION_ID_PROGRAMS, "programs", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM, -1 } },
248 [SECTION_ID_ROOT] = { SECTION_ID_ROOT, "root", SECTION_FLAG_IS_WRAPPER,
249 { SECTION_ID_CHAPTERS, SECTION_ID_FORMAT, SECTION_ID_FRAMES, SECTION_ID_PROGRAMS, SECTION_ID_STREAMS,
250 SECTION_ID_PACKETS, SECTION_ID_ERROR, SECTION_ID_PROGRAM_VERSION, SECTION_ID_LIBRARY_VERSIONS,
251 SECTION_ID_PIXEL_FORMATS, -1} },
252 [SECTION_ID_STREAMS] = { SECTION_ID_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM, -1 } },
253 [SECTION_ID_STREAM] = { SECTION_ID_STREAM, "stream", 0, { SECTION_ID_STREAM_DISPOSITION, SECTION_ID_STREAM_TAGS, SECTION_ID_STREAM_SIDE_DATA_LIST, -1 } },
254 [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
255 [SECTION_ID_STREAM_TAGS] = { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
256 [SECTION_ID_STREAM_SIDE_DATA_LIST] ={ SECTION_ID_STREAM_SIDE_DATA_LIST, "side_data_list", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM_SIDE_DATA, -1 }, .element_name = "side_data", .unique_name = "stream_side_data_list" },
257 [SECTION_ID_STREAM_SIDE_DATA] = { SECTION_ID_STREAM_SIDE_DATA, "side_data", 0, { -1 } },
258 [SECTION_ID_SUBTITLE] = { SECTION_ID_SUBTITLE, "subtitle", 0, { -1 } },
259 };
260
261 static const OptionDef *options;
262
263 /* FFprobe context */
264 static const char *input_filename;
265 static const char *print_input_filename;
266 static const AVInputFormat *iformat = NULL;
267
268 static struct AVHashContext *hash;
269
270 static const struct {
271 double bin_val;
272 double dec_val;
273 const char *bin_str;
274 const char *dec_str;
275 } si_prefixes[] = {
276 { 1.0, 1.0, "", "" },
277 { 1.024e3, 1e3, "Ki", "K" },
278 { 1.048576e6, 1e6, "Mi", "M" },
279 { 1.073741824e9, 1e9, "Gi", "G" },
280 { 1.099511627776e12, 1e12, "Ti", "T" },
281 { 1.125899906842624e15, 1e15, "Pi", "P" },
282 };
283
284 static const char unit_second_str[] = "s" ;
285 static const char unit_hertz_str[] = "Hz" ;
286 static const char unit_byte_str[] = "byte" ;
287 static const char unit_bit_per_second_str[] = "bit/s";
288
289 static int nb_streams;
290 static uint64_t *nb_streams_packets;
291 static uint64_t *nb_streams_frames;
292 static int *selected_streams;
293
294 #if HAVE_THREADS
295 pthread_mutex_t log_mutex;
296 #endif
297 typedef struct LogBuffer {
298 char *context_name;
299 int log_level;
300 char *log_message;
301 AVClassCategory category;
302 char *parent_name;
303 AVClassCategory parent_category;
304 }LogBuffer;
305
306 static LogBuffer *log_buffer;
307 static int log_buffer_size;
308
309 static void log_callback(void *ptr, int level, const char *fmt, va_list vl)
310 {
311 AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
312 va_list vl2;
313 char line[1024];
314 static int print_prefix = 1;
315 void *new_log_buffer;
316
317 va_copy(vl2, vl);
318 av_log_default_callback(ptr, level, fmt, vl);
319 av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
320 va_end(vl2);
321
322 #if HAVE_THREADS
323 pthread_mutex_lock(&log_mutex);
324
325 new_log_buffer = av_realloc_array(log_buffer, log_buffer_size + 1, sizeof(*log_buffer));
326 if (new_log_buffer) {
327 char *msg;
328 int i;
329
330 log_buffer = new_log_buffer;
331 memset(&log_buffer[log_buffer_size], 0, sizeof(log_buffer[log_buffer_size]));
332 log_buffer[log_buffer_size].context_name= avc ? av_strdup(avc->item_name(ptr)) : NULL;
333 if (avc) {
334 if (avc->get_category) log_buffer[log_buffer_size].category = avc->get_category(ptr);
335 else log_buffer[log_buffer_size].category = avc->category;
336 }
337 log_buffer[log_buffer_size].log_level = level;
338 msg = log_buffer[log_buffer_size].log_message = av_strdup(line);
339 for (i=strlen(msg) - 1; i>=0 && msg[i] == '\n'; i--) {
340 msg[i] = 0;
341 }
342 if (avc && avc->parent_log_context_offset) {
343 AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
344 avc->parent_log_context_offset);
345 if (parent && *parent) {
346 log_buffer[log_buffer_size].parent_name = av_strdup((*parent)->item_name(parent));
347 log_buffer[log_buffer_size].parent_category =
348 (*parent)->get_category ? (*parent)->get_category(parent) :(*parent)->category;
349 }
350 }
351 log_buffer_size ++;
352 }
353
354 pthread_mutex_unlock(&log_mutex);
355 #endif
356 }
357
358 static void ffprobe_cleanup(int ret)
359 {
360 int i;
361 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
362 av_dict_free(&(sections[i].entries_to_show));
363
364 #if HAVE_THREADS
365 pthread_mutex_destroy(&log_mutex);
366 #endif
367 }
368
369 struct unit_value {
370 union { double d; long long int i; } val;
371 const char *unit;
372 };
373
374 37213 static char *value_string(char *buf, int buf_size, struct unit_value uv)
375 {
376 double vald;
377 long long int vali;
378 37213 int show_float = 0;
379
380
2/2
✓ Branch 0 taken 28474 times.
✓ Branch 1 taken 8739 times.
37213 if (uv.unit == unit_second_str) {
381 28474 vald = uv.val.d;
382 28474 show_float = 1;
383 } else {
384 8739 vald = vali = uv.val.i;
385 }
386
387
3/4
✓ Branch 0 taken 28474 times.
✓ Branch 1 taken 8739 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28474 times.
37213 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
388 double secs;
389 int hours, mins;
390 secs = vald;
391 mins = (int)secs / 60;
392 secs = secs - mins * 60;
393 hours = mins / 60;
394 mins %= 60;
395 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
396 } else {
397 37213 const char *prefix_string = "";
398
399
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 37213 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
37213 if (use_value_prefix && vald > 1) {
400 long long int index;
401
402 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
403 index = (long long int) (log2(vald)) / 10;
404 index = av_clip(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
405 vald /= si_prefixes[index].bin_val;
406 prefix_string = si_prefixes[index].bin_str;
407 } else {
408 index = (long long int) (log10(vald)) / 3;
409 index = av_clip(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
410 vald /= si_prefixes[index].dec_val;
411 prefix_string = si_prefixes[index].dec_str;
412 }
413 vali = vald;
414 }
415
416
3/6
✓ Branch 0 taken 8739 times.
✓ Branch 1 taken 28474 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8739 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
37213 if (show_float || (use_value_prefix && vald != (long long int)vald))
417 28474 snprintf(buf, buf_size, "%f", vald);
418 else
419 8739 snprintf(buf, buf_size, "%lld", vali);
420
2/4
✓ Branch 0 taken 37213 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37213 times.
37213 av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37213 times.
37213 prefix_string, show_value_unit ? uv.unit : "");
422 }
423
424 37213 return buf;
425 }
426
427 /* WRITERS API */
428
429 typedef struct WriterContext WriterContext;
430
431 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
432 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
433
434 typedef enum {
435 WRITER_STRING_VALIDATION_FAIL,
436 WRITER_STRING_VALIDATION_REPLACE,
437 WRITER_STRING_VALIDATION_IGNORE,
438 WRITER_STRING_VALIDATION_NB
439 } StringValidation;
440
441 typedef struct Writer {
442 const AVClass *priv_class; ///< private class of the writer, if any
443 int priv_size; ///< private size for the writer context
444 const char *name;
445
446 int (*init) (WriterContext *wctx);
447 void (*uninit)(WriterContext *wctx);
448
449 void (*print_section_header)(WriterContext *wctx);
450 void (*print_section_footer)(WriterContext *wctx);
451 void (*print_integer) (WriterContext *wctx, const char *, long long int);
452 void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
453 void (*print_string) (WriterContext *wctx, const char *, const char *);
454 int flags; ///< a combination or WRITER_FLAG_*
455 } Writer;
456
457 #define SECTION_MAX_NB_LEVELS 10
458
459 struct WriterContext {
460 const AVClass *class; ///< class of the writer
461 const Writer *writer; ///< the Writer of which this is an instance
462 char *name; ///< name of this writer instance
463 void *priv; ///< private data for use by the filter
464
465 const struct section *sections; ///< array containing all sections
466 int nb_sections; ///< number of sections
467
468 int level; ///< current level, starting from 0
469
470 /** number of the item printed in the given section, starting from 0 */
471 unsigned int nb_item[SECTION_MAX_NB_LEVELS];
472
473 /** section per each level */
474 const struct section *section[SECTION_MAX_NB_LEVELS];
475 AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
476 /// used by various writers
477
478 unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
479 unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
480 unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
481
482 int string_validation;
483 char *string_validation_replacement;
484 unsigned int string_validation_utf8_flags;
485 };
486
487 static const char *writer_get_name(void *p)
488 {
489 WriterContext *wctx = p;
490 return wctx->writer->name;
491 }
492
493 #define OFFSET(x) offsetof(WriterContext, x)
494
495 static const AVOption writer_options[] = {
496 { "string_validation", "set string validation mode",
497 OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
498 { "sv", "set string validation mode",
499 OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
500 { "ignore", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_IGNORE}, .unit = "sv" },
501 { "replace", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_REPLACE}, .unit = "sv" },
502 { "fail", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_FAIL}, .unit = "sv" },
503 { "string_validation_replacement", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str=""}},
504 { "svr", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str="\xEF\xBF\xBD"}},
505 { NULL }
506 };
507
508 28 static void *writer_child_next(void *obj, void *prev)
509 {
510 28 WriterContext *ctx = obj;
511
4/8
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
28 if (!prev && ctx->writer && ctx->writer->priv_class && ctx->priv)
512 28 return ctx->priv;
513 return NULL;
514 }
515
516 static const AVClass writer_class = {
517 .class_name = "Writer",
518 .item_name = writer_get_name,
519 .option = writer_options,
520 .version = LIBAVUTIL_VERSION_INT,
521 .child_next = writer_child_next,
522 };
523
524 101 static void writer_close(WriterContext **wctx)
525 {
526 int i;
527
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (!*wctx)
529 return;
530
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if ((*wctx)->writer->uninit)
532 (*wctx)->writer->uninit(*wctx);
533
2/2
✓ Branch 0 taken 1010 times.
✓ Branch 1 taken 101 times.
1111 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
534 1010 av_bprint_finalize(&(*wctx)->section_pbuf[i], NULL);
535
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 if ((*wctx)->writer->priv_class)
536 101 av_opt_free((*wctx)->priv);
537 101 av_freep(&((*wctx)->priv));
538 101 av_opt_free(*wctx);
539 101 av_freep(wctx);
540 }
541
542 static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size)
543 {
544 int i;
545 av_bprintf(bp, "0X");
546 for (i = 0; i < ubuf_size; i++)
547 av_bprintf(bp, "%02X", ubuf[i]);
548 }
549
550
551 101 static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
552 const struct section *sections, int nb_sections)
553 {
554 101 int i, ret = 0;
555
556
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
101 if (!(*wctx = av_mallocz(sizeof(WriterContext)))) {
557 ret = AVERROR(ENOMEM);
558 goto fail;
559 }
560
561
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
101 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
562 ret = AVERROR(ENOMEM);
563 goto fail;
564 }
565
566 101 (*wctx)->class = &writer_class;
567 101 (*wctx)->writer = writer;
568 101 (*wctx)->level = -1;
569 101 (*wctx)->sections = sections;
570 101 (*wctx)->nb_sections = nb_sections;
571
572 101 av_opt_set_defaults(*wctx);
573
574
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 if (writer->priv_class) {
575 101 void *priv_ctx = (*wctx)->priv;
576 101 *((const AVClass **)priv_ctx) = writer->priv_class;
577 101 av_opt_set_defaults(priv_ctx);
578 }
579
580 /* convert options to dictionary */
581
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 81 times.
101 if (args) {
582 20 AVDictionary *opts = NULL;
583 20 AVDictionaryEntry *opt = NULL;
584
585
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if ((ret = av_dict_parse_string(&opts, args, "=", ":", 0)) < 0) {
586 av_log(*wctx, AV_LOG_ERROR, "Failed to parse option string '%s' provided to writer context\n", args);
587 av_dict_free(&opts);
588 goto fail;
589 }
590
591
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 20 times.
48 while ((opt = av_dict_get(opts, "", opt, AV_DICT_IGNORE_SUFFIX))) {
592
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if ((ret = av_opt_set(*wctx, opt->key, opt->value, AV_OPT_SEARCH_CHILDREN)) < 0) {
593 av_log(*wctx, AV_LOG_ERROR, "Failed to set option '%s' with value '%s' provided to writer context\n",
594 opt->key, opt->value);
595 av_dict_free(&opts);
596 goto fail;
597 }
598 }
599
600 20 av_dict_free(&opts);
601 }
602
603 /* validate replace string */
604 {
605 101 const uint8_t *p = (*wctx)->string_validation_replacement;
606 101 const uint8_t *endp = p + strlen(p);
607
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 101 times.
202 while (*p) {
608 101 const uint8_t *p0 = p;
609 int32_t code;
610 101 ret = av_utf8_decode(&code, &p, endp, (*wctx)->string_validation_utf8_flags);
611
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (ret < 0) {
612 AVBPrint bp;
613 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
614 bprint_bytes(&bp, p0, p-p0),
615 av_log(wctx, AV_LOG_ERROR,
616 "Invalid UTF8 sequence %s found in string validation replace '%s'\n",
617 bp.str, (*wctx)->string_validation_replacement);
618 return ret;
619 }
620 }
621 }
622
623
2/2
✓ Branch 0 taken 1010 times.
✓ Branch 1 taken 101 times.
1111 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
624 1010 av_bprint_init(&(*wctx)->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
625
626
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 59 times.
101 if ((*wctx)->writer->init)
627 42 ret = (*wctx)->writer->init(*wctx);
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (ret < 0)
629 goto fail;
630
631 101 return 0;
632
633 fail:
634 writer_close(wctx);
635 return ret;
636 }
637
638 11577 static inline void writer_print_section_header(WriterContext *wctx,
639 int section_id)
640 {
641 int parent_section_id;
642 11577 wctx->level++;
643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11577 times.
11577 av_assert0(wctx->level < SECTION_MAX_NB_LEVELS);
644 23154 parent_section_id = wctx->level ?
645
2/2
✓ Branch 0 taken 11476 times.
✓ Branch 1 taken 101 times.
11577 (wctx->section[wctx->level-1])->id : SECTION_ID_NONE;
646
647 11577 wctx->nb_item[wctx->level] = 0;
648 11577 wctx->section[wctx->level] = &wctx->sections[section_id];
649
650
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 11572 times.
11577 if (section_id == SECTION_ID_PACKETS_AND_FRAMES) {
651 5 wctx->nb_section_packet = wctx->nb_section_frame =
652 5 wctx->nb_section_packet_frame = 0;
653
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 11432 times.
11572 } else if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
654 140 wctx->nb_section_packet_frame = section_id == SECTION_ID_PACKET ?
655
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 70 times.
140 wctx->nb_section_packet : wctx->nb_section_frame;
656 }
657
658
1/2
✓ Branch 0 taken 11577 times.
✗ Branch 1 not taken.
11577 if (wctx->writer->print_section_header)
659 11577 wctx->writer->print_section_header(wctx);
660 11577 }
661
662 11577 static inline void writer_print_section_footer(WriterContext *wctx)
663 {
664 11577 int section_id = wctx->section[wctx->level]->id;
665 23154 int parent_section_id = wctx->level ?
666
2/2
✓ Branch 0 taken 11476 times.
✓ Branch 1 taken 101 times.
11577 wctx->section[wctx->level-1]->id : SECTION_ID_NONE;
667
668
2/2
✓ Branch 0 taken 11476 times.
✓ Branch 1 taken 101 times.
11577 if (parent_section_id != SECTION_ID_NONE)
669 11476 wctx->nb_item[wctx->level-1]++;
670
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 11437 times.
11577 if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
671
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 70 times.
140 if (section_id == SECTION_ID_PACKET) wctx->nb_section_packet++;
672 70 else wctx->nb_section_frame++;
673 }
674
2/2
✓ Branch 0 taken 11493 times.
✓ Branch 1 taken 84 times.
11577 if (wctx->writer->print_section_footer)
675 11493 wctx->writer->print_section_footer(wctx);
676 11577 wctx->level--;
677 11577 }
678
679 51439 static inline void writer_print_integer(WriterContext *wctx,
680 const char *key, long long int val)
681 {
682 51439 const struct section *section = wctx->section[wctx->level];
683
684
4/4
✓ Branch 0 taken 31872 times.
✓ Branch 1 taken 19567 times.
✓ Branch 3 taken 16727 times.
✓ Branch 4 taken 15145 times.
51439 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
685 36294 wctx->writer->print_integer(wctx, key, val);
686 36294 wctx->nb_item[wctx->level]++;
687 }
688 51439 }
689
690 5202 static inline int validate_string(WriterContext *wctx, char **dstp, const char *src)
691 {
692 const uint8_t *p, *endp;
693 AVBPrint dstbuf;
694 5202 int invalid_chars_nb = 0, ret = 0;
695
696 5202 av_bprint_init(&dstbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
697
698 5202 endp = src + strlen(src);
699
2/2
✓ Branch 0 taken 81624 times.
✓ Branch 1 taken 5202 times.
86826 for (p = (uint8_t *)src; *p;) {
700 uint32_t code;
701 81624 int invalid = 0;
702 81624 const uint8_t *p0 = p;
703
704
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 81624 times.
81624 if (av_utf8_decode(&code, &p, endp, wctx->string_validation_utf8_flags) < 0) {
705 AVBPrint bp;
706 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
707 bprint_bytes(&bp, p0, p-p0);
708 av_log(wctx, AV_LOG_DEBUG,
709 "Invalid UTF-8 sequence %s found in string '%s'\n", bp.str, src);
710 invalid = 1;
711 }
712
713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81624 times.
81624 if (invalid) {
714 invalid_chars_nb++;
715
716 switch (wctx->string_validation) {
717 case WRITER_STRING_VALIDATION_FAIL:
718 av_log(wctx, AV_LOG_ERROR,
719 "Invalid UTF-8 sequence found in string '%s'\n", src);
720 ret = AVERROR_INVALIDDATA;
721 goto end;
722 break;
723
724 case WRITER_STRING_VALIDATION_REPLACE:
725 av_bprintf(&dstbuf, "%s", wctx->string_validation_replacement);
726 break;
727 }
728 }
729
730
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 81624 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
81624 if (!invalid || wctx->string_validation == WRITER_STRING_VALIDATION_IGNORE)
731 81624 av_bprint_append_data(&dstbuf, p0, p-p0);
732 }
733
734
1/4
✓ Branch 0 taken 5202 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5202 if (invalid_chars_nb && wctx->string_validation == WRITER_STRING_VALIDATION_REPLACE) {
735 av_log(wctx, AV_LOG_WARNING,
736 "%d invalid UTF-8 sequence(s) found in string '%s', replaced with '%s'\n",
737 invalid_chars_nb, src, wctx->string_validation_replacement);
738 }
739
740 5202 end:
741 5202 av_bprint_finalize(&dstbuf, dstp);
742 5202 return ret;
743 }
744
745 #define PRINT_STRING_OPT 1
746 #define PRINT_STRING_VALIDATE 2
747
748 78129 static inline int writer_print_string(WriterContext *wctx,
749 const char *key, const char *val, int flags)
750 {
751 78129 const struct section *section = wctx->section[wctx->level];
752 78129 int ret = 0;
753
754
1/2
✓ Branch 0 taken 78129 times.
✗ Branch 1 not taken.
78129 if (show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER ||
755
1/2
✓ Branch 0 taken 78129 times.
✗ Branch 1 not taken.
78129 (show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO
756
2/2
✓ Branch 0 taken 4782 times.
✓ Branch 1 taken 73347 times.
78129 && (flags & PRINT_STRING_OPT)
757
2/2
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 4524 times.
4782 && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS)))
758 258 return 0;
759
760
4/4
✓ Branch 0 taken 42081 times.
✓ Branch 1 taken 35790 times.
✓ Branch 3 taken 2102 times.
✓ Branch 4 taken 39979 times.
77871 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
761
2/2
✓ Branch 0 taken 2601 times.
✓ Branch 1 taken 35291 times.
37892 if (flags & PRINT_STRING_VALIDATE) {
762 2601 char *key1 = NULL, *val1 = NULL;
763 2601 ret = validate_string(wctx, &key1, key);
764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2601 times.
2601 if (ret < 0) goto end;
765 2601 ret = validate_string(wctx, &val1, val);
766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2601 times.
2601 if (ret < 0) goto end;
767 2601 wctx->writer->print_string(wctx, key1, val1);
768 2601 end:
769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2601 times.
2601 if (ret < 0) {
770 av_log(wctx, AV_LOG_ERROR,
771 "Invalid key=value string combination %s=%s in section %s\n",
772 key, val, section->unique_name);
773 }
774 2601 av_free(key1);
775 2601 av_free(val1);
776 } else {
777 35291 wctx->writer->print_string(wctx, key, val);
778 }
779
780 37892 wctx->nb_item[wctx->level]++;
781 }
782
783 77871 return ret;
784 }
785
786 1047 static inline void writer_print_rational(WriterContext *wctx,
787 const char *key, AVRational q, char sep)
788 {
789 AVBPrint buf;
790 1047 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
791 1047 av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
792 1047 writer_print_string(wctx, key, buf.str, 0);
793 1047 }
794
795 29059 static void writer_print_time(WriterContext *wctx, const char *key,
796 int64_t ts, const AVRational *time_base, int is_duration)
797 {
798 char buf[128];
799
800
8/8
✓ Branch 0 taken 20505 times.
✓ Branch 1 taken 8554 times.
✓ Branch 2 taken 19943 times.
✓ Branch 3 taken 562 times.
✓ Branch 4 taken 8554 times.
✓ Branch 5 taken 19943 times.
✓ Branch 6 taken 23 times.
✓ Branch 7 taken 8531 times.
29059 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
801 585 writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
802 } else {
803 28474 double d = ts * av_q2d(*time_base);
804 struct unit_value uv;
805 28474 uv.val.d = d;
806 28474 uv.unit = unit_second_str;
807 28474 value_string(buf, sizeof(buf), uv);
808 28474 writer_print_string(wctx, key, buf, 0);
809 }
810 29059 }
811
812 28961 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
813 {
814
8/8
✓ Branch 0 taken 20407 times.
✓ Branch 1 taken 8554 times.
✓ Branch 2 taken 19846 times.
✓ Branch 3 taken 561 times.
✓ Branch 4 taken 8554 times.
✓ Branch 5 taken 19846 times.
✓ Branch 6 taken 23 times.
✓ Branch 7 taken 8531 times.
28961 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
815 584 writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
816 } else {
817 28377 writer_print_integer(wctx, key, ts);
818 }
819 28961 }
820
821 static void writer_print_data(WriterContext *wctx, const char *name,
822 uint8_t *data, int size)
823 {
824 AVBPrint bp;
825 int offset = 0, l, i;
826
827 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
828 av_bprintf(&bp, "\n");
829 while (size) {
830 av_bprintf(&bp, "%08x: ", offset);
831 l = FFMIN(size, 16);
832 for (i = 0; i < l; i++) {
833 av_bprintf(&bp, "%02x", data[i]);
834 if (i & 1)
835 av_bprintf(&bp, " ");
836 }
837 av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
838 for (i = 0; i < l; i++)
839 av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
840 av_bprintf(&bp, "\n");
841 offset += l;
842 data += l;
843 size -= l;
844 }
845 writer_print_string(wctx, name, bp.str, 0);
846 av_bprint_finalize(&bp, NULL);
847 }
848
849 5586 static void writer_print_data_hash(WriterContext *wctx, const char *name,
850 uint8_t *data, int size)
851 {
852 5586 char *p, buf[AV_HASH_MAX_SIZE * 2 + 64] = { 0 };
853
854
2/2
✓ Branch 0 taken 3644 times.
✓ Branch 1 taken 1942 times.
5586 if (!hash)
855 3644 return;
856 1942 av_hash_init(hash);
857 1942 av_hash_update(hash, data, size);
858 1942 snprintf(buf, sizeof(buf), "%s:", av_hash_get_name(hash));
859 1942 p = buf + strlen(buf);
860 1942 av_hash_final_hex(hash, p, buf + sizeof(buf) - p);
861 1942 writer_print_string(wctx, name, buf, 0);
862 }
863
864 2 static void writer_print_integers(WriterContext *wctx, const char *name,
865 uint8_t *data, int size, const char *format,
866 int columns, int bytes, int offset_add)
867 {
868 AVBPrint bp;
869 2 int offset = 0, l, i;
870
871 2 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
872 2 av_bprintf(&bp, "\n");
873
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 while (size) {
874 6 av_bprintf(&bp, "%08x: ", offset);
875 6 l = FFMIN(size, columns);
876
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
24 for (i = 0; i < l; i++) {
877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (bytes == 1) av_bprintf(&bp, format, *data);
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 else if (bytes == 2) av_bprintf(&bp, format, AV_RN16(data));
879
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 else if (bytes == 4) av_bprintf(&bp, format, AV_RN32(data));
880 18 data += bytes;
881 18 size --;
882 }
883 6 av_bprintf(&bp, "\n");
884 6 offset += offset_add;
885 }
886 2 writer_print_string(wctx, name, bp.str, 0);
887 2 av_bprint_finalize(&bp, NULL);
888 2 }
889
890 #define MAX_REGISTERED_WRITERS_NB 64
891
892 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
893
894 707 static int writer_register(const Writer *writer)
895 {
896 static int next_registered_writer_idx = 0;
897
898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 707 times.
707 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
899 return AVERROR(ENOMEM);
900
901 707 registered_writers[next_registered_writer_idx++] = writer;
902 707 return 0;
903 }
904
905 101 static const Writer *writer_get_by_name(const char *name)
906 {
907 int i;
908
909
1/2
✓ Branch 0 taken 172 times.
✗ Branch 1 not taken.
172 for (i = 0; registered_writers[i]; i++)
910
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 71 times.
172 if (!strcmp(registered_writers[i]->name, name))
911 101 return registered_writers[i];
912
913 return NULL;
914 }
915
916
917 /* WRITERS */
918
919 #define DEFINE_WRITER_CLASS(name) \
920 static const char *name##_get_name(void *ctx) \
921 { \
922 return #name ; \
923 } \
924 static const AVClass name##_class = { \
925 .class_name = #name, \
926 .item_name = name##_get_name, \
927 .option = name##_options \
928 }
929
930 /* Default output */
931
932 typedef struct DefaultContext {
933 const AVClass *class;
934 int nokey;
935 int noprint_wrappers;
936 int nested_section[SECTION_MAX_NB_LEVELS];
937 } DefaultContext;
938
939 #undef OFFSET
940 #define OFFSET(x) offsetof(DefaultContext, x)
941
942 static const AVOption default_options[] = {
943 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
944 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
945 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
946 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
947 {NULL},
948 };
949
950 DEFINE_WRITER_CLASS(default);
951
952 /* lame uppercasing routine, assumes the string is lower case ASCII */
953 2669 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
954 {
955 int i;
956
3/4
✓ Branch 0 taken 16977 times.
✓ Branch 1 taken 2669 times.
✓ Branch 2 taken 16977 times.
✗ Branch 3 not taken.
19646 for (i = 0; src[i] && i < dst_size-1; i++)
957 16977 dst[i] = av_toupper(src[i]);
958 2669 dst[i] = 0;
959 2669 return dst;
960 }
961
962 1660 static void default_print_section_header(WriterContext *wctx)
963 {
964 1660 DefaultContext *def = wctx->priv;
965 char buf[32];
966 1660 const struct section *section = wctx->section[wctx->level];
967 3320 const struct section *parent_section = wctx->level ?
968
2/2
✓ Branch 0 taken 1602 times.
✓ Branch 1 taken 58 times.
1660 wctx->section[wctx->level-1] : NULL;
969
970 1660 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
971
2/2
✓ Branch 0 taken 1602 times.
✓ Branch 1 taken 58 times.
1660 if (parent_section &&
972
2/2
✓ Branch 0 taken 353 times.
✓ Branch 1 taken 1249 times.
1602 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
973 353 def->nested_section[wctx->level] = 1;
974 353 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
975 353 wctx->section_pbuf[wctx->level-1].str,
976 upcase_string(buf, sizeof(buf),
977 353 av_x_if_null(section->element_name, section->name)));
978 }
979
980
4/4
✓ Branch 0 taken 1652 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 353 times.
✓ Branch 3 taken 1299 times.
1660 if (def->noprint_wrappers || def->nested_section[wctx->level])
981 361 return;
982
983
2/2
✓ Branch 0 taken 1158 times.
✓ Branch 1 taken 141 times.
1299 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
984 1158 printf("[%s]\n", upcase_string(buf, sizeof(buf), section->name));
985 }
986
987 1660 static void default_print_section_footer(WriterContext *wctx)
988 {
989 1660 DefaultContext *def = wctx->priv;
990 1660 const struct section *section = wctx->section[wctx->level];
991 char buf[32];
992
993
4/4
✓ Branch 0 taken 1652 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 353 times.
✓ Branch 3 taken 1299 times.
1660 if (def->noprint_wrappers || def->nested_section[wctx->level])
994 361 return;
995
996
2/2
✓ Branch 0 taken 1158 times.
✓ Branch 1 taken 141 times.
1299 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
997 1158 printf("[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
998 }
999
1000 7198 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
1001 {
1002 7198 DefaultContext *def = wctx->priv;
1003
1004
2/2
✓ Branch 0 taken 7194 times.
✓ Branch 1 taken 4 times.
7198 if (!def->nokey)
1005 7194 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
1006 7198 printf("%s\n", value);
1007 7198 }
1008
1009 4500 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
1010 {
1011 4500 DefaultContext *def = wctx->priv;
1012
1013
1/2
✓ Branch 0 taken 4500 times.
✗ Branch 1 not taken.
4500 if (!def->nokey)
1014 4500 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
1015 4500 printf("%lld\n", value);
1016 4500 }
1017
1018 static const Writer default_writer = {
1019 .name = "default",
1020 .priv_size = sizeof(DefaultContext),
1021 .print_section_header = default_print_section_header,
1022 .print_section_footer = default_print_section_footer,
1023 .print_integer = default_print_int,
1024 .print_string = default_print_str,
1025 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
1026 .priv_class = &default_class,
1027 };
1028
1029 /* Compact output */
1030
1031 /**
1032 * Apply C-language-like string escaping.
1033 */
1034 28734 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
1035 {
1036 const char *p;
1037
1038
2/2
✓ Branch 0 taken 180422 times.
✓ Branch 1 taken 28734 times.
209156 for (p = src; *p; p++) {
1039
2/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 180418 times.
180422 switch (*p) {
1040 case '\b': av_bprintf(dst, "%s", "\\b"); break;
1041 case '\f': av_bprintf(dst, "%s", "\\f"); break;
1042 4 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1043 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1044 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
1045 180418 default:
1046
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 180418 times.
180418 if (*p == sep)
1047 av_bprint_chars(dst, '\\', 1);
1048 180418 av_bprint_chars(dst, *p, 1);
1049 }
1050 }
1051 28734 return dst->str;
1052 }
1053
1054 /**
1055 * Quote fields containing special characters, check RFC4180.
1056 */
1057 362 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
1058 {
1059 362 char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
1060 362 int needs_quoting = !!src[strcspn(src, meta_chars)];
1061
1062
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 361 times.
362 if (needs_quoting)
1063 1 av_bprint_chars(dst, '"', 1);
1064
1065
2/2
✓ Branch 0 taken 2312 times.
✓ Branch 1 taken 362 times.
2674 for (; *src; src++) {
1066
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2310 times.
2312 if (*src == '"')
1067 2 av_bprint_chars(dst, '"', 1);
1068 2312 av_bprint_chars(dst, *src, 1);
1069 }
1070
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 361 times.
362 if (needs_quoting)
1071 1 av_bprint_chars(dst, '"', 1);
1072 362 return dst->str;
1073 }
1074
1075 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
1076 {
1077 return src;
1078 }
1079
1080 typedef struct CompactContext {
1081 const AVClass *class;
1082 char *item_sep_str;
1083 char item_sep;
1084 int nokey;
1085 int print_section;
1086 char *escape_mode_str;
1087 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
1088 int nested_section[SECTION_MAX_NB_LEVELS];
1089 int has_nested_elems[SECTION_MAX_NB_LEVELS];
1090 int terminate_line[SECTION_MAX_NB_LEVELS];
1091 } CompactContext;
1092
1093 #undef OFFSET
1094 #define OFFSET(x) offsetof(CompactContext, x)
1095
1096 static const AVOption compact_options[]= {
1097 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, 0, 0 },
1098 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, 0, 0 },
1099 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1100 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1101 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, 0, 0 },
1102 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, 0, 0 },
1103 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1104 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1105 {NULL},
1106 };
1107
1108 DEFINE_WRITER_CLASS(compact);
1109
1110 36 static av_cold int compact_init(WriterContext *wctx)
1111 {
1112 36 CompactContext *compact = wctx->priv;
1113
1114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (strlen(compact->item_sep_str) != 1) {
1115 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
1116 compact->item_sep_str);
1117 return AVERROR(EINVAL);
1118 }
1119 36 compact->item_sep = compact->item_sep_str[0];
1120
1121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
1122
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 1 times.
36 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
1123
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
1124 else {
1125 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
1126 return AVERROR(EINVAL);
1127 }
1128
1129 36 return 0;
1130 }
1131
1132 9693 static void compact_print_section_header(WriterContext *wctx)
1133 {
1134 9693 CompactContext *compact = wctx->priv;
1135 9693 const struct section *section = wctx->section[wctx->level];
1136 19386 const struct section *parent_section = wctx->level ?
1137
2/2
✓ Branch 0 taken 9657 times.
✓ Branch 1 taken 36 times.
9693 wctx->section[wctx->level-1] : NULL;
1138 9693 compact->terminate_line[wctx->level] = 1;
1139 9693 compact->has_nested_elems[wctx->level] = 0;
1140
1141 9693 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
1142
4/4
✓ Branch 0 taken 8852 times.
✓ Branch 1 taken 841 times.
✓ Branch 2 taken 8816 times.
✓ Branch 3 taken 36 times.
9693 if (!(section->flags & SECTION_FLAG_IS_ARRAY) && parent_section &&
1143
2/2
✓ Branch 0 taken 479 times.
✓ Branch 1 taken 8337 times.
8816 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
1144 479 compact->nested_section[wctx->level] = 1;
1145 479 compact->has_nested_elems[wctx->level-1] = 1;
1146 479 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
1147 479 wctx->section_pbuf[wctx->level-1].str,
1148 479 (char *)av_x_if_null(section->element_name, section->name));
1149 479 wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level-1];
1150 } else {
1151
4/4
✓ Branch 0 taken 9178 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 9145 times.
9214 if (parent_section && compact->has_nested_elems[wctx->level-1] &&
1152
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 (section->flags & SECTION_FLAG_IS_ARRAY)) {
1153 33 compact->terminate_line[wctx->level-1] = 0;
1154 33 printf("\n");
1155 }
1156
2/2
✓ Branch 0 taken 7998 times.
✓ Branch 1 taken 1216 times.
9214 if (compact->print_section &&
1157
2/2
✓ Branch 0 taken 7249 times.
✓ Branch 1 taken 749 times.
7998 !(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
1158 7249 printf("%s%c", section->name, compact->item_sep);
1159 }
1160 9693 }
1161
1162 9693 static void compact_print_section_footer(WriterContext *wctx)
1163 {
1164 9693 CompactContext *compact = wctx->priv;
1165
1166
2/2
✓ Branch 0 taken 9181 times.
✓ Branch 1 taken 512 times.
9693 if (!compact->nested_section[wctx->level] &&
1167
2/2
✓ Branch 0 taken 9148 times.
✓ Branch 1 taken 33 times.
9181 compact->terminate_line[wctx->level] &&
1168
2/2
✓ Branch 0 taken 8304 times.
✓ Branch 1 taken 844 times.
9148 !(wctx->section[wctx->level]->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
1169 8304 printf("\n");
1170 9693 }
1171
1172 29096 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
1173 {
1174 29096 CompactContext *compact = wctx->priv;
1175 AVBPrint buf;
1176
1177
2/2
✓ Branch 0 taken 25420 times.
✓ Branch 1 taken 3676 times.
29096 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
1178
2/2
✓ Branch 0 taken 26216 times.
✓ Branch 1 taken 2880 times.
29096 if (!compact->nokey)
1179 26216 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
1180 29096 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1181 29096 printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
1182 29096 av_bprint_finalize(&buf, NULL);
1183 29096 }
1184
1185 30342 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
1186 {
1187 30342 CompactContext *compact = wctx->priv;
1188
1189
2/2
✓ Branch 0 taken 25685 times.
✓ Branch 1 taken 4657 times.
30342 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
1190
2/2
✓ Branch 0 taken 28565 times.
✓ Branch 1 taken 1777 times.
30342 if (!compact->nokey)
1191 28565 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
1192 30342 printf("%lld", value);
1193 30342 }
1194
1195 static const Writer compact_writer = {
1196 .name = "compact",
1197 .priv_size = sizeof(CompactContext),
1198 .init = compact_init,
1199 .print_section_header = compact_print_section_header,
1200 .print_section_footer = compact_print_section_footer,
1201 .print_integer = compact_print_int,
1202 .print_string = compact_print_str,
1203 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
1204 .priv_class = &compact_class,
1205 };
1206
1207 /* CSV output */
1208
1209 #undef OFFSET
1210 #define OFFSET(x) offsetof(CompactContext, x)
1211
1212 static const AVOption csv_options[] = {
1213 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, 0, 0 },
1214 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, 0, 0 },
1215 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1216 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1217 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, 0, 0 },
1218 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, 0, 0 },
1219 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1220 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1221 {NULL},
1222 };
1223
1224 DEFINE_WRITER_CLASS(csv);
1225
1226 static const Writer csv_writer = {
1227 .name = "csv",
1228 .priv_size = sizeof(CompactContext),
1229 .init = compact_init,
1230 .print_section_header = compact_print_section_header,
1231 .print_section_footer = compact_print_section_footer,
1232 .print_integer = compact_print_int,
1233 .print_string = compact_print_str,
1234 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
1235 .priv_class = &csv_class,
1236 };
1237
1238 /* Flat output */
1239
1240 typedef struct FlatContext {
1241 const AVClass *class;
1242 const char *sep_str;
1243 char sep;
1244 int hierarchical;
1245 } FlatContext;
1246
1247 #undef OFFSET
1248 #define OFFSET(x) offsetof(FlatContext, x)
1249
1250 static const AVOption flat_options[]= {
1251 {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
1252 {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
1253 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1254 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1255 {NULL},
1256 };
1257
1258 DEFINE_WRITER_CLASS(flat);
1259
1260 1 static av_cold int flat_init(WriterContext *wctx)
1261 {
1262 1 FlatContext *flat = wctx->priv;
1263
1264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (strlen(flat->sep_str) != 1) {
1265 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
1266 flat->sep_str);
1267 return AVERROR(EINVAL);
1268 }
1269 1 flat->sep = flat->sep_str[0];
1270
1271 1 return 0;
1272 }
1273
1274 362 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
1275 {
1276 const char *p;
1277
1278
2/2
✓ Branch 0 taken 3871 times.
✓ Branch 1 taken 362 times.
4233 for (p = src; *p; p++) {
1279
3/4
✓ Branch 0 taken 3871 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3870 times.
✓ Branch 3 taken 1 times.
3871 if (!((*p >= '0' && *p <= '9') ||
1280
3/4
✓ Branch 0 taken 3477 times.
✓ Branch 1 taken 393 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3477 times.
3870 (*p >= 'a' && *p <= 'z') ||
1281
3/4
✓ Branch 0 taken 393 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 392 times.
✓ Branch 3 taken 1 times.
393 (*p >= 'A' && *p <= 'Z')))
1282 392 av_bprint_chars(dst, '_', 1);
1283 else
1284 3479 av_bprint_chars(dst, *p, 1);
1285 }
1286 362 return dst->str;
1287 }
1288
1289 362 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
1290 {
1291 const char *p;
1292
1293
2/2
✓ Branch 0 taken 2312 times.
✓ Branch 1 taken 362 times.
2674 for (p = src; *p; p++) {
1294
2/7
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2310 times.
2312 switch (*p) {
1295 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1296 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1297 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
1298 2 case '"': av_bprintf(dst, "%s", "\\\""); break;
1299 case '`': av_bprintf(dst, "%s", "\\`"); break;
1300 case '$': av_bprintf(dst, "%s", "\\$"); break;
1301 2310 default: av_bprint_chars(dst, *p, 1); break;
1302 }
1303 }
1304 362 return dst->str;
1305 }
1306
1307 42 static void flat_print_section_header(WriterContext *wctx)
1308 {
1309 42 FlatContext *flat = wctx->priv;
1310 42 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1311 42 const struct section *section = wctx->section[wctx->level];
1312 84 const struct section *parent_section = wctx->level ?
1313
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 1 times.
42 wctx->section[wctx->level-1] : NULL;
1314
1315 /* build section header */
1316 42 av_bprint_clear(buf);
1317
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 41 times.
42 if (!parent_section)
1318 1 return;
1319 41 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1320
1321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (flat->hierarchical ||
1322 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
1323 41 av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
1324
1325
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 10 times.
41 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1326 62 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1327
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 3 times.
31 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1328 31 av_bprintf(buf, "%d%s", n, flat->sep_str);
1329 }
1330 }
1331 }
1332
1333 288 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
1334 {
1335 288 printf("%s%s=%lld\n", wctx->section_pbuf[wctx->level].str, key, value);
1336 288 }
1337
1338 362 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
1339 {
1340 362 FlatContext *flat = wctx->priv;
1341 AVBPrint buf;
1342
1343 362 printf("%s", wctx->section_pbuf[wctx->level].str);
1344 362 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1345 362 printf("%s=", flat_escape_key_str(&buf, key, flat->sep));
1346 362 av_bprint_clear(&buf);
1347 362 printf("\"%s\"\n", flat_escape_value_str(&buf, value));
1348 362 av_bprint_finalize(&buf, NULL);
1349 362 }
1350
1351 static const Writer flat_writer = {
1352 .name = "flat",
1353 .priv_size = sizeof(FlatContext),
1354 .init = flat_init,
1355 .print_section_header = flat_print_section_header,
1356 .print_integer = flat_print_int,
1357 .print_string = flat_print_str,
1358 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1359 .priv_class = &flat_class,
1360 };
1361
1362 /* INI format output */
1363
1364 typedef struct INIContext {
1365 const AVClass *class;
1366 int hierarchical;
1367 } INIContext;
1368
1369 #undef OFFSET
1370 #define OFFSET(x) offsetof(INIContext, x)
1371
1372 static const AVOption ini_options[] = {
1373 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1374 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1375 {NULL},
1376 };
1377
1378 DEFINE_WRITER_CLASS(ini);
1379
1380 724 static char *ini_escape_str(AVBPrint *dst, const char *src)
1381 {
1382 724 int i = 0;
1383 724 char c = 0;
1384
1385
2/2
✓ Branch 0 taken 6183 times.
✓ Branch 1 taken 724 times.
6907 while (c = src[i++]) {
1386
2/7
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
✓ Branch 6 taken 6169 times.
6183 switch (c) {
1387 case '\b': av_bprintf(dst, "%s", "\\b"); break;
1388 case '\f': av_bprintf(dst, "%s", "\\f"); break;
1389 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1390 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1391 case '\t': av_bprintf(dst, "%s", "\\t"); break;
1392 14 case '\\':
1393 case '#' :
1394 case '=' :
1395 14 case ':' : av_bprint_chars(dst, '\\', 1);
1396 6183 default:
1397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6183 times.
6183 if ((unsigned char)c < 32)
1398 av_bprintf(dst, "\\x00%02x", c & 0xff);
1399 else
1400 6183 av_bprint_chars(dst, c, 1);
1401 6183 break;
1402 }
1403 }
1404 724 return dst->str;
1405 }
1406
1407 42 static void ini_print_section_header(WriterContext *wctx)
1408 {
1409 42 INIContext *ini = wctx->priv;
1410 42 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1411 42 const struct section *section = wctx->section[wctx->level];
1412 84 const struct section *parent_section = wctx->level ?
1413
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 1 times.
42 wctx->section[wctx->level-1] : NULL;
1414
1415 42 av_bprint_clear(buf);
1416
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 41 times.
42 if (!parent_section) {
1417 1 printf("# ffprobe output\n\n");
1418 1 return;
1419 }
1420
1421
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 3 times.
41 if (wctx->nb_item[wctx->level-1])
1422 38 printf("\n");
1423
1424 41 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (ini->hierarchical ||
1426 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
1427
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 3 times.
41 av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
1428
1429
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 10 times.
41 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1430 62 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1431
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 3 times.
31 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1432 31 av_bprintf(buf, ".%d", n);
1433 }
1434 }
1435
1436
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 2 times.
41 if (!(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER)))
1437 39 printf("[%s]\n", buf->str);
1438 }
1439
1440 362 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1441 {
1442 AVBPrint buf;
1443
1444 362 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1445 362 printf("%s=", ini_escape_str(&buf, key));
1446 362 av_bprint_clear(&buf);
1447 362 printf("%s\n", ini_escape_str(&buf, value));
1448 362 av_bprint_finalize(&buf, NULL);
1449 362 }
1450
1451 288 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1452 {
1453 288 printf("%s=%lld\n", key, value);
1454 288 }
1455
1456 static const Writer ini_writer = {
1457 .name = "ini",
1458 .priv_size = sizeof(INIContext),
1459 .print_section_header = ini_print_section_header,
1460 .print_integer = ini_print_int,
1461 .print_string = ini_print_str,
1462 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1463 .priv_class = &ini_class,
1464 };
1465
1466 /* JSON output */
1467
1468 typedef struct JSONContext {
1469 const AVClass *class;
1470 int indent_level;
1471 int compact;
1472 const char *item_sep, *item_start_end;
1473 } JSONContext;
1474
1475 #undef OFFSET
1476 #define OFFSET(x) offsetof(JSONContext, x)
1477
1478 static const AVOption json_options[]= {
1479 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1480 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1481 { NULL }
1482 };
1483
1484 DEFINE_WRITER_CLASS(json);
1485
1486 3 static av_cold int json_init(WriterContext *wctx)
1487 {
1488 3 JSONContext *json = wctx->priv;
1489
1490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 json->item_sep = json->compact ? ", " : ",\n";
1491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 json->item_start_end = json->compact ? " " : "\n";
1492
1493 3 return 0;
1494 }
1495
1496 981 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1497 {
1498 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1499 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1500 const char *p;
1501
1502
2/2
✓ Branch 0 taken 8448 times.
✓ Branch 1 taken 981 times.
9429 for (p = src; *p; p++) {
1503 8448 char *s = strchr(json_escape, *p);
1504
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8446 times.
8448 if (s) {
1505 2 av_bprint_chars(dst, '\\', 1);
1506 2 av_bprint_chars(dst, json_subst[s - json_escape], 1);
1507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8446 times.
8446 } else if ((unsigned char)*p < 32) {
1508 av_bprintf(dst, "\\u00%02x", *p & 0xff);
1509 } else {
1510 8446 av_bprint_chars(dst, *p, 1);
1511 }
1512 }
1513 981 return dst->str;
1514 }
1515
1516 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
1517
1518 56 static void json_print_section_header(WriterContext *wctx)
1519 {
1520 56 JSONContext *json = wctx->priv;
1521 AVBPrint buf;
1522 56 const struct section *section = wctx->section[wctx->level];
1523 112 const struct section *parent_section = wctx->level ?
1524
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 3 times.
56 wctx->section[wctx->level-1] : NULL;
1525
1526
4/4
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 9 times.
56 if (wctx->level && wctx->nb_item[wctx->level-1])
1527 44 printf(",\n");
1528
1529
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 53 times.
56 if (section->flags & SECTION_FLAG_IS_WRAPPER) {
1530 3 printf("{\n");
1531 3 json->indent_level++;
1532 } else {
1533 53 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1534 53 json_escape_str(&buf, section->name, wctx);
1535 53 JSON_INDENT();
1536
1537 53 json->indent_level++;
1538
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 45 times.
53 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1539 8 printf("\"%s\": [\n", buf.str);
1540
3/4
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 35 times.
45 } else if (parent_section && !(parent_section->flags & SECTION_FLAG_IS_ARRAY)) {
1541 10 printf("\"%s\": {%s", buf.str, json->item_start_end);
1542 } else {
1543 35 printf("{%s", json->item_start_end);
1544
1545 /* this is required so the parser can distinguish between packets and frames */
1546
3/4
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 7 times.
35 if (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES) {
1547
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (!json->compact)
1548 28 JSON_INDENT();
1549 28 printf("\"type\": \"%s\"", section->name);
1550 }
1551 }
1552 53 av_bprint_finalize(&buf, NULL);
1553 }
1554 56 }
1555
1556 56 static void json_print_section_footer(WriterContext *wctx)
1557 {
1558 56 JSONContext *json = wctx->priv;
1559 56 const struct section *section = wctx->section[wctx->level];
1560
1561
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 53 times.
56 if (wctx->level == 0) {
1562 3 json->indent_level--;
1563 3 printf("\n}\n");
1564
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 45 times.
53 } else if (section->flags & SECTION_FLAG_IS_ARRAY) {
1565 8 printf("\n");
1566 8 json->indent_level--;
1567 8 JSON_INDENT();
1568 8 printf("]");
1569 } else {
1570 45 printf("%s", json->item_start_end);
1571 45 json->indent_level--;
1572
1/2
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
45 if (!json->compact)
1573 45 JSON_INDENT();
1574 45 printf("}");
1575 }
1576 56 }
1577
1578 314 static inline void json_print_item_str(WriterContext *wctx,
1579 const char *key, const char *value)
1580 {
1581 AVBPrint buf;
1582
1583 314 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1584 314 printf("\"%s\":", json_escape_str(&buf, key, wctx));
1585 314 av_bprint_clear(&buf);
1586 314 printf(" \"%s\"", json_escape_str(&buf, value, wctx));
1587 314 av_bprint_finalize(&buf, NULL);
1588 314 }
1589
1590 314 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1591 {
1592 314 JSONContext *json = wctx->priv;
1593 628 const struct section *parent_section = wctx->level ?
1594
1/2
✓ Branch 0 taken 314 times.
✗ Branch 1 not taken.
314 wctx->section[wctx->level-1] : NULL;
1595
1596
5/6
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 277 times.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✓ Branch 5 taken 9 times.
314 if (wctx->nb_item[wctx->level] || (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES))
1597 305 printf("%s", json->item_sep);
1598
1/2
✓ Branch 0 taken 314 times.
✗ Branch 1 not taken.
314 if (!json->compact)
1599 314 JSON_INDENT();
1600 314 json_print_item_str(wctx, key, value);
1601 314 }
1602
1603 300 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1604 {
1605 300 JSONContext *json = wctx->priv;
1606 600 const struct section *parent_section = wctx->level ?
1607
1/2
✓ Branch 0 taken 300 times.
✗ Branch 1 not taken.
300 wctx->section[wctx->level-1] : NULL;
1608 AVBPrint buf;
1609
1610
4/6
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 292 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
300 if (wctx->nb_item[wctx->level] || (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES))
1611 292 printf("%s", json->item_sep);
1612
1/2
✓ Branch 0 taken 300 times.
✗ Branch 1 not taken.
300 if (!json->compact)
1613 300 JSON_INDENT();
1614
1615 300 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1616 300 printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1617 300 av_bprint_finalize(&buf, NULL);
1618 300 }
1619
1620 static const Writer json_writer = {
1621 .name = "json",
1622 .priv_size = sizeof(JSONContext),
1623 .init = json_init,
1624 .print_section_header = json_print_section_header,
1625 .print_section_footer = json_print_section_footer,
1626 .print_integer = json_print_int,
1627 .print_string = json_print_str,
1628 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1629 .priv_class = &json_class,
1630 };
1631
1632 /* XML output */
1633
1634 typedef struct XMLContext {
1635 const AVClass *class;
1636 int within_tag;
1637 int indent_level;
1638 int fully_qualified;
1639 int xsd_strict;
1640 } XMLContext;
1641
1642 #undef OFFSET
1643 #define OFFSET(x) offsetof(XMLContext, x)
1644
1645 static const AVOption xml_options[] = {
1646 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1647 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1648 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1649 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1650 {NULL},
1651 };
1652
1653 DEFINE_WRITER_CLASS(xml);
1654
1655 2 static av_cold int xml_init(WriterContext *wctx)
1656 {
1657 2 XMLContext *xml = wctx->priv;
1658
1659
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (xml->xsd_strict) {
1660 1 xml->fully_qualified = 1;
1661 #define CHECK_COMPLIANCE(opt, opt_name) \
1662 if (opt) { \
1663 av_log(wctx, AV_LOG_ERROR, \
1664 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1665 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1666 return AVERROR(EINVAL); \
1667 }
1668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 CHECK_COMPLIANCE(show_private_data, "private");
1669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 CHECK_COMPLIANCE(show_value_unit, "unit");
1670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 CHECK_COMPLIANCE(use_value_prefix, "prefix");
1671 }
1672
1673 2 return 0;
1674 }
1675
1676 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1677
1678 84 static void xml_print_section_header(WriterContext *wctx)
1679 {
1680 84 XMLContext *xml = wctx->priv;
1681 84 const struct section *section = wctx->section[wctx->level];
1682 168 const struct section *parent_section = wctx->level ?
1683
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 2 times.
84 wctx->section[wctx->level-1] : NULL;
1684
1685
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 82 times.
84 if (wctx->level == 0) {
1686 2 const char *qual = " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
1687 "xmlns:ffprobe=\"http://www.ffmpeg.org/schema/ffprobe\" "
1688 "xsi:schemaLocation=\"http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd\"";
1689
1690 2 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1691 4 printf("<%sffprobe%s>\n",
1692
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 xml->fully_qualified ? "ffprobe:" : "",
1693
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 xml->fully_qualified ? qual : "");
1694 2 return;
1695 }
1696
1697
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 74 times.
82 if (xml->within_tag) {
1698 8 xml->within_tag = 0;
1699 8 printf(">\n");
1700 }
1701
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 74 times.
82 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1702 8 xml->indent_level++;
1703 } else {
1704
3/4
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 68 times.
74 if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
1705
3/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
6 wctx->level && wctx->nb_item[wctx->level-1])
1706 4 printf("\n");
1707 74 xml->indent_level++;
1708
1709
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 70 times.
74 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1710 4 XML_INDENT(); printf("<%s>\n", section->name);
1711 } else {
1712 70 XML_INDENT(); printf("<%s ", section->name);
1713 70 xml->within_tag = 1;
1714 }
1715 }
1716 }
1717
1718 84 static void xml_print_section_footer(WriterContext *wctx)
1719 {
1720 84 XMLContext *xml = wctx->priv;
1721 84 const struct section *section = wctx->section[wctx->level];
1722
1723
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 82 times.
84 if (wctx->level == 0) {
1724
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1725
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 20 times.
82 } else if (xml->within_tag) {
1726 62 xml->within_tag = 0;
1727 62 printf("/>\n");
1728 62 xml->indent_level--;
1729
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 12 times.
20 } else if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1730 8 xml->indent_level--;
1731 } else {
1732 12 XML_INDENT(); printf("</%s>\n", section->name);
1733 12 xml->indent_level--;
1734 }
1735 84 }
1736
1737 560 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1738 {
1739 AVBPrint buf;
1740 560 XMLContext *xml = wctx->priv;
1741 560 const struct section *section = wctx->section[wctx->level];
1742
1743 560 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1744
1745
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 542 times.
560 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1746 18 XML_INDENT();
1747 18 av_bprint_escape(&buf, key, NULL,
1748 AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
1749 18 printf("<%s key=\"%s\"",
1750 18 section->element_name, buf.str);
1751 18 av_bprint_clear(&buf);
1752
1753 18 av_bprint_escape(&buf, value, NULL,
1754 AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
1755 18 printf(" value=\"%s\"/>\n", buf.str);
1756 } else {
1757
2/2
✓ Branch 0 taken 484 times.
✓ Branch 1 taken 58 times.
542 if (wctx->nb_item[wctx->level])
1758 484 printf(" ");
1759
1760 542 av_bprint_escape(&buf, value, NULL,
1761 AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
1762 542 printf("%s=\"%s\"", key, buf.str);
1763 }
1764
1765 560 av_bprint_finalize(&buf, NULL);
1766 560 }
1767
1768 576 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1769 {
1770
2/2
✓ Branch 0 taken 564 times.
✓ Branch 1 taken 12 times.
576 if (wctx->nb_item[wctx->level])
1771 564 printf(" ");
1772 576 printf("%s=\"%lld\"", key, value);
1773 576 }
1774
1775 static Writer xml_writer = {
1776 .name = "xml",
1777 .priv_size = sizeof(XMLContext),
1778 .init = xml_init,
1779 .print_section_header = xml_print_section_header,
1780 .print_section_footer = xml_print_section_footer,
1781 .print_integer = xml_print_int,
1782 .print_string = xml_print_str,
1783 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1784 .priv_class = &xml_class,
1785 };
1786
1787 101 static void writer_register_all(void)
1788 {
1789 static int initialized;
1790
1791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (initialized)
1792 return;
1793 101 initialized = 1;
1794
1795 101 writer_register(&default_writer);
1796 101 writer_register(&compact_writer);
1797 101 writer_register(&csv_writer);
1798 101 writer_register(&flat_writer);
1799 101 writer_register(&ini_writer);
1800 101 writer_register(&json_writer);
1801 101 writer_register(&xml_writer);
1802 }
1803
1804 #define print_fmt(k, f, ...) do { \
1805 av_bprint_clear(&pbuf); \
1806 av_bprintf(&pbuf, f, __VA_ARGS__); \
1807 writer_print_string(w, k, pbuf.str, 0); \
1808 } while (0)
1809
1810 #define print_int(k, v) writer_print_integer(w, k, v)
1811 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1812 #define print_str(k, v) writer_print_string(w, k, v, 0)
1813 #define print_str_opt(k, v) writer_print_string(w, k, v, PRINT_STRING_OPT)
1814 #define print_str_validate(k, v) writer_print_string(w, k, v, PRINT_STRING_VALIDATE)
1815 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1816 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1817 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1818 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1819 #define print_val(k, v, u) do { \
1820 struct unit_value uv; \
1821 uv.val.i = v; \
1822 uv.unit = u; \
1823 writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
1824 } while (0)
1825
1826 #define print_section_header(s) writer_print_section_header(w, s)
1827 #define print_section_footer(s) writer_print_section_footer(w, s)
1828
1829 #define REALLOCZ_ARRAY_STREAM(ptr, cur_n, new_n) \
1830 { \
1831 ret = av_reallocp_array(&(ptr), (new_n), sizeof(*(ptr))); \
1832 if (ret < 0) \
1833 goto end; \
1834 memset( (ptr) + (cur_n), 0, ((new_n) - (cur_n)) * sizeof(*(ptr)) ); \
1835 }
1836
1837 1304 static inline int show_tags(WriterContext *w, AVDictionary *tags, int section_id)
1838 {
1839 1304 AVDictionaryEntry *tag = NULL;
1840 1304 int ret = 0;
1841
1842
2/2
✓ Branch 0 taken 657 times.
✓ Branch 1 taken 647 times.
1304 if (!tags)
1843 657 return 0;
1844 647 writer_print_section_header(w, section_id);
1845
1846
2/2
✓ Branch 1 taken 2595 times.
✓ Branch 2 taken 647 times.
3242 while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1847
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2595 times.
2595 if ((ret = print_str_validate(tag->key, tag->value)) < 0)
1848 break;
1849 }
1850 647 writer_print_section_footer(w);
1851
1852 647 return ret;
1853 }
1854
1855 1 static void print_dynamic_hdr10_plus(WriterContext *w, const AVDynamicHDRPlus *metadata)
1856 {
1857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!metadata)
1858 return;
1859 1 print_int("application version", metadata->application_version);
1860 1 print_int("num_windows", metadata->num_windows);
1861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 for (int n = 1; n < metadata->num_windows; n++) {
1862 const AVHDRPlusColorTransformParams *params = &metadata->params[n];
1863 print_q("window_upper_left_corner_x",
1864 params->window_upper_left_corner_x,'/');
1865 print_q("window_upper_left_corner_y",
1866 params->window_upper_left_corner_y,'/');
1867 print_q("window_lower_right_corner_x",
1868 params->window_lower_right_corner_x,'/');
1869 print_q("window_lower_right_corner_y",
1870 params->window_lower_right_corner_y,'/');
1871 print_q("window_upper_left_corner_x",
1872 params->window_upper_left_corner_x,'/');
1873 print_q("window_upper_left_corner_y",
1874 params->window_upper_left_corner_y,'/');
1875 print_int("center_of_ellipse_x",
1876 params->center_of_ellipse_x ) ;
1877 print_int("center_of_ellipse_y",
1878 params->center_of_ellipse_y );
1879 print_int("rotation_angle",
1880 params->rotation_angle);
1881 print_int("semimajor_axis_internal_ellipse",
1882 params->semimajor_axis_internal_ellipse);
1883 print_int("semimajor_axis_external_ellipse",
1884 params->semimajor_axis_external_ellipse);
1885 print_int("semiminor_axis_external_ellipse",
1886 params->semiminor_axis_external_ellipse);
1887 print_int("overlap_process_option",
1888 params->overlap_process_option);
1889 }
1890 1 print_q("targeted_system_display_maximum_luminance",
1891 metadata->targeted_system_display_maximum_luminance,'/');
1892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (metadata->targeted_system_display_actual_peak_luminance_flag) {
1893 print_int("num_rows_targeted_system_display_actual_peak_luminance",
1894 metadata->num_rows_targeted_system_display_actual_peak_luminance);
1895 print_int("num_cols_targeted_system_display_actual_peak_luminance",
1896 metadata->num_cols_targeted_system_display_actual_peak_luminance);
1897 for (int i = 0; i < metadata->num_rows_targeted_system_display_actual_peak_luminance; i++) {
1898 for (int j = 0; j < metadata->num_cols_targeted_system_display_actual_peak_luminance; j++) {
1899 print_q("targeted_system_display_actual_peak_luminance",
1900 metadata->targeted_system_display_actual_peak_luminance[i][j],'/');
1901 }
1902 }
1903 }
1904
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (int n = 0; n < metadata->num_windows; n++) {
1905 1 const AVHDRPlusColorTransformParams *params = &metadata->params[n];
1906
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (int i = 0; i < 3; i++) {
1907 3 print_q("maxscl",params->maxscl[i],'/');
1908 }
1909 1 print_q("average_maxrgb",
1910 params->average_maxrgb,'/');
1911 1 print_int("num_distribution_maxrgb_percentiles",
1912 params->num_distribution_maxrgb_percentiles);
1913
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) {
1914 9 print_int("distribution_maxrgb_percentage",
1915 params->distribution_maxrgb[i].percentage);
1916 9 print_q("distribution_maxrgb_percentile",
1917 params->distribution_maxrgb[i].percentile,'/');
1918 }
1919 1 print_q("fraction_bright_pixels",
1920 params->fraction_bright_pixels,'/');
1921 }
1922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (metadata->mastering_display_actual_peak_luminance_flag) {
1923 print_int("num_rows_mastering_display_actual_peak_luminance",
1924 metadata->num_rows_mastering_display_actual_peak_luminance);
1925 print_int("num_cols_mastering_display_actual_peak_luminance",
1926 metadata->num_cols_mastering_display_actual_peak_luminance);
1927 for (int i = 0; i < metadata->num_rows_mastering_display_actual_peak_luminance; i++) {
1928 for (int j = 0; j < metadata->num_cols_mastering_display_actual_peak_luminance; j++) {
1929 print_q("mastering_display_actual_peak_luminance",
1930 metadata->mastering_display_actual_peak_luminance[i][j],'/');
1931 }
1932 }
1933 }
1934
1935
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (int n = 0; n < metadata->num_windows; n++) {
1936 1 const AVHDRPlusColorTransformParams *params = &metadata->params[n];
1937
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (params->tone_mapping_flag) {
1938 1 print_q("knee_point_x", params->knee_point_x,'/');
1939 1 print_q("knee_point_y", params->knee_point_y,'/');
1940 1 print_int("num_bezier_curve_anchors",
1941 params->num_bezier_curve_anchors );
1942
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < params->num_bezier_curve_anchors; i++) {
1943 9 print_q("bezier_curve_anchors",
1944 params->bezier_curve_anchors[i],'/');
1945 }
1946 }
1947
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (params->color_saturation_mapping_flag) {
1948 print_q("color_saturation_weight",
1949 params->color_saturation_weight,'/');
1950 }
1951 }
1952 }
1953
1954 847 static void print_pkt_side_data(WriterContext *w,
1955 AVCodecParameters *par,
1956 const AVPacketSideData *side_data,
1957 int nb_side_data,
1958 SectionID id_data_list,
1959 SectionID id_data)
1960 {
1961 int i;
1962
1963 847 writer_print_section_header(w, id_data_list);
1964
2/2
✓ Branch 0 taken 854 times.
✓ Branch 1 taken 847 times.
1701 for (i = 0; i < nb_side_data; i++) {
1965 854 const AVPacketSideData *sd = &side_data[i];
1966 854 const char *name = av_packet_side_data_name(sd->type);
1967
1968 854 writer_print_section_header(w, id_data);
1969
1/2
✓ Branch 0 taken 854 times.
✗ Branch 1 not taken.
854 print_str("side_data_type", name ? name : "unknown");
1970
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 852 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
854 if (sd->type == AV_PKT_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
1971 2 writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
1972 2 print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
1973
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 847 times.
852 } else if (sd->type == AV_PKT_DATA_STEREO3D) {
1974 5 const AVStereo3D *stereo = (AVStereo3D *)sd->data;
1975 5 print_str("type", av_stereo3d_type_name(stereo->type));
1976 5 print_int("inverted", !!(stereo->flags & AV_STEREO3D_FLAG_INVERT));
1977
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 843 times.
847 } else if (sd->type == AV_PKT_DATA_SPHERICAL) {
1978 4 const AVSphericalMapping *spherical = (AVSphericalMapping *)sd->data;
1979 4 print_str("projection", av_spherical_projection_name(spherical->projection));
1980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (spherical->projection == AV_SPHERICAL_CUBEMAP) {
1981 print_int("padding", spherical->padding);
1982
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 } else if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE) {
1983 size_t l, t, r, b;
1984 4 av_spherical_tile_bounds(spherical, par->width, par->height,
1985 &l, &t, &r, &b);
1986 4 print_int("bound_left", l);
1987 4 print_int("bound_top", t);
1988 4 print_int("bound_right", r);
1989 4 print_int("bound_bottom", b);
1990 }
1991
1992 4 print_int("yaw", (double) spherical->yaw / (1 << 16));
1993 4 print_int("pitch", (double) spherical->pitch / (1 << 16));
1994 4 print_int("roll", (double) spherical->roll / (1 << 16));
1995
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 833 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
843 } else if (sd->type == AV_PKT_DATA_SKIP_SAMPLES && sd->size == 10) {
1996 10 print_int("skip_samples", AV_RL32(sd->data));
1997 10 print_int("discard_padding", AV_RL32(sd->data + 4));
1998 10 print_int("skip_reason", AV_RL8(sd->data + 8));
1999 10 print_int("discard_reason", AV_RL8(sd->data + 9));
2000
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 830 times.
833 } else if (sd->type == AV_PKT_DATA_MASTERING_DISPLAY_METADATA) {
2001 3 AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
2002
2003
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (metadata->has_primaries) {
2004 3 print_q("red_x", metadata->display_primaries[0][0], '/');
2005 3 print_q("red_y", metadata->display_primaries[0][1], '/');
2006 3 print_q("green_x", metadata->display_primaries[1][0], '/');
2007 3 print_q("green_y", metadata->display_primaries[1][1], '/');
2008 3 print_q("blue_x", metadata->display_primaries[2][0], '/');
2009 3 print_q("blue_y", metadata->display_primaries[2][1], '/');
2010
2011 3 print_q("white_point_x", metadata->white_point[0], '/');
2012 3 print_q("white_point_y", metadata->white_point[1], '/');
2013 }
2014
2015
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (metadata->has_luminance) {
2016 3 print_q("min_luminance", metadata->min_luminance, '/');
2017 3 print_q("max_luminance", metadata->max_luminance, '/');
2018 }
2019
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 827 times.
830 } else if (sd->type == AV_PKT_DATA_CONTENT_LIGHT_LEVEL) {
2020 3 AVContentLightMetadata *metadata = (AVContentLightMetadata *)sd->data;
2021 3 print_int("max_content", metadata->MaxCLL);
2022 3 print_int("max_average", metadata->MaxFALL);
2023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 827 times.
827 } else if (sd->type == AV_PKT_DATA_DOVI_CONF) {
2024 AVDOVIDecoderConfigurationRecord *dovi = (AVDOVIDecoderConfigurationRecord *)sd->data;
2025 print_int("dv_version_major", dovi->dv_version_major);
2026 print_int("dv_version_minor", dovi->dv_version_minor);
2027 print_int("dv_profile", dovi->dv_profile);
2028 print_int("dv_level", dovi->dv_level);
2029 print_int("rpu_present_flag", dovi->rpu_present_flag);
2030 print_int("el_present_flag", dovi->el_present_flag);
2031 print_int("bl_present_flag", dovi->bl_present_flag);
2032 print_int("dv_bl_signal_compatibility_id", dovi->dv_bl_signal_compatibility_id);
2033
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 826 times.
827 } else if (sd->type == AV_PKT_DATA_AUDIO_SERVICE_TYPE) {
2034 1 enum AVAudioServiceType *t = (enum AVAudioServiceType *)sd->data;
2035 1 print_int("service_type", *t);
2036
2/2
✓ Branch 0 taken 667 times.
✓ Branch 1 taken 159 times.
826 } else if (sd->type == AV_PKT_DATA_MPEGTS_STREAM_ID) {
2037 667 print_int("id", *sd->data);
2038
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 157 times.
159 } else if (sd->type == AV_PKT_DATA_CPB_PROPERTIES) {
2039 2 const AVCPBProperties *prop = (AVCPBProperties *)sd->data;
2040 2 print_int("max_bitrate", prop->max_bitrate);
2041 2 print_int("min_bitrate", prop->min_bitrate);
2042 2 print_int("avg_bitrate", prop->avg_bitrate);
2043 2 print_int("buffer_size", prop->buffer_size);
2044 2 print_int("vbv_delay", prop->vbv_delay);
2045
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 8 times.
157 } else if (sd->type == AV_PKT_DATA_WEBVTT_IDENTIFIER ||
2046
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 133 times.
149 sd->type == AV_PKT_DATA_WEBVTT_SETTINGS) {
2047
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (do_show_data)
2048 writer_print_data(w, "data", sd->data, sd->size);
2049 24 writer_print_data_hash(w, "data_hash", sd->data, sd->size);
2050 }
2051 854 writer_print_section_footer(w);
2052 }
2053 847 writer_print_section_footer(w);
2054 847 }
2055
2056 525 static void print_color_range(WriterContext *w, enum AVColorRange color_range)
2057 {
2058 525 const char *val = av_color_range_name(color_range);
2059
3/4
✓ Branch 0 taken 525 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 417 times.
✓ Branch 3 taken 108 times.
525 if (!val || color_range == AVCOL_RANGE_UNSPECIFIED) {
2060 417 print_str_opt("color_range", "unknown");
2061 } else {
2062 108 print_str("color_range", val);
2063 }
2064 525 }
2065
2066 525 static void print_color_space(WriterContext *w, enum AVColorSpace color_space)
2067 {
2068 525 const char *val = av_color_space_name(color_space);
2069
3/4
✓ Branch 0 taken 525 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 438 times.
✓ Branch 3 taken 87 times.
525 if (!val || color_space == AVCOL_SPC_UNSPECIFIED) {
2070 438 print_str_opt("color_space", "unknown");
2071 } else {
2072 87 print_str("color_space", val);
2073 }
2074 525 }
2075
2076 525 static void print_primaries(WriterContext *w, enum AVColorPrimaries color_primaries)
2077 {
2078 525 const char *val = av_color_primaries_name(color_primaries);
2079
3/4
✓ Branch 0 taken 525 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 445 times.
✓ Branch 3 taken 80 times.
525 if (!val || color_primaries == AVCOL_PRI_UNSPECIFIED) {
2080 445 print_str_opt("color_primaries", "unknown");
2081 } else {
2082 80 print_str("color_primaries", val);
2083 }
2084 525 }
2085
2086 525 static void print_color_trc(WriterContext *w, enum AVColorTransferCharacteristic color_trc)
2087 {
2088 525 const char *val = av_color_transfer_name(color_trc);
2089
3/4
✓ Branch 0 taken 525 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 444 times.
✓ Branch 3 taken 81 times.
525 if (!val || color_trc == AVCOL_TRC_UNSPECIFIED) {
2090 444 print_str_opt("color_transfer", "unknown");
2091 } else {
2092 81 print_str("color_transfer", val);
2093 }
2094 525 }
2095
2096 525 static void print_chroma_location(WriterContext *w, enum AVChromaLocation chroma_location)
2097 {
2098 525 const char *val = av_chroma_location_name(chroma_location);
2099
3/4
✓ Branch 0 taken 525 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 422 times.
✓ Branch 3 taken 103 times.
525 if (!val || chroma_location == AVCHROMA_LOC_UNSPECIFIED) {
2100 422 print_str_opt("chroma_location", "unspecified");
2101 } else {
2102 103 print_str("chroma_location", val);
2103 }
2104 525 }
2105
2106
2107 5989 static void clear_log(int need_lock)
2108 {
2109 int i;
2110
2111
1/2
✓ Branch 0 taken 5989 times.
✗ Branch 1 not taken.
5989 if (need_lock)
2112 5989 pthread_mutex_lock(&log_mutex);
2113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5989 times.
5989 for (i=0; i<log_buffer_size; i++) {
2114 av_freep(&log_buffer[i].context_name);
2115 av_freep(&log_buffer[i].parent_name);
2116 av_freep(&log_buffer[i].log_message);
2117 }
2118 5989 log_buffer_size = 0;
2119
1/2
✓ Branch 0 taken 5989 times.
✗ Branch 1 not taken.
5989 if(need_lock)
2120 5989 pthread_mutex_unlock(&log_mutex);
2121 5989 }
2122
2123 static int show_log(WriterContext *w, int section_ids, int section_id, int log_level)
2124 {
2125 int i;
2126 pthread_mutex_lock(&log_mutex);
2127 if (!log_buffer_size) {
2128 pthread_mutex_unlock(&log_mutex);
2129 return 0;
2130 }
2131 writer_print_section_header(w, section_ids);
2132
2133 for (i=0; i<log_buffer_size; i++) {
2134 if (log_buffer[i].log_level <= log_level) {
2135 writer_print_section_header(w, section_id);
2136 print_str("context", log_buffer[i].context_name);
2137 print_int("level", log_buffer[i].log_level);
2138 print_int("category", log_buffer[i].category);
2139 if (log_buffer[i].parent_name) {
2140 print_str("parent_context", log_buffer[i].parent_name);
2141 print_int("parent_category", log_buffer[i].parent_category);
2142 } else {
2143 print_str_opt("parent_context", "N/A");
2144 print_str_opt("parent_category", "N/A");
2145 }
2146 print_str("message", log_buffer[i].log_message);
2147 writer_print_section_footer(w);
2148 }
2149 }
2150 clear_log(0);
2151 pthread_mutex_unlock(&log_mutex);
2152
2153 writer_print_section_footer(w);
2154
2155 return 0;
2156 }
2157
2158 5515 static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int packet_idx)
2159 {
2160 char val_str[128];
2161 5515 AVStream *st = ifile->streams[pkt->stream_index].st;
2162 AVBPrint pbuf;
2163 const char *s;
2164
2165 5515 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2166
2167 5515 writer_print_section_header(w, SECTION_ID_PACKET);
2168
2169 5515 s = av_get_media_type_string(st->codecpar->codec_type);
2170
1/2
✓ Branch 0 taken 5515 times.
✗ Branch 1 not taken.
5515 if (s) print_str ("codec_type", s);
2171 else print_str_opt("codec_type", "unknown");
2172 5515 print_int("stream_index", pkt->stream_index);
2173 5515 print_ts ("pts", pkt->pts);
2174 5515 print_time("pts_time", pkt->pts, &st->time_base);
2175 5515 print_ts ("dts", pkt->dts);
2176 5515 print_time("dts_time", pkt->dts, &st->time_base);
2177 5515 print_duration_ts("duration", pkt->duration);
2178 5515 print_duration_time("duration_time", pkt->duration, &st->time_base);
2179 5515 print_val("size", pkt->size, unit_byte_str);
2180
2/2
✓ Branch 0 taken 5427 times.
✓ Branch 1 taken 88 times.
5515 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
2181 88 else print_str_opt("pos", "N/A");
2182
4/4
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 5509 times.
✓ Branch 3 taken 4555 times.
✓ Branch 4 taken 960 times.
5515 print_fmt("flags", "%c%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_',
2183 pkt->flags & AV_PKT_FLAG_DISCARD ? 'D' : '_');
2184
2185
2/2
✓ Branch 0 taken 834 times.
✓ Branch 1 taken 4681 times.
5515 if (pkt->side_data_elems) {
2186 size_t size;
2187 const uint8_t *side_metadata;
2188
2189 834 side_metadata = av_packet_get_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, &size);
2190
4/6
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 703 times.
✓ Branch 2 taken 131 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 131 times.
✗ Branch 5 not taken.
834 if (side_metadata && size && do_show_packet_tags) {
2191 131 AVDictionary *dict = NULL;
2192
1/2
✓ Branch 1 taken 131 times.
✗ Branch 2 not taken.
131 if (av_packet_unpack_dictionary(side_metadata, size, &dict) >= 0)
2193 131 show_tags(w, dict, SECTION_ID_PACKET_TAGS);
2194 131 av_dict_free(&dict);
2195 }
2196
2197 834 print_pkt_side_data(w, st->codecpar, pkt->side_data, pkt->side_data_elems,
2198 SECTION_ID_PACKET_SIDE_DATA_LIST,
2199 SECTION_ID_PACKET_SIDE_DATA);
2200 }
2201
2202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5515 times.
5515 if (do_show_data)
2203 writer_print_data(w, "data", pkt->data, pkt->size);
2204 5515 writer_print_data_hash(w, "data_hash", pkt->data, pkt->size);
2205 5515 writer_print_section_footer(w);
2206
2207 5515 av_bprint_finalize(&pbuf, NULL);
2208 5515 fflush(stdout);
2209 5515 }
2210
2211 static void show_subtitle(WriterContext *w, AVSubtitle *sub, AVStream *stream,
2212 AVFormatContext *fmt_ctx)
2213 {
2214 AVBPrint pbuf;
2215
2216 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2217
2218 writer_print_section_header(w, SECTION_ID_SUBTITLE);
2219
2220 print_str ("media_type", "subtitle");
2221 print_ts ("pts", sub->pts);
2222 print_time("pts_time", sub->pts, &AV_TIME_BASE_Q);
2223 print_int ("format", sub->format);
2224 print_int ("start_display_time", sub->start_display_time);
2225 print_int ("end_display_time", sub->end_display_time);
2226 print_int ("num_rects", sub->num_rects);
2227
2228 writer_print_section_footer(w);
2229
2230 av_bprint_finalize(&pbuf, NULL);
2231 fflush(stdout);
2232 }
2233
2234 3039 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
2235 AVFormatContext *fmt_ctx)
2236 {
2237 AVBPrint pbuf;
2238 char val_str[128];
2239 const char *s;
2240 int i;
2241
2242 3039 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2243
2244 3039 writer_print_section_header(w, SECTION_ID_FRAME);
2245
2246 3039 s = av_get_media_type_string(stream->codecpar->codec_type);
2247
1/2
✓ Branch 0 taken 3039 times.
✗ Branch 1 not taken.
3039 if (s) print_str ("media_type", s);
2248 else print_str_opt("media_type", "unknown");
2249 3039 print_int("stream_index", stream->index);
2250 3039 print_int("key_frame", frame->key_frame);
2251 3039 print_ts ("pts", frame->pts);
2252 3039 print_time("pts_time", frame->pts, &stream->time_base);
2253 3039 print_ts ("pkt_dts", frame->pkt_dts);
2254 3039 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
2255 3039 print_ts ("best_effort_timestamp", frame->best_effort_timestamp);
2256 3039 print_time("best_effort_timestamp_time", frame->best_effort_timestamp, &stream->time_base);
2257 3039 print_duration_ts ("pkt_duration", frame->pkt_duration);
2258 3039 print_duration_time("pkt_duration_time", frame->pkt_duration, &stream->time_base);
2259
2/2
✓ Branch 0 taken 2736 times.
✓ Branch 1 taken 303 times.
3039 if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
2260 303 else print_str_opt("pkt_pos", "N/A");
2261
1/2
✓ Branch 0 taken 3039 times.
✗ Branch 1 not taken.
3039 if (frame->pkt_size != -1) print_val ("pkt_size", frame->pkt_size, unit_byte_str);
2262 else print_str_opt("pkt_size", "N/A");
2263
2264
2/3
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 2575 times.
✗ Branch 2 not taken.
3039 switch (stream->codecpar->codec_type) {
2265 AVRational sar;
2266
2267 464 case AVMEDIA_TYPE_VIDEO:
2268 464 print_int("width", frame->width);
2269 464 print_int("height", frame->height);
2270 464 s = av_get_pix_fmt_name(frame->format);
2271
1/2
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
464 if (s) print_str ("pix_fmt", s);
2272 else print_str_opt("pix_fmt", "unknown");
2273 464 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
2274
2/2
✓ Branch 0 taken 461 times.
✓ Branch 1 taken 3 times.
464 if (sar.num) {
2275 461 print_q("sample_aspect_ratio", sar, ':');
2276 } else {
2277 3 print_str_opt("sample_aspect_ratio", "N/A");
2278 }
2279 464 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
2280 464 print_int("coded_picture_number", frame->coded_picture_number);
2281 464 print_int("display_picture_number", frame->display_picture_number);
2282 464 print_int("interlaced_frame", frame->interlaced_frame);
2283 464 print_int("top_field_first", frame->top_field_first);
2284 464 print_int("repeat_pict", frame->repeat_pict);
2285
2286 464 print_color_range(w, frame->color_range);
2287 464 print_color_space(w, frame->colorspace);
2288 464 print_primaries(w, frame->color_primaries);
2289 464 print_color_trc(w, frame->color_trc);
2290 464 print_chroma_location(w, frame->chroma_location);
2291 464 break;
2292
2293 2575 case AVMEDIA_TYPE_AUDIO:
2294 2575 s = av_get_sample_fmt_name(frame->format);
2295
1/2
✓ Branch 0 taken 2575 times.
✗ Branch 1 not taken.
2575 if (s) print_str ("sample_fmt", s);
2296 else print_str_opt("sample_fmt", "unknown");
2297 2575 print_int("nb_samples", frame->nb_samples);
2298 2575 print_int("channels", frame->channels);
2299
2/2
✓ Branch 0 taken 2352 times.
✓ Branch 1 taken 223 times.
2575 if (frame->channel_layout) {
2300 2352 av_bprint_clear(&pbuf);
2301 2352 av_bprint_channel_layout(&pbuf, frame->channels,
2302 frame->channel_layout);
2303 2352 print_str ("channel_layout", pbuf.str);
2304 } else
2305 223 print_str_opt("channel_layout", "unknown");
2306 2575 break;
2307 }
2308
2/2
✓ Branch 0 taken 1058 times.
✓ Branch 1 taken 1981 times.
3039 if (do_show_frame_tags)
2309 1058 show_tags(w, frame->metadata, SECTION_ID_FRAME_TAGS);
2310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3039 times.
3039 if (do_show_log)
2311 show_log(w, SECTION_ID_FRAME_LOGS, SECTION_ID_FRAME_LOG, do_show_log);
2312
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 2970 times.
3039 if (frame->nb_side_data) {
2313 69 writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_LIST);
2314
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 69 times.
141 for (i = 0; i < frame->nb_side_data; i++) {
2315 72 AVFrameSideData *sd = frame->side_data[i];
2316 const char *name;
2317
2318 72 writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA);
2319 72 name = av_frame_side_data_name(sd->type);
2320
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 print_str("side_data_type", name ? name : "unknown");
2321
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
72 if (sd->type == AV_FRAME_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
2322 writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
2323 print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
2324
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
72 } else if (sd->type == AV_FRAME_DATA_GOP_TIMECODE && sd->size >= 8) {
2325 char tcbuf[AV_TIMECODE_STR_SIZE];
2326 av_timecode_make_mpeg_tc_string(tcbuf, *(int64_t *)(sd->data));
2327 print_str("timecode", tcbuf);
2328
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 71 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
73 } else if (sd->type == AV_FRAME_DATA_S12M_TIMECODE && sd->size == 16) {
2329 1 uint32_t *tc = (uint32_t*)sd->data;
2330 1 int m = FFMIN(tc[0],3);
2331 1 writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST);
2332
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (int j = 1; j <= m ; j++) {
2333 char tcbuf[AV_TIMECODE_STR_SIZE];
2334 1 av_timecode_make_smpte_tc_string2(tcbuf, stream->avg_frame_rate, tc[j], 0, 0);
2335 1 writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_TIMECODE);
2336 1 print_str("value", tcbuf);
2337 1 writer_print_section_footer(w);
2338 }
2339 1 writer_print_section_footer(w);
2340
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 69 times.
71 } else if (sd->type == AV_FRAME_DATA_MASTERING_DISPLAY_METADATA) {
2341 2 AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
2342
2343
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (metadata->has_primaries) {
2344 2 print_q("red_x", metadata->display_primaries[0][0], '/');
2345 2 print_q("red_y", metadata->display_primaries[0][1], '/');
2346 2 print_q("green_x", metadata->display_primaries[1][0], '/');
2347 2 print_q("green_y", metadata->display_primaries[1][1], '/');
2348 2 print_q("blue_x", metadata->display_primaries[2][0], '/');
2349 2 print_q("blue_y", metadata->display_primaries[2][1], '/');
2350
2351 2 print_q("white_point_x", metadata->white_point[0], '/');
2352 2 print_q("white_point_y", metadata->white_point[1], '/');
2353 }
2354
2355
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (metadata->has_luminance) {
2356 1 print_q("min_luminance", metadata->min_luminance, '/');
2357 1 print_q("max_luminance", metadata->max_luminance, '/');
2358 }
2359
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 68 times.
69 } else if (sd->type == AV_FRAME_DATA_DYNAMIC_HDR_PLUS) {
2360 1 AVDynamicHDRPlus *metadata = (AVDynamicHDRPlus *)sd->data;
2361 1 print_dynamic_hdr10_plus(w, metadata);
2362
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 67 times.
68 } else if (sd->type == AV_FRAME_DATA_CONTENT_LIGHT_LEVEL) {
2363 1 AVContentLightMetadata *metadata = (AVContentLightMetadata *)sd->data;
2364 1 print_int("max_content", metadata->MaxCLL);
2365 1 print_int("max_average", metadata->MaxFALL);
2366
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 66 times.
67 } else if (sd->type == AV_FRAME_DATA_ICC_PROFILE) {
2367 1 AVDictionaryEntry *tag = av_dict_get(sd->metadata, "name", NULL, AV_DICT_MATCH_CASE);
2368
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (tag)
2369 1 print_str(tag->key, tag->value);
2370 1 print_int("size", sd->size);
2371 }
2372 72 writer_print_section_footer(w);
2373 }
2374 69 writer_print_section_footer(w);
2375 }
2376
2377 3039 writer_print_section_footer(w);
2378
2379 3039 av_bprint_finalize(&pbuf, NULL);
2380 3039 fflush(stdout);
2381 3039 }
2382
2383 5989 static av_always_inline int process_frame(WriterContext *w,
2384 InputFile *ifile,
2385 AVFrame *frame, AVPacket *pkt,
2386 int *packet_new)
2387 {
2388 5989 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2389 5989 AVCodecContext *dec_ctx = ifile->streams[pkt->stream_index].dec_ctx;
2390 5989 AVCodecParameters *par = ifile->streams[pkt->stream_index].st->codecpar;
2391 AVSubtitle sub;
2392 5989 int ret = 0, got_frame = 0;
2393
2394 5989 clear_log(1);
2395
2/4
✓ Branch 0 taken 5989 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5989 times.
✗ Branch 3 not taken.
5989 if (dec_ctx && dec_ctx->codec) {
2396
1/3
✓ Branch 0 taken 5989 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
5989 switch (par->codec_type) {
2397 5989 case AVMEDIA_TYPE_VIDEO:
2398 case AVMEDIA_TYPE_AUDIO:
2399
2/2
✓ Branch 0 taken 2957 times.
✓ Branch 1 taken 3032 times.
5989 if (*packet_new) {
2400 2957 ret = avcodec_send_packet(dec_ctx, pkt);
2401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2957 times.
2957 if (ret == AVERROR(EAGAIN)) {
2402 ret = 0;
2403
4/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2948 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 2 times.
2957 } else if (ret >= 0 || ret == AVERROR_EOF) {
2404 2955 ret = 0;
2405 2955 *packet_new = 0;
2406 }
2407 }
2408
2/2
✓ Branch 0 taken 5987 times.
✓ Branch 1 taken 2 times.
5989 if (ret >= 0) {
2409 5987 ret = avcodec_receive_frame(dec_ctx, frame);
2410
2/2
✓ Branch 0 taken 3039 times.
✓ Branch 1 taken 2948 times.
5987 if (ret >= 0) {
2411 3039 got_frame = 1;
2412
3/4
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 2892 times.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
2948 } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
2413 2948 ret = 0;
2414 }
2415 }
2416 5989 break;
2417
2418 case AVMEDIA_TYPE_SUBTITLE:
2419 if (*packet_new)
2420 ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_frame, pkt);
2421 *packet_new = 0;
2422 break;
2423 default:
2424 *packet_new = 0;
2425 }
2426 } else {
2427 *packet_new = 0;
2428 }
2429
2430
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5987 times.
5989 if (ret < 0)
2431 2 return ret;
2432
2/2
✓ Branch 0 taken 3039 times.
✓ Branch 1 taken 2948 times.
5987 if (got_frame) {
2433 3039 int is_sub = (par->codec_type == AVMEDIA_TYPE_SUBTITLE);
2434 3039 nb_streams_frames[pkt->stream_index]++;
2435
1/2
✓ Branch 0 taken 3039 times.
✗ Branch 1 not taken.
3039 if (do_show_frames)
2436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3039 times.
3039 if (is_sub)
2437 show_subtitle(w, &sub, ifile->streams[pkt->stream_index].st, fmt_ctx);
2438 else
2439 3039 show_frame(w, frame, ifile->streams[pkt->stream_index].st, fmt_ctx);
2440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3039 times.
3039 if (is_sub)
2441 avsubtitle_free(&sub);
2442 }
2443
3/4
✓ Branch 0 taken 2948 times.
✓ Branch 1 taken 3039 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2948 times.
5987 return got_frame || *packet_new;
2444 }
2445
2446 61 static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
2447 {
2448 61 av_log(log_ctx, log_level, "id:%d", interval->id);
2449
2450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (interval->has_start) {
2451 av_log(log_ctx, log_level, " start:%s%s", interval->start_is_offset ? "+" : "",
2452 av_ts2timestr(interval->start, &AV_TIME_BASE_Q));
2453 } else {
2454 61 av_log(log_ctx, log_level, " start:N/A");
2455 }
2456
2457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (interval->has_end) {
2458 av_log(log_ctx, log_level, " end:%s", interval->end_is_offset ? "+" : "");
2459 if (interval->duration_frames)
2460 av_log(log_ctx, log_level, "#%"PRId64, interval->end);
2461 else
2462 av_log(log_ctx, log_level, "%s", av_ts2timestr(interval->end, &AV_TIME_BASE_Q));
2463 } else {
2464 61 av_log(log_ctx, log_level, " end:N/A");
2465 }
2466
2467 61 av_log(log_ctx, log_level, "\n");
2468 61 }
2469
2470 61 static int read_interval_packets(WriterContext *w, InputFile *ifile,
2471 const ReadInterval *interval, int64_t *cur_ts)
2472 {
2473 61 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2474 61 AVPacket *pkt = NULL;
2475 61 AVFrame *frame = NULL;
2476 61 int ret = 0, i = 0, frame_count = 0;
2477 61 int64_t start = -INT64_MAX, end = interval->end;
2478
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
61 int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
2479
2480 61 av_log(NULL, AV_LOG_VERBOSE, "Processing read interval ");
2481 61 log_read_interval(interval, NULL, AV_LOG_VERBOSE);
2482
2483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (interval->has_start) {
2484 int64_t target;
2485 if (interval->start_is_offset) {
2486 if (*cur_ts == AV_NOPTS_VALUE) {
2487 av_log(NULL, AV_LOG_ERROR,
2488 "Could not seek to relative position since current "
2489 "timestamp is not defined\n");
2490 ret = AVERROR(EINVAL);
2491 goto end;
2492 }
2493 target = *cur_ts + interval->start;
2494 } else {
2495 target = interval->start;
2496 }
2497
2498 av_log(NULL, AV_LOG_VERBOSE, "Seeking to read interval start point %s\n",
2499 av_ts2timestr(target, &AV_TIME_BASE_Q));
2500 if ((ret = avformat_seek_file(fmt_ctx, -1, -INT64_MAX, target, INT64_MAX, 0)) < 0) {
2501 av_log(NULL, AV_LOG_ERROR, "Could not seek to position %"PRId64": %s\n",
2502 interval->start, av_err2str(ret));
2503 goto end;
2504 }
2505 }
2506
2507 61 frame = av_frame_alloc();
2508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (!frame) {
2509 ret = AVERROR(ENOMEM);
2510 goto end;
2511 }
2512 61 pkt = av_packet_alloc();
2513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (!pkt) {
2514 ret = AVERROR(ENOMEM);
2515 goto end;
2516 }
2517
2/2
✓ Branch 1 taken 8217 times.
✓ Branch 2 taken 61 times.
8278 while (!av_read_frame(fmt_ctx, pkt)) {
2518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8217 times.
8217 if (fmt_ctx->nb_streams > nb_streams) {
2519 REALLOCZ_ARRAY_STREAM(nb_streams_frames, nb_streams, fmt_ctx->nb_streams);
2520 REALLOCZ_ARRAY_STREAM(nb_streams_packets, nb_streams, fmt_ctx->nb_streams);
2521 REALLOCZ_ARRAY_STREAM(selected_streams, nb_streams, fmt_ctx->nb_streams);
2522 nb_streams = fmt_ctx->nb_streams;
2523 }
2524
2/2
✓ Branch 0 taken 8214 times.
✓ Branch 1 taken 3 times.
8217 if (selected_streams[pkt->stream_index]) {
2525 8214 AVRational tb = ifile->streams[pkt->stream_index].st->time_base;
2526
2527
2/2
✓ Branch 0 taken 8201 times.
✓ Branch 1 taken 13 times.
8214 if (pkt->pts != AV_NOPTS_VALUE)
2528 8201 *cur_ts = av_rescale_q(pkt->pts, tb, AV_TIME_BASE_Q);
2529
2530
4/4
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 8145 times.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 12 times.
8214 if (!has_start && *cur_ts != AV_NOPTS_VALUE) {
2531 57 start = *cur_ts;
2532 57 has_start = 1;
2533 }
2534
2535
4/6
✓ Branch 0 taken 8202 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 8202 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8202 times.
8214 if (has_start && !has_end && interval->end_is_offset) {
2536 end = start + interval->end;
2537 has_end = 1;
2538 }
2539
2540
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8214 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8214 if (interval->end_is_offset && interval->duration_frames) {
2541 if (frame_count >= interval->end)
2542 break;
2543
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 8214 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
8214 } else if (has_end && *cur_ts != AV_NOPTS_VALUE && *cur_ts >= end) {
2544 break;
2545 }
2546
2547 8214 frame_count++;
2548
2/2
✓ Branch 0 taken 5515 times.
✓ Branch 1 taken 2699 times.
8214 if (do_read_packets) {
2549
1/2
✓ Branch 0 taken 5515 times.
✗ Branch 1 not taken.
5515 if (do_show_packets)
2550 5515 show_packet(w, ifile, pkt, i++);
2551 5515 nb_streams_packets[pkt->stream_index]++;
2552 }
2553
2/2
✓ Branch 0 taken 2894 times.
✓ Branch 1 taken 5320 times.
8214 if (do_read_frames) {
2554 2894 int packet_new = 1;
2555
2/2
✓ Branch 1 taken 3032 times.
✓ Branch 2 taken 2894 times.
5926 while (process_frame(w, ifile, frame, pkt, &packet_new) > 0);
2556 }
2557 }
2558 8217 av_packet_unref(pkt);
2559 }
2560 61 av_packet_unref(pkt);
2561 //Flush remaining frames that are cached in the decoder
2562
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 61 times.
153 for (i = 0; i < fmt_ctx->nb_streams; i++) {
2563 92 pkt->stream_index = i;
2564
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 36 times.
92 if (do_read_frames)
2565
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 56 times.
63 while (process_frame(w, ifile, frame, pkt, &(int){1}) > 0);
2566 }
2567
2568 61 end:
2569 61 av_frame_free(&frame);
2570 61 av_packet_free(&pkt);
2571
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (ret < 0) {
2572 av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
2573 log_read_interval(interval, NULL, AV_LOG_ERROR);
2574 }
2575 61 return ret;
2576 }
2577
2578 61 static int read_packets(WriterContext *w, InputFile *ifile)
2579 {
2580 61 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2581 61 int i, ret = 0;
2582 61 int64_t cur_ts = fmt_ctx->start_time;
2583
2584
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 if (read_intervals_nb == 0) {
2585 61 ReadInterval interval = (ReadInterval) { .has_start = 0, .has_end = 0 };
2586 61 ret = read_interval_packets(w, ifile, &interval, &cur_ts);
2587 } else {
2588 for (i = 0; i < read_intervals_nb; i++) {
2589 ret = read_interval_packets(w, ifile, &read_intervals[i], &cur_ts);
2590 if (ret < 0)
2591 break;
2592 }
2593 }
2594
2595 61 return ret;
2596 }
2597
2598 130 static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, InputStream *ist, int in_program)
2599 {
2600 130 AVStream *stream = ist->st;
2601 AVCodecParameters *par;
2602 AVCodecContext *dec_ctx;
2603 char val_str[128];
2604 const char *s;
2605 AVRational sar, dar;
2606 AVBPrint pbuf;
2607 const AVCodecDescriptor *cd;
2608 130 int ret = 0;
2609 130 const char *profile = NULL;
2610
2611 130 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2612
2613
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 122 times.
130 writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM : SECTION_ID_STREAM);
2614
2615 130 print_int("index", stream->index);
2616
2617 130 par = stream->codecpar;
2618 130 dec_ctx = ist->dec_ctx;
2619
2/2
✓ Branch 1 taken 125 times.
✓ Branch 2 taken 5 times.
130 if (cd = avcodec_descriptor_get(par->codec_id)) {
2620 125 print_str("codec_name", cd->name);
2621
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 86 times.
125 if (!do_bitexact) {
2622
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 print_str("codec_long_name",
2623 cd->long_name ? cd->long_name : "unknown");
2624 }
2625 } else {
2626 5 print_str_opt("codec_name", "unknown");
2627
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if (!do_bitexact) {
2628 2 print_str_opt("codec_long_name", "unknown");
2629 }
2630 }
2631
2632
4/4
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 89 times.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 24 times.
130 if (!do_bitexact && (profile = avcodec_profile_name(par->codec_id, par->profile)))
2633 17 print_str("profile", profile);
2634 else {
2635
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 80 times.
113 if (par->profile != FF_PROFILE_UNKNOWN) {
2636 char profile_num[12];
2637 33 snprintf(profile_num, sizeof(profile_num), "%d", par->profile);
2638 33 print_str("profile", profile_num);
2639 } else
2640 80 print_str_opt("profile", "unknown");
2641 }
2642
2643 130 s = av_get_media_type_string(par->codec_type);
2644
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 if (s) print_str ("codec_type", s);
2645 else print_str_opt("codec_type", "unknown");
2646
2647 /* print AVI/FourCC tag */
2648 130 print_str("codec_tag_string", av_fourcc2str(par->codec_tag));
2649 130 print_fmt("codec_tag", "0x%04"PRIx32, par->codec_tag);
2650
2651
4/4
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 8 times.
130 switch (par->codec_type) {
2652 61 case AVMEDIA_TYPE_VIDEO:
2653 61 print_int("width", par->width);
2654 61 print_int("height", par->height);
2655
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 if (dec_ctx) {
2656 61 print_int("coded_width", dec_ctx->coded_width);
2657 61 print_int("coded_height", dec_ctx->coded_height);
2658 61 print_int("closed_captions", !!(dec_ctx->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS));
2659 61 print_int("film_grain", !!(dec_ctx->properties & FF_CODEC_PROPERTY_FILM_GRAIN));
2660 }
2661 61 print_int("has_b_frames", par->video_delay);
2662 61 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
2663
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 9 times.
61 if (sar.num) {
2664 52 print_q("sample_aspect_ratio", sar, ':');
2665 52 av_reduce(&dar.num, &dar.den,
2666 52 par->width * sar.num,
2667 52 par->height * sar.den,
2668 1024*1024);
2669 52 print_q("display_aspect_ratio", dar, ':');
2670 } else {
2671 9 print_str_opt("sample_aspect_ratio", "N/A");
2672 9 print_str_opt("display_aspect_ratio", "N/A");
2673 }
2674 61 s = av_get_pix_fmt_name(par->format);
2675
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 2 times.
61 if (s) print_str ("pix_fmt", s);
2676 2 else print_str_opt("pix_fmt", "unknown");
2677 61 print_int("level", par->level);
2678
2679 61 print_color_range(w, par->color_range);
2680 61 print_color_space(w, par->color_space);
2681 61 print_color_trc(w, par->color_trc);
2682 61 print_primaries(w, par->color_primaries);
2683 61 print_chroma_location(w, par->chroma_location);
2684
2685
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 47 times.
61 if (par->field_order == AV_FIELD_PROGRESSIVE)
2686 14 print_str("field_order", "progressive");
2687
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 42 times.
47 else if (par->field_order == AV_FIELD_TT)
2688 5 print_str("field_order", "tt");
2689
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 41 times.
42 else if (par->field_order == AV_FIELD_BB)
2690 1 print_str("field_order", "bb");
2691
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 39 times.
41 else if (par->field_order == AV_FIELD_TB)
2692 2 print_str("field_order", "tb");
2693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 else if (par->field_order == AV_FIELD_BT)
2694 print_str("field_order", "bt");
2695 else
2696 39 print_str_opt("field_order", "unknown");
2697
2698
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 if (dec_ctx)
2699 61 print_int("refs", dec_ctx->refs);
2700 61 break;
2701
2702 56 case AVMEDIA_TYPE_AUDIO:
2703 56 s = av_get_sample_fmt_name(par->format);
2704
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 if (s) print_str ("sample_fmt", s);
2705 else print_str_opt("sample_fmt", "unknown");
2706 56 print_val("sample_rate", par->sample_rate, unit_hertz_str);
2707 56 print_int("channels", par->channels);
2708
2709
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 23 times.
56 if (par->channel_layout) {
2710 33 av_bprint_clear(&pbuf);
2711 33 av_bprint_channel_layout(&pbuf, par->channels, par->channel_layout);
2712 33 print_str ("channel_layout", pbuf.str);
2713 } else {
2714 23 print_str_opt("channel_layout", "unknown");
2715 }
2716
2717 56 print_int("bits_per_sample", av_get_bits_per_sample(par->codec_id));
2718 56 break;
2719
2720 5 case AVMEDIA_TYPE_SUBTITLE:
2721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (par->width)
2722 print_int("width", par->width);
2723 else
2724 5 print_str_opt("width", "N/A");
2725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (par->height)
2726 print_int("height", par->height);
2727 else
2728 5 print_str_opt("height", "N/A");
2729 5 break;
2730 }
2731
2732
7/8
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 122 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 66 times.
✓ Branch 5 taken 56 times.
✓ Branch 6 taken 64 times.
✓ Branch 7 taken 2 times.
130 if (dec_ctx && dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
2733 64 const AVOption *opt = NULL;
2734
2/2
✓ Branch 1 taken 255 times.
✓ Branch 2 taken 64 times.
319 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
2735 uint8_t *str;
2736
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 28 times.
255 if (!(opt->flags & AV_OPT_FLAG_EXPORT)) continue;
2737
1/2
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
28 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
2738 28 print_str(opt->name, str);
2739 28 av_free(str);
2740 }
2741 }
2742 }
2743
2744
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 79 times.
130 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
2745 79 else print_str_opt("id", "N/A");
2746 130 print_q("r_frame_rate", stream->r_frame_rate, '/');
2747 130 print_q("avg_frame_rate", stream->avg_frame_rate, '/');
2748 130 print_q("time_base", stream->time_base, '/');
2749 130 print_ts ("start_pts", stream->start_time);
2750 130 print_time("start_time", stream->start_time, &stream->time_base);
2751 130 print_ts ("duration_ts", stream->duration);
2752 130 print_time("duration", stream->duration, &stream->time_base);
2753
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 63 times.
130 if (par->bit_rate > 0) print_val ("bit_rate", par->bit_rate, unit_bit_per_second_str);
2754 63 else print_str_opt("bit_rate", "N/A");
2755
3/4
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 122 times.
130 if (dec_ctx && dec_ctx->rc_max_rate > 0)
2756 print_val ("max_bit_rate", dec_ctx->rc_max_rate, unit_bit_per_second_str);
2757 else
2758 130 print_str_opt("max_bit_rate", "N/A");
2759
4/4
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 94 times.
130 if (dec_ctx && dec_ctx->bits_per_raw_sample > 0) print_fmt("bits_per_raw_sample", "%d", dec_ctx->bits_per_raw_sample);
2760 102 else print_str_opt("bits_per_raw_sample", "N/A");
2761
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 103 times.
130 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
2762 103 else print_str_opt("nb_frames", "N/A");
2763
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 100 times.
130 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
2764 100 else print_str_opt("nb_read_frames", "N/A");
2765
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 74 times.
130 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
2766 74 else print_str_opt("nb_read_packets", "N/A");
2767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 if (do_show_data)
2768 writer_print_data(w, "extradata", par->extradata,
2769 par->extradata_size);
2770
2771
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 83 times.
130 if (par->extradata_size > 0) {
2772 47 writer_print_data_hash(w, "extradata_hash", par->extradata,
2773 par->extradata_size);
2774 }
2775
2776 /* Print disposition information */
2777 #define PRINT_DISPOSITION(flagname, name) do { \
2778 print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
2779 } while (0)
2780
2781
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 51 times.
130 if (do_show_stream_disposition) {
2782
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM_DISPOSITION : SECTION_ID_STREAM_DISPOSITION);
2783 79 PRINT_DISPOSITION(DEFAULT, "default");
2784 79 PRINT_DISPOSITION(DUB, "dub");
2785 79 PRINT_DISPOSITION(ORIGINAL, "original");
2786 79 PRINT_DISPOSITION(COMMENT, "comment");
2787 79 PRINT_DISPOSITION(LYRICS, "lyrics");
2788 79 PRINT_DISPOSITION(KARAOKE, "karaoke");
2789 79 PRINT_DISPOSITION(FORCED, "forced");
2790 79 PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
2791 79 PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
2792 79 PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
2793 79 PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
2794 79 PRINT_DISPOSITION(TIMED_THUMBNAILS, "timed_thumbnails");
2795 79 PRINT_DISPOSITION(CAPTIONS, "captions");
2796 79 PRINT_DISPOSITION(DESCRIPTIONS, "descriptions");
2797 79 PRINT_DISPOSITION(METADATA, "metadata");
2798 79 PRINT_DISPOSITION(DEPENDENT, "dependent");
2799 79 PRINT_DISPOSITION(STILL_IMAGE, "still_image");
2800 79 writer_print_section_footer(w);
2801 }
2802
2803
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 56 times.
130 if (do_show_stream_tags)
2804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 ret = show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
2805
2806
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 117 times.
130 if (stream->nb_side_data) {
2807 13 print_pkt_side_data(w, stream->codecpar, stream->side_data, stream->nb_side_data,
2808 SECTION_ID_STREAM_SIDE_DATA_LIST,
2809 SECTION_ID_STREAM_SIDE_DATA);
2810 }
2811
2812 130 writer_print_section_footer(w);
2813 130 av_bprint_finalize(&pbuf, NULL);
2814 130 fflush(stdout);
2815
2816 130 return ret;
2817 }
2818
2819 64 static int show_streams(WriterContext *w, InputFile *ifile)
2820 {
2821 64 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2822 64 int i, ret = 0;
2823
2824 64 writer_print_section_header(w, SECTION_ID_STREAMS);
2825
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 64 times.
193 for (i = 0; i < ifile->nb_streams; i++)
2826
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 7 times.
129 if (selected_streams[i]) {
2827 122 ret = show_stream(w, fmt_ctx, i, &ifile->streams[i], 0);
2828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
122 if (ret < 0)
2829 break;
2830 }
2831 64 writer_print_section_footer(w);
2832
2833 64 return ret;
2834 }
2835
2836 3 static int show_program(WriterContext *w, InputFile *ifile, AVProgram *program)
2837 {
2838 3 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2839 3 int i, ret = 0;
2840
2841 3 writer_print_section_header(w, SECTION_ID_PROGRAM);
2842 3 print_int("program_id", program->id);
2843 3 print_int("program_num", program->program_num);
2844 3 print_int("nb_streams", program->nb_stream_indexes);
2845 3 print_int("pmt_pid", program->pmt_pid);
2846 3 print_int("pcr_pid", program->pcr_pid);
2847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (do_show_program_tags)
2848 ret = show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
2849
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
2850 goto end;
2851
2852 3 writer_print_section_header(w, SECTION_ID_PROGRAM_STREAMS);
2853
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 for (i = 0; i < program->nb_stream_indexes; i++) {
2854
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 if (selected_streams[program->stream_index[i]]) {
2855 8 ret = show_stream(w, fmt_ctx, program->stream_index[i], &ifile->streams[program->stream_index[i]], 1);
2856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
2857 break;
2858 }
2859 }
2860 3 writer_print_section_footer(w);
2861
2862 3 end:
2863 3 writer_print_section_footer(w);
2864 3 return ret;
2865 }
2866
2867 38 static int show_programs(WriterContext *w, InputFile *ifile)
2868 {
2869 38 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2870 38 int i, ret = 0;
2871
2872 38 writer_print_section_header(w, SECTION_ID_PROGRAMS);
2873
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 38 times.
41 for (i = 0; i < fmt_ctx->nb_programs; i++) {
2874 3 AVProgram *program = fmt_ctx->programs[i];
2875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!program)
2876 continue;
2877 3 ret = show_program(w, ifile, program);
2878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
2879 break;
2880 }
2881 38 writer_print_section_footer(w);
2882 38 return ret;
2883 }
2884
2885 4 static int show_chapters(WriterContext *w, InputFile *ifile)
2886 {
2887 4 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2888 4 int i, ret = 0;
2889
2890 4 writer_print_section_header(w, SECTION_ID_CHAPTERS);
2891
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 4 times.
22 for (i = 0; i < fmt_ctx->nb_chapters; i++) {
2892 18 AVChapter *chapter = fmt_ctx->chapters[i];
2893
2894 18 writer_print_section_header(w, SECTION_ID_CHAPTER);
2895 18 print_int("id", chapter->id);
2896 18 print_q ("time_base", chapter->time_base, '/');
2897 18 print_int("start", chapter->start);
2898 18 print_time("start_time", chapter->start, &chapter->time_base);
2899 18 print_int("end", chapter->end);
2900 18 print_time("end_time", chapter->end, &chapter->time_base);
2901
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (do_show_chapter_tags)
2902 18 ret = show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
2903 18 writer_print_section_footer(w);
2904 }
2905 4 writer_print_section_footer(w);
2906
2907 4 return ret;
2908 }
2909
2910 31 static int show_format(WriterContext *w, InputFile *ifile)
2911 {
2912 31 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2913 char val_str[128];
2914
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
2915 31 int ret = 0;
2916
2917 31 writer_print_section_header(w, SECTION_ID_FORMAT);
2918 31 print_str_validate("filename", fmt_ctx->url);
2919 31 print_int("nb_streams", fmt_ctx->nb_streams);
2920 31 print_int("nb_programs", fmt_ctx->nb_programs);
2921 31 print_str("format_name", fmt_ctx->iformat->name);
2922
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 22 times.
31 if (!do_bitexact) {
2923
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
2924 else print_str_opt("format_long_name", "unknown");
2925 }
2926 31 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
2927 31 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
2928
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (size >= 0) print_val ("size", size, unit_byte_str);
2929 else print_str_opt("size", "N/A");
2930
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
2931 else print_str_opt("bit_rate", "N/A");
2932 31 print_int("probe_score", fmt_ctx->probe_score);
2933
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 8 times.
31 if (do_show_format_tags)
2934 23 ret = show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
2935
2936 31 writer_print_section_footer(w);
2937 31 fflush(stdout);
2938 31 return ret;
2939 }
2940
2941 static void show_error(WriterContext *w, int err)
2942 {
2943 char errbuf[128];
2944 const char *errbuf_ptr = errbuf;
2945
2946 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
2947 errbuf_ptr = strerror(AVUNERROR(err));
2948
2949 writer_print_section_header(w, SECTION_ID_ERROR);
2950 print_int("code", err);
2951 print_str("string", errbuf_ptr);
2952 writer_print_section_footer(w);
2953 }
2954
2955 101 static int open_input_file(InputFile *ifile, const char *filename,
2956 const char *print_filename)
2957 {
2958 int err, i;
2959 101 AVFormatContext *fmt_ctx = NULL;
2960 101 AVDictionaryEntry *t = NULL;
2961 101 int scan_all_pmts_set = 0;
2962
2963 101 fmt_ctx = avformat_alloc_context();
2964
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (!fmt_ctx) {
2965 print_error(filename, AVERROR(ENOMEM));
2966 exit_program(1);
2967 }
2968
2969
1/2
✓ Branch 1 taken 101 times.
✗ Branch 2 not taken.
101 if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
2970 101 av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
2971 101 scan_all_pmts_set = 1;
2972 }
2973
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
101 if ((err = avformat_open_input(&fmt_ctx, filename,
2974 iformat, &format_opts)) < 0) {
2975 print_error(filename, err);
2976 return err;
2977 }
2978
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 87 times.
101 if (print_filename) {
2979 14 av_freep(&fmt_ctx->url);
2980 14 fmt_ctx->url = av_strdup(print_filename);
2981 }
2982 101 ifile->fmt_ctx = fmt_ctx;
2983
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 if (scan_all_pmts_set)
2984 101 av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
2985
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
101 while ((t = av_dict_get(format_opts, "", t, AV_DICT_IGNORE_SUFFIX)))
2986 av_log(NULL, AV_LOG_WARNING, "Option %s skipped - not known to demuxer.\n", t->key);
2987
2988
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 if (find_stream_info) {
2989 101 AVDictionary **opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
2990 101 int orig_nb_streams = fmt_ctx->nb_streams;
2991
2992 101 err = avformat_find_stream_info(fmt_ctx, opts);
2993
2994
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 101 times.
263 for (i = 0; i < orig_nb_streams; i++)
2995 162 av_dict_free(&opts[i]);
2996 101 av_freep(&opts);
2997
2998
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (err < 0) {
2999 print_error(filename, err);
3000 return err;
3001 }
3002 }
3003
3004 101 av_dump_format(fmt_ctx, 0, filename, 0);
3005
3006 101 ifile->streams = av_mallocz_array(fmt_ctx->nb_streams,
3007 sizeof(*ifile->streams));
3008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (!ifile->streams)
3009 exit(1);
3010 101 ifile->nb_streams = fmt_ctx->nb_streams;
3011
3012 /* bind a decoder to each input stream */
3013
2/2
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 101 times.
272 for (i = 0; i < fmt_ctx->nb_streams; i++) {
3014 171 InputStream *ist = &ifile->streams[i];
3015 171 AVStream *stream = fmt_ctx->streams[i];
3016 const AVCodec *codec;
3017
3018 171 ist->st = stream;
3019
3020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 171 times.
171 if (stream->codecpar->codec_id == AV_CODEC_ID_PROBE) {
3021 av_log(NULL, AV_LOG_WARNING,
3022 "Failed to probe codec for input stream %d\n",
3023 stream->index);
3024 continue;
3025 }
3026
3027 171 codec = avcodec_find_decoder(stream->codecpar->codec_id);
3028
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 162 times.
171 if (!codec) {
3029 9 av_log(NULL, AV_LOG_WARNING,
3030 "Unsupported codec with id %d for input stream %d\n",
3031 9 stream->codecpar->codec_id, stream->index);
3032 9 continue;
3033 }
3034 {
3035 162 AVDictionary *opts = filter_codec_opts(codec_opts, stream->codecpar->codec_id,
3036 fmt_ctx, stream, codec);
3037
3038 162 ist->dec_ctx = avcodec_alloc_context3(codec);
3039
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 162 times.
162 if (!ist->dec_ctx)
3040 exit(1);
3041
3042 162 err = avcodec_parameters_to_context(ist->dec_ctx, stream->codecpar);
3043
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 162 times.
162 if (err < 0)
3044 exit(1);
3045
3046
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 162 times.
162 if (do_show_log) {
3047 // For loging it is needed to disable at least frame threads as otherwise
3048 // the log information would need to be reordered and matches up to contexts and frames
3049 // That is in fact possible but not trivial
3050 av_dict_set(&codec_opts, "threads", "1", 0);
3051 }
3052
3053 162 ist->dec_ctx->pkt_timebase = stream->time_base;
3054
3055
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 162 times.
162 if (avcodec_open2(ist->dec_ctx, codec, &opts) < 0) {
3056 av_log(NULL, AV_LOG_WARNING, "Could not open codec for input stream %d\n",
3057 stream->index);
3058 exit(1);
3059 }
3060
3061
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 162 times.
162 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
3062 av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
3063 t->key, stream->index);
3064 return AVERROR_OPTION_NOT_FOUND;
3065 }
3066 }
3067 }
3068
3069 101 ifile->fmt_ctx = fmt_ctx;
3070 101 return 0;
3071 }
3072
3073 101 static void close_input_file(InputFile *ifile)
3074 {
3075 int i;
3076
3077 /* close decoder for each stream */
3078
2/2
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 101 times.
272 for (i = 0; i < ifile->nb_streams; i++)
3079 171 avcodec_free_context(&ifile->streams[i].dec_ctx);
3080
3081 101 av_freep(&ifile->streams);
3082 101 ifile->nb_streams = 0;
3083
3084 101 avformat_close_input(&ifile->fmt_ctx);
3085 101 }
3086
3087 101 static int probe_file(WriterContext *wctx, const char *filename,
3088 const char *print_filename)
3089 {
3090 101 InputFile ifile = { 0 };
3091 int ret, i;
3092 int section_id;
3093
3094
3/4
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 63 times.
101 do_read_frames = do_show_frames || do_count_frames;
3095
3/4
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 67 times.
101 do_read_packets = do_show_packets || do_count_packets;
3096
3097 101 ret = open_input_file(&ifile, filename, print_filename);
3098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (ret < 0)
3099 goto end;
3100
3101 #define CHECK_END if (ret < 0) goto end
3102
3103 101 nb_streams = ifile.fmt_ctx->nb_streams;
3104
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
101 REALLOCZ_ARRAY_STREAM(nb_streams_frames,0,ifile.fmt_ctx->nb_streams);
3105
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
101 REALLOCZ_ARRAY_STREAM(nb_streams_packets,0,ifile.fmt_ctx->nb_streams);
3106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
101 REALLOCZ_ARRAY_STREAM(selected_streams,0,ifile.fmt_ctx->nb_streams);
3107
3108
2/2
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 101 times.
272 for (i = 0; i < ifile.fmt_ctx->nb_streams; i++) {
3109
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 139 times.
171 if (stream_specifier) {
3110 32 ret = avformat_match_stream_specifier(ifile.fmt_ctx,
3111 32 ifile.fmt_ctx->streams[i],
3112 stream_specifier);
3113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 CHECK_END;
3114 else
3115 32 selected_streams[i] = ret;
3116 32 ret = 0;
3117 } else {
3118 139 selected_streams[i] = 1;
3119 }
3120
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 163 times.
171 if (!selected_streams[i])
3121 8 ifile.fmt_ctx->streams[i]->discard = AVDISCARD_ALL;
3122 }
3123
3124
4/4
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 40 times.
101 if (do_read_frames || do_read_packets) {
3125
4/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 27 times.
61 if (do_show_frames && do_show_packets &&
3126
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6 times.
11 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
3127 5 section_id = SECTION_ID_PACKETS_AND_FRAMES;
3128
4/4
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 6 times.
56 else if (do_show_packets && !do_show_frames)
3129 23 section_id = SECTION_ID_PACKETS;
3130 else // (!do_show_packets && do_show_frames)
3131 33 section_id = SECTION_ID_FRAMES;
3132
3/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
61 if (do_show_frames || do_show_packets)
3133 61 writer_print_section_header(wctx, section_id);
3134 61 ret = read_packets(wctx, &ifile);
3135
3/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
61 if (do_show_frames || do_show_packets)
3136 61 writer_print_section_footer(wctx);
3137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 CHECK_END;
3138 }
3139
3140
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 63 times.
101 if (do_show_programs) {
3141 38 ret = show_programs(wctx, &ifile);
3142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 CHECK_END;
3143 }
3144
3145
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 37 times.
101 if (do_show_streams) {
3146 64 ret = show_streams(wctx, &ifile);
3147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 CHECK_END;
3148 }
3149
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 97 times.
101 if (do_show_chapters) {
3150 4 ret = show_chapters(wctx, &ifile);
3151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 CHECK_END;
3152 }
3153
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 31 times.
101 if (do_show_format) {
3154 31 ret = show_format(wctx, &ifile);
3155
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 CHECK_END;
3156 }
3157
3158 101 end:
3159
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 if (ifile.fmt_ctx)
3160 101 close_input_file(&ifile);
3161 101 av_freep(&nb_streams_frames);
3162 101 av_freep(&nb_streams_packets);
3163 101 av_freep(&selected_streams);
3164
3165 101 return ret;
3166 }
3167
3168 static void show_usage(void)
3169 {
3170 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
3171 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
3172 av_log(NULL, AV_LOG_INFO, "\n");
3173 }
3174
3175 static void ffprobe_show_program_version(WriterContext *w)
3176 {
3177 AVBPrint pbuf;
3178 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
3179
3180 writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
3181 print_str("version", FFMPEG_VERSION);
3182 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
3183 program_birth_year, CONFIG_THIS_YEAR);
3184 print_str("compiler_ident", CC_IDENT);
3185 print_str("configuration", FFMPEG_CONFIGURATION);
3186 writer_print_section_footer(w);
3187
3188 av_bprint_finalize(&pbuf, NULL);
3189 }
3190
3191 #define SHOW_LIB_VERSION(libname, LIBNAME) \
3192 do { \
3193 if (CONFIG_##LIBNAME) { \
3194 unsigned int version = libname##_version(); \
3195 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
3196 print_str("name", "lib" #libname); \
3197 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
3198 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
3199 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
3200 print_int("version", version); \
3201 print_str("ident", LIB##LIBNAME##_IDENT); \
3202 writer_print_section_footer(w); \
3203 } \
3204 } while (0)
3205
3206 static void ffprobe_show_library_versions(WriterContext *w)
3207 {
3208 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSIONS);
3209 SHOW_LIB_VERSION(avutil, AVUTIL);
3210 SHOW_LIB_VERSION(avcodec, AVCODEC);
3211 SHOW_LIB_VERSION(avformat, AVFORMAT);
3212 SHOW_LIB_VERSION(avdevice, AVDEVICE);
3213 SHOW_LIB_VERSION(avfilter, AVFILTER);
3214 SHOW_LIB_VERSION(swscale, SWSCALE);
3215 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
3216 SHOW_LIB_VERSION(postproc, POSTPROC);
3217 writer_print_section_footer(w);
3218 }
3219
3220 #define PRINT_PIX_FMT_FLAG(flagname, name) \
3221 do { \
3222 print_int(name, !!(pixdesc->flags & AV_PIX_FMT_FLAG_##flagname)); \
3223 } while (0)
3224
3225 static void ffprobe_show_pixel_formats(WriterContext *w)
3226 {
3227 const AVPixFmtDescriptor *pixdesc = NULL;
3228 int i, n;
3229
3230 writer_print_section_header(w, SECTION_ID_PIXEL_FORMATS);
3231 while (pixdesc = av_pix_fmt_desc_next(pixdesc)) {
3232 writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT);
3233 print_str("name", pixdesc->name);
3234 print_int("nb_components", pixdesc->nb_components);
3235 if ((pixdesc->nb_components >= 3) && !(pixdesc->flags & AV_PIX_FMT_FLAG_RGB)) {
3236 print_int ("log2_chroma_w", pixdesc->log2_chroma_w);
3237 print_int ("log2_chroma_h", pixdesc->log2_chroma_h);
3238 } else {
3239 print_str_opt("log2_chroma_w", "N/A");
3240 print_str_opt("log2_chroma_h", "N/A");
3241 }
3242 n = av_get_bits_per_pixel(pixdesc);
3243 if (n) print_int ("bits_per_pixel", n);
3244 else print_str_opt("bits_per_pixel", "N/A");
3245 if (do_show_pixel_format_flags) {
3246 writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_FLAGS);
3247 PRINT_PIX_FMT_FLAG(BE, "big_endian");
3248 PRINT_PIX_FMT_FLAG(PAL, "palette");
3249 PRINT_PIX_FMT_FLAG(BITSTREAM, "bitstream");
3250 PRINT_PIX_FMT_FLAG(HWACCEL, "hwaccel");
3251 PRINT_PIX_FMT_FLAG(PLANAR, "planar");
3252 PRINT_PIX_FMT_FLAG(RGB, "rgb");
3253 PRINT_PIX_FMT_FLAG(ALPHA, "alpha");
3254 writer_print_section_footer(w);
3255 }
3256 if (do_show_pixel_format_components && (pixdesc->nb_components > 0)) {
3257 writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_COMPONENTS);
3258 for (i = 0; i < pixdesc->nb_components; i++) {
3259 writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_COMPONENT);
3260 print_int("index", i + 1);
3261 print_int("bit_depth", pixdesc->comp[i].depth);
3262 writer_print_section_footer(w);
3263 }
3264 writer_print_section_footer(w);
3265 }
3266 writer_print_section_footer(w);
3267 }
3268 writer_print_section_footer(w);
3269 }
3270
3271 static int opt_show_optional_fields(void *optctx, const char *opt, const char *arg)
3272 {
3273 if (!av_strcasecmp(arg, "always")) show_optional_fields = SHOW_OPTIONAL_FIELDS_ALWAYS;
3274 else if (!av_strcasecmp(arg, "never")) show_optional_fields = SHOW_OPTIONAL_FIELDS_NEVER;
3275 else if (!av_strcasecmp(arg, "auto")) show_optional_fields = SHOW_OPTIONAL_FIELDS_AUTO;
3276
3277 if (show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO && av_strcasecmp(arg, "auto"))
3278 show_optional_fields = parse_number_or_die("show_optional_fields", arg, OPT_INT, SHOW_OPTIONAL_FIELDS_AUTO, SHOW_OPTIONAL_FIELDS_ALWAYS);
3279 return 0;
3280 }
3281
3282 12 static int opt_format(void *optctx, const char *opt, const char *arg)
3283 {
3284 12 iformat = av_find_input_format(arg);
3285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!iformat) {
3286 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
3287 return AVERROR(EINVAL);
3288 }
3289 12 return 0;
3290 }
3291
3292 713 static inline void mark_section_show_entries(SectionID section_id,
3293 int show_all_entries, AVDictionary *entries)
3294 {
3295 713 struct section *section = &sections[section_id];
3296
3297 713 section->show_all_entries = show_all_entries;
3298
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 111 times.
713 if (show_all_entries) {
3299 SectionID *id;
3300
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 602 times.
1065 for (id = section->children_ids; *id != -1; id++)
3301 463 mark_section_show_entries(*id, show_all_entries, entries);
3302 } else {
3303 111 av_dict_copy(&section->entries_to_show, entries, 0);
3304 }
3305 713 }
3306
3307 125 static int match_section(const char *section_name,
3308 int show_all_entries, AVDictionary *entries)
3309 {
3310 125 int i, ret = 0;
3311
3312
2/2
✓ Branch 0 taken 5500 times.
✓ Branch 1 taken 125 times.
5625 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
3313 5500 const struct section *section = &sections[i];
3314
2/2
✓ Branch 0 taken 5381 times.
✓ Branch 1 taken 119 times.
5500 if (!strcmp(section_name, section->name) ||
3315
4/4
✓ Branch 0 taken 1962 times.
✓ Branch 1 taken 3419 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 1916 times.
5381 (section->unique_name && !strcmp(section_name, section->unique_name))) {
3316 165 av_log(NULL, AV_LOG_DEBUG,
3317 "'%s' matches section with unique name '%s'\n", section_name,
3318 165 (char *)av_x_if_null(section->unique_name, section->name));
3319 165 ret++;
3320 165 mark_section_show_entries(section->id, show_all_entries, entries);
3321 }
3322 }
3323 125 return ret;
3324 }
3325
3326 66 static int opt_show_entries(void *optctx, const char *opt, const char *arg)
3327 {
3328 66 const char *p = arg;
3329 66 int ret = 0;
3330
3331
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 66 times.
191 while (*p) {
3332 125 AVDictionary *entries = NULL;
3333 125 char *section_name = av_get_token(&p, "=:");
3334 125 int show_all_entries = 0;
3335
3336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (!section_name) {
3337 av_log(NULL, AV_LOG_ERROR,
3338 "Missing section name for option '%s'\n", opt);
3339 return AVERROR(EINVAL);
3340 }
3341
3342
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 48 times.
125 if (*p == '=') {
3343 77 p++;
3344
4/4
✓ Branch 0 taken 209 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 169 times.
✓ Branch 3 taken 40 times.
246 while (*p && *p != ':') {
3345 169 char *entry = av_get_token(&p, ",:");
3346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 169 times.
169 if (!entry)
3347 break;
3348 169 av_log(NULL, AV_LOG_VERBOSE,
3349 "Adding '%s' to the entries to show in section '%s'\n",
3350 entry, section_name);
3351 169 av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
3352
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 77 times.
169 if (*p == ',')
3353 92 p++;
3354 }
3355 } else {
3356 48 show_all_entries = 1;
3357 }
3358
3359 125 ret = match_section(section_name, show_all_entries, entries);
3360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (ret == 0) {
3361 av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
3362 ret = AVERROR(EINVAL);
3363 }
3364 125 av_dict_free(&entries);
3365 125 av_free(section_name);
3366
3367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (ret <= 0)
3368 break;
3369
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 66 times.
125 if (*p)
3370 59 p++;
3371 }
3372
3373 66 return ret;
3374 }
3375
3376 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
3377 {
3378 char *buf = av_asprintf("format=%s", arg);
3379 int ret;
3380
3381 if (!buf)
3382 return AVERROR(ENOMEM);
3383
3384 av_log(NULL, AV_LOG_WARNING,
3385 "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
3386 opt, arg);
3387 ret = opt_show_entries(optctx, opt, buf);
3388 av_free(buf);
3389 return ret;
3390 }
3391
3392 101 static void opt_input_file(void *optctx, const char *arg)
3393 {
3394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (input_filename) {
3395 av_log(NULL, AV_LOG_ERROR,
3396 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3397 arg, input_filename);
3398 exit_program(1);
3399 }
3400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (!strcmp(arg, "-"))
3401 arg = "pipe:";
3402 101 input_filename = arg;
3403 101 }
3404
3405 9 static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
3406 {
3407 9 opt_input_file(optctx, arg);
3408 9 return 0;
3409 }
3410
3411 14 static int opt_print_filename(void *optctx, const char *opt, const char *arg)
3412 {
3413 14 print_input_filename = arg;
3414 14 return 0;
3415 }