~ppsspp/ppsspp/ffmpeg

« back to all changes in this revision

Viewing changes to ffprobe.c

  • Committer: Henrik Rydgård
  • Date: 2014-01-03 10:44:32 UTC
  • Revision ID: git-v1:87c6c126784b1718bfa448ecf2e6a9fef781eb4e
Update our ffmpeg snapshot to a clone of the official repository.

This is because Maxim's at3plus support has been officially merged!

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 */
25
25
 
26
26
#include "config.h"
27
 
#include "version.h"
 
27
#include "libavutil/ffversion.h"
28
28
 
29
29
#include <string.h>
30
30
 
66
66
static int do_show_program_version  = 0;
67
67
static int do_show_library_versions = 0;
68
68
 
 
69
static int do_show_chapter_tags = 0;
 
70
static int do_show_format_tags = 0;
 
71
static int do_show_frame_tags = 0;
 
72
static int do_show_program_tags = 0;
 
73
static int do_show_stream_tags = 0;
 
74
 
69
75
static int show_value_unit              = 0;
70
76
static int use_value_prefix             = 0;
71
77
static int use_byte_value_binary_prefix = 0;
135
141
    SECTION_ID_STREAM_DISPOSITION,
136
142
    SECTION_ID_STREAMS,
137
143
    SECTION_ID_STREAM_TAGS,
 
144
    SECTION_ID_SUBTITLE,
138
145
} SectionID;
139
146
 
140
147
static struct section sections[] = {
144
151
    [SECTION_ID_ERROR] =              { SECTION_ID_ERROR, "error", 0, { -1 } },
145
152
    [SECTION_ID_FORMAT] =             { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
146
153
    [SECTION_ID_FORMAT_TAGS] =        { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
147
 
    [SECTION_ID_FRAMES] =             { SECTION_ID_FRAMES, "frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME, -1 } },
 
154
    [SECTION_ID_FRAMES] =             { SECTION_ID_FRAMES, "frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME, SECTION_ID_SUBTITLE, -1 } },
148
155
    [SECTION_ID_FRAME] =              { SECTION_ID_FRAME, "frame", 0, { SECTION_ID_FRAME_TAGS, -1 } },
149
156
    [SECTION_ID_FRAME_TAGS] =         { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
150
157
    [SECTION_ID_LIBRARY_VERSIONS] =   { SECTION_ID_LIBRARY_VERSIONS, "library_versions", SECTION_FLAG_IS_ARRAY, { SECTION_ID_LIBRARY_VERSION, -1 } },
167
174
    [SECTION_ID_STREAM] =             { SECTION_ID_STREAM, "stream", 0, { SECTION_ID_STREAM_DISPOSITION, SECTION_ID_STREAM_TAGS, -1 } },
168
175
    [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
169
176
    [SECTION_ID_STREAM_TAGS] =        { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
 
177
    [SECTION_ID_SUBTITLE] =           { SECTION_ID_SUBTITLE, "subtitle", 0, { -1 } },
170
178
};
171
179
 
172
180
static const OptionDef *options;
258
266
#define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
259
267
#define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
260
268
 
 
269
typedef enum {
 
270
    WRITER_STRING_VALIDATION_FAIL,
 
271
    WRITER_STRING_VALIDATION_REPLACE,
 
272
    WRITER_STRING_VALIDATION_IGNORE,
 
273
    WRITER_STRING_VALIDATION_NB
 
274
} StringValidation;
 
275
 
261
276
typedef struct Writer {
262
277
    const AVClass *priv_class;      ///< private class of the writer, if any
263
278
    int priv_size;                  ///< private size for the writer context
298
313
    unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
299
314
    unsigned int nb_section_frame;  ///< number of the frame  section in case we are in "packets_and_frames" section
300
315
    unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
 
316
 
 
317
    StringValidation string_validation;
 
318
    char *string_validation_replacement;
 
319
    unsigned int string_validation_utf8_flags;
301
320
};
302
321
 
303
322
static const char *writer_get_name(void *p)
306
325
    return wctx->writer->name;
307
326
}
308
327
 
 
328
#define OFFSET(x) offsetof(WriterContext, x)
 
329
 
 
330
static const AVOption writer_options[] = {
 
331
    { "string_validation", "set string validation mode",
 
332
      OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
 
333
    { "sv", "set string validation mode",
 
334
      OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
 
335
    { "ignore",  NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_IGNORE},  .unit = "sv" },
 
336
    { "replace", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_REPLACE}, .unit = "sv" },
 
337
    { "fail",    NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_FAIL},    .unit = "sv" },
 
338
    { "string_validation_replacement", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str=""}},
 
339
    { "svr", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str=""}},
 
340
    { NULL }
 
341
};
 
342
 
 
343
static void *writer_child_next(void *obj, void *prev)
 
344
{
 
345
    WriterContext *ctx = obj;
 
346
    if (!prev && ctx->writer && ctx->writer->priv_class && ctx->priv)
 
347
        return ctx->priv;
 
348
    return NULL;
 
349
}
 
350
 
309
351
static const AVClass writer_class = {
310
 
    "Writer",
311
 
    writer_get_name,
312
 
    NULL,
313
 
    LIBAVUTIL_VERSION_INT,
 
352
    .class_name = "Writer",
 
353
    .item_name  = writer_get_name,
 
354
    .option     = writer_options,
 
355
    .version    = LIBAVUTIL_VERSION_INT,
 
356
    .child_next = writer_child_next,
314
357
};
315
358
 
316
359
static void writer_close(WriterContext **wctx)
327
370
    if ((*wctx)->writer->priv_class)
328
371
        av_opt_free((*wctx)->priv);
329
372
    av_freep(&((*wctx)->priv));
 
373
    av_opt_free(*wctx);
330
374
    av_freep(wctx);
331
375
}
332
376
 
 
377
static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size)
 
378
{
 
379
    int i;
 
380
    av_bprintf(bp, "0X");
 
381
    for (i = 0; i < ubuf_size; i++)
 
382
        av_bprintf(bp, "%02X", ubuf[i]);
 
383
}
 
384
 
 
385
 
333
386
static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
334
387
                       const struct section *sections, int nb_sections)
335
388
{
351
404
    (*wctx)->sections = sections;
352
405
    (*wctx)->nb_sections = nb_sections;
353
406
 
 
407
    av_opt_set_defaults(*wctx);
 
408
 
354
409
    if (writer->priv_class) {
355
410
        void *priv_ctx = (*wctx)->priv;
356
411
        *((const AVClass **)priv_ctx) = writer->priv_class;
357
412
        av_opt_set_defaults(priv_ctx);
358
 
 
359
 
        if (args &&
360
 
            (ret = av_set_options_string(priv_ctx, args, "=", ":")) < 0)
 
413
    }
 
414
 
 
415
    /* convert options to dictionary */
 
416
    if (args) {
 
417
        AVDictionary *opts = NULL;
 
418
        AVDictionaryEntry *opt = NULL;
 
419
 
 
420
        if ((ret = av_dict_parse_string(&opts, args, "=", ":", 0)) < 0) {
 
421
            av_log(*wctx, AV_LOG_ERROR, "Failed to parse option string '%s' provided to writer context\n", args);
 
422
            av_dict_free(&opts);
361
423
            goto fail;
 
424
        }
 
425
 
 
426
        while ((opt = av_dict_get(opts, "", opt, AV_DICT_IGNORE_SUFFIX))) {
 
427
            if ((ret = av_opt_set(*wctx, opt->key, opt->value, AV_OPT_SEARCH_CHILDREN)) < 0) {
 
428
                av_log(*wctx, AV_LOG_ERROR, "Failed to set option '%s' with value '%s' provided to writer context\n",
 
429
                       opt->key, opt->value);
 
430
                av_dict_free(&opts);
 
431
                goto fail;
 
432
            }
 
433
        }
 
434
 
 
435
        av_dict_free(&opts);
 
436
    }
 
437
 
 
438
    /* validate replace string */
 
439
    {
 
440
        const uint8_t *p = (*wctx)->string_validation_replacement;
 
441
        const uint8_t *endp = p + strlen(p);
 
442
        while (*p) {
 
443
            const uint8_t *p0 = p;
 
444
            int32_t code;
 
445
            ret = av_utf8_decode(&code, &p, endp, (*wctx)->string_validation_utf8_flags);
 
446
            if (ret < 0) {
 
447
                AVBPrint bp;
 
448
                av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
 
449
                bprint_bytes(&bp, p0, p-p0),
 
450
                    av_log(wctx, AV_LOG_ERROR,
 
451
                           "Invalid UTF8 sequence %s found in string validation replace '%s'\n",
 
452
                           bp.str, (*wctx)->string_validation_replacement);
 
453
                return ret;
 
454
            }
 
455
        }
362
456
    }
363
457
 
364
458
    for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
428
522
    }
429
523
}
430
524
 
431
 
static inline void writer_print_string(WriterContext *wctx,
432
 
                                       const char *key, const char *val, int opt)
 
525
static inline int validate_string(WriterContext *wctx, char **dstp, const char *src)
 
526
{
 
527
    const uint8_t *p, *endp;
 
528
    AVBPrint dstbuf;
 
529
    int invalid_chars_nb = 0, ret = 0;
 
530
 
 
531
    av_bprint_init(&dstbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
 
532
 
 
533
    endp = src + strlen(src);
 
534
    for (p = (uint8_t *)src; *p;) {
 
535
        uint32_t code;
 
536
        int invalid = 0;
 
537
        const uint8_t *p0 = p;
 
538
 
 
539
        if (av_utf8_decode(&code, &p, endp, wctx->string_validation_utf8_flags) < 0) {
 
540
            AVBPrint bp;
 
541
            av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
 
542
            bprint_bytes(&bp, p0, p-p0);
 
543
            av_log(wctx, AV_LOG_DEBUG,
 
544
                   "Invalid UTF-8 sequence %s found in string '%s'\n", bp.str, src);
 
545
            invalid = 1;
 
546
        }
 
547
 
 
548
        if (invalid) {
 
549
            invalid_chars_nb++;
 
550
 
 
551
            switch (wctx->string_validation) {
 
552
            case WRITER_STRING_VALIDATION_FAIL:
 
553
                av_log(wctx, AV_LOG_ERROR,
 
554
                       "Invalid UTF-8 sequence found in string '%s'\n", src);
 
555
                ret = AVERROR_INVALIDDATA;
 
556
                goto end;
 
557
                break;
 
558
 
 
559
            case WRITER_STRING_VALIDATION_REPLACE:
 
560
                av_bprintf(&dstbuf, "%s", wctx->string_validation_replacement);
 
561
                break;
 
562
            }
 
563
        }
 
564
 
 
565
        if (!invalid || wctx->string_validation == WRITER_STRING_VALIDATION_IGNORE)
 
566
            av_bprint_append_data(&dstbuf, p0, p-p0);
 
567
    }
 
568
 
 
569
    if (invalid_chars_nb && wctx->string_validation == WRITER_STRING_VALIDATION_REPLACE) {
 
570
        av_log(wctx, AV_LOG_WARNING,
 
571
               "%d invalid UTF-8 sequence(s) found in string '%s', replaced with '%s'\n",
 
572
               invalid_chars_nb, src, wctx->string_validation_replacement);
 
573
    }
 
574
 
 
575
end:
 
576
    av_bprint_finalize(&dstbuf, dstp);
 
577
    return ret;
 
578
}
 
579
 
 
580
#define PRINT_STRING_OPT      1
 
581
#define PRINT_STRING_VALIDATE 2
 
582
 
 
583
static inline int writer_print_string(WriterContext *wctx,
 
584
                                      const char *key, const char *val, int flags)
433
585
{
434
586
    const struct section *section = wctx->section[wctx->level];
 
587
    int ret = 0;
435
588
 
436
 
    if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
437
 
        return;
 
589
    if ((flags & PRINT_STRING_OPT)
 
590
        && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
 
591
        return 0;
438
592
 
439
593
    if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
440
 
        wctx->writer->print_string(wctx, key, val);
 
594
        if (flags & PRINT_STRING_VALIDATE) {
 
595
            char *key1 = NULL, *val1 = NULL;
 
596
            ret = validate_string(wctx, &key1, key);
 
597
            if (ret < 0) goto end;
 
598
            ret = validate_string(wctx, &val1, val);
 
599
            if (ret < 0) goto end;
 
600
            wctx->writer->print_string(wctx, key1, val1);
 
601
        end:
 
602
            if (ret < 0) {
 
603
                av_log(wctx, AV_LOG_ERROR,
 
604
                       "Invalid key=value string combination %s=%s in section %s\n",
 
605
                       key, val, section->unique_name);
 
606
            }
 
607
            av_free(key1);
 
608
            av_free(val1);
 
609
        } else {
 
610
            wctx->writer->print_string(wctx, key, val);
 
611
        }
 
612
 
441
613
        wctx->nb_item[wctx->level]++;
442
614
    }
 
615
 
 
616
    return ret;
443
617
}
444
618
 
445
619
static inline void writer_print_rational(WriterContext *wctx,
457
631
    char buf[128];
458
632
 
459
633
    if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
460
 
        writer_print_string(wctx, key, "N/A", 1);
 
634
        writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
461
635
    } else {
462
636
        double d = ts * av_q2d(*time_base);
463
637
        struct unit_value uv;
471
645
static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
472
646
{
473
647
    if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
474
 
        writer_print_string(wctx, key, "N/A", 1);
 
648
        writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
475
649
    } else {
476
650
        writer_print_integer(wctx, key, ts);
477
651
    }
540
714
    return #name ;                                  \
541
715
}                                                   \
542
716
static const AVClass name##_class = {               \
543
 
    #name,                                          \
544
 
    name##_get_name,                                \
545
 
    name##_options                                  \
 
717
    .class_name = #name,                            \
 
718
    .item_name  = name##_get_name,                  \
 
719
    .option     = name##_options                    \
546
720
}
547
721
 
548
722
/* Default output */
554
728
    int nested_section[SECTION_MAX_NB_LEVELS];
555
729
} DefaultContext;
556
730
 
 
731
#undef OFFSET
557
732
#define OFFSET(x) offsetof(DefaultContext, x)
558
733
 
559
734
static const AVOption default_options[] = {
1440
1615
#define print_int(k, v)         writer_print_integer(w, k, v)
1441
1616
#define print_q(k, v, s)        writer_print_rational(w, k, v, s)
1442
1617
#define print_str(k, v)         writer_print_string(w, k, v, 0)
1443
 
#define print_str_opt(k, v)     writer_print_string(w, k, v, 1)
 
1618
#define print_str_opt(k, v)     writer_print_string(w, k, v, PRINT_STRING_OPT)
 
1619
#define print_str_validate(k, v) writer_print_string(w, k, v, PRINT_STRING_VALIDATE)
1444
1620
#define print_time(k, v, tb)    writer_print_time(w, k, v, tb, 0)
1445
1621
#define print_ts(k, v)          writer_print_ts(w, k, v, 0)
1446
1622
#define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1455
1631
#define print_section_header(s) writer_print_section_header(w, s)
1456
1632
#define print_section_footer(s) writer_print_section_footer(w, s)
1457
1633
 
1458
 
static inline void show_tags(WriterContext *wctx, AVDictionary *tags, int section_id)
 
1634
static inline int show_tags(WriterContext *w, AVDictionary *tags, int section_id)
1459
1635
{
1460
1636
    AVDictionaryEntry *tag = NULL;
 
1637
    int ret = 0;
1461
1638
 
1462
1639
    if (!tags)
1463
 
        return;
1464
 
    writer_print_section_header(wctx, section_id);
1465
 
    while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX)))
1466
 
        writer_print_string(wctx, tag->key, tag->value, 0);
1467
 
    writer_print_section_footer(wctx);
 
1640
        return 0;
 
1641
    writer_print_section_header(w, section_id);
 
1642
 
 
1643
    while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX))) {
 
1644
        if ((ret = print_str_validate(tag->key, tag->value)) < 0)
 
1645
            break;
 
1646
    }
 
1647
    writer_print_section_footer(w);
 
1648
 
 
1649
    return ret;
1468
1650
}
1469
1651
 
1470
1652
static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1502
1684
    fflush(stdout);
1503
1685
}
1504
1686
 
 
1687
static void show_subtitle(WriterContext *w, AVSubtitle *sub, AVStream *stream,
 
1688
                          AVFormatContext *fmt_ctx)
 
1689
{
 
1690
    AVBPrint pbuf;
 
1691
 
 
1692
    av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
 
1693
 
 
1694
    writer_print_section_header(w, SECTION_ID_SUBTITLE);
 
1695
 
 
1696
    print_str ("media_type",         "subtitle");
 
1697
    print_ts  ("pts",                 sub->pts);
 
1698
    print_time("pts_time",            sub->pts, &AV_TIME_BASE_Q);
 
1699
    print_int ("format",              sub->format);
 
1700
    print_int ("start_display_time",  sub->start_display_time);
 
1701
    print_int ("end_display_time",    sub->end_display_time);
 
1702
    print_int ("num_rects",           sub->num_rects);
 
1703
 
 
1704
    writer_print_section_footer(w);
 
1705
 
 
1706
    av_bprint_finalize(&pbuf, NULL);
 
1707
    fflush(stdout);
 
1708
}
 
1709
 
1505
1710
static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
1506
1711
                       AVFormatContext *fmt_ctx)
1507
1712
{
1520
1725
    print_time("pkt_pts_time",          frame->pkt_pts, &stream->time_base);
1521
1726
    print_ts  ("pkt_dts",               frame->pkt_dts);
1522
1727
    print_time("pkt_dts_time",          frame->pkt_dts, &stream->time_base);
 
1728
    print_ts  ("best_effort_timestamp", av_frame_get_best_effort_timestamp(frame));
 
1729
    print_time("best_effort_timestamp_time", av_frame_get_best_effort_timestamp(frame), &stream->time_base);
1523
1730
    print_duration_ts  ("pkt_duration",      av_frame_get_pkt_duration(frame));
1524
1731
    print_duration_time("pkt_duration_time", av_frame_get_pkt_duration(frame), &stream->time_base);
1525
1732
    if (av_frame_get_pkt_pos (frame) != -1) print_fmt    ("pkt_pos", "%"PRId64, av_frame_get_pkt_pos(frame));
1565
1772
            print_str_opt("channel_layout", "unknown");
1566
1773
        break;
1567
1774
    }
1568
 
    show_tags(w, av_frame_get_metadata(frame), SECTION_ID_FRAME_TAGS);
 
1775
    if (do_show_frame_tags)
 
1776
        show_tags(w, av_frame_get_metadata(frame), SECTION_ID_FRAME_TAGS);
1569
1777
 
1570
1778
    writer_print_section_footer(w);
1571
1779
 
1578
1786
                                          AVFrame *frame, AVPacket *pkt)
1579
1787
{
1580
1788
    AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
 
1789
    AVSubtitle sub;
1581
1790
    int ret = 0, got_frame = 0;
1582
1791
 
1583
 
    avcodec_get_frame_defaults(frame);
1584
1792
    if (dec_ctx->codec) {
1585
1793
        switch (dec_ctx->codec_type) {
1586
1794
        case AVMEDIA_TYPE_VIDEO:
1590
1798
        case AVMEDIA_TYPE_AUDIO:
1591
1799
            ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
1592
1800
            break;
 
1801
 
 
1802
        case AVMEDIA_TYPE_SUBTITLE:
 
1803
            ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_frame, pkt);
 
1804
            break;
1593
1805
        }
1594
1806
    }
1595
1807
 
1599
1811
    pkt->data += ret;
1600
1812
    pkt->size -= ret;
1601
1813
    if (got_frame) {
 
1814
        int is_sub = (dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE);
1602
1815
        nb_streams_frames[pkt->stream_index]++;
1603
1816
        if (do_show_frames)
1604
 
            show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
 
1817
            if (is_sub)
 
1818
                show_subtitle(w, &sub, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
 
1819
            else
 
1820
                show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
 
1821
        if (is_sub)
 
1822
            avsubtitle_free(&sub);
1605
1823
    }
1606
1824
    return got_frame;
1607
1825
}
1634
1852
                                 const ReadInterval *interval, int64_t *cur_ts)
1635
1853
{
1636
1854
    AVPacket pkt, pkt1;
1637
 
    AVFrame frame;
 
1855
    AVFrame *frame = NULL;
1638
1856
    int ret = 0, i = 0, frame_count = 0;
1639
1857
    int64_t start = -INT64_MAX, end = interval->end;
1640
1858
    int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
1668
1886
        }
1669
1887
    }
1670
1888
 
 
1889
    frame = av_frame_alloc();
 
1890
    if (!frame) {
 
1891
        ret = AVERROR(ENOMEM);
 
1892
        goto end;
 
1893
    }
1671
1894
    while (!av_read_frame(fmt_ctx, &pkt)) {
1672
1895
        if (selected_streams[pkt.stream_index]) {
1673
1896
            AVRational tb = fmt_ctx->streams[pkt.stream_index]->time_base;
1700
1923
            }
1701
1924
            if (do_read_frames) {
1702
1925
                pkt1 = pkt;
1703
 
                while (pkt1.size && process_frame(w, fmt_ctx, &frame, &pkt1) > 0);
 
1926
                while (pkt1.size && process_frame(w, fmt_ctx, frame, &pkt1) > 0);
1704
1927
            }
1705
1928
        }
1706
1929
        av_free_packet(&pkt);
1712
1935
    for (i = 0; i < fmt_ctx->nb_streams; i++) {
1713
1936
        pkt.stream_index = i;
1714
1937
        if (do_read_frames)
1715
 
            while (process_frame(w, fmt_ctx, &frame, &pkt) > 0);
 
1938
            while (process_frame(w, fmt_ctx, frame, &pkt) > 0);
1716
1939
    }
1717
1940
 
1718
1941
end:
 
1942
    av_frame_free(&frame);
1719
1943
    if (ret < 0) {
1720
1944
        av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
1721
1945
        log_read_interval(interval, NULL, AV_LOG_ERROR);
1723
1947
    return ret;
1724
1948
}
1725
1949
 
1726
 
static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
 
1950
static int read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1727
1951
{
1728
1952
    int i, ret = 0;
1729
1953
    int64_t cur_ts = fmt_ctx->start_time;
1738
1962
                break;
1739
1963
        }
1740
1964
    }
 
1965
 
 
1966
    return ret;
1741
1967
}
1742
1968
 
1743
 
static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, int in_program)
 
1969
static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, int in_program)
1744
1970
{
1745
1971
    AVStream *stream = fmt_ctx->streams[stream_idx];
1746
1972
    AVCodecContext *dec_ctx;
1749
1975
    const char *s;
1750
1976
    AVRational sar, dar;
1751
1977
    AVBPrint pbuf;
 
1978
    int ret = 0;
1752
1979
 
1753
1980
    av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1754
1981
 
1903
2130
    writer_print_section_footer(w);
1904
2131
    }
1905
2132
 
1906
 
    show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
 
2133
    if (do_show_stream_tags)
 
2134
        ret = show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
1907
2135
 
1908
2136
    writer_print_section_footer(w);
1909
2137
    av_bprint_finalize(&pbuf, NULL);
1910
2138
    fflush(stdout);
 
2139
 
 
2140
    return ret;
1911
2141
}
1912
2142
 
1913
 
static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
 
2143
static int show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
1914
2144
{
1915
 
    int i;
 
2145
    int i, ret = 0;
 
2146
 
1916
2147
    writer_print_section_header(w, SECTION_ID_STREAMS);
1917
2148
    for (i = 0; i < fmt_ctx->nb_streams; i++)
1918
 
        if (selected_streams[i])
1919
 
            show_stream(w, fmt_ctx, i, 0);
 
2149
        if (selected_streams[i]) {
 
2150
            ret = show_stream(w, fmt_ctx, i, 0);
 
2151
            if (ret < 0)
 
2152
                break;
 
2153
        }
1920
2154
    writer_print_section_footer(w);
 
2155
 
 
2156
    return ret;
1921
2157
}
1922
2158
 
1923
 
static void show_program(WriterContext *w, AVFormatContext *fmt_ctx, AVProgram *program)
 
2159
static int show_program(WriterContext *w, AVFormatContext *fmt_ctx, AVProgram *program)
1924
2160
{
1925
 
    int i;
 
2161
    int i, ret = 0;
1926
2162
 
1927
2163
    writer_print_section_header(w, SECTION_ID_PROGRAM);
1928
2164
    print_int("program_id", program->id);
1934
2170
    print_time("start_time", program->start_time, &AV_TIME_BASE_Q);
1935
2171
    print_ts("end_pts", program->end_time);
1936
2172
    print_time("end_time", program->end_time, &AV_TIME_BASE_Q);
1937
 
    show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
 
2173
    if (do_show_program_tags)
 
2174
        ret = show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
 
2175
    if (ret < 0)
 
2176
        goto end;
1938
2177
 
1939
2178
    writer_print_section_header(w, SECTION_ID_PROGRAM_STREAMS);
1940
2179
    for (i = 0; i < program->nb_stream_indexes; i++) {
1941
 
        if (selected_streams[program->stream_index[i]])
1942
 
            show_stream(w, fmt_ctx, program->stream_index[i], 1);
 
2180
        if (selected_streams[program->stream_index[i]]) {
 
2181
            ret = show_stream(w, fmt_ctx, program->stream_index[i], 1);
 
2182
            if (ret < 0)
 
2183
                break;
 
2184
        }
1943
2185
    }
1944
2186
    writer_print_section_footer(w);
1945
2187
 
 
2188
end:
1946
2189
    writer_print_section_footer(w);
 
2190
    return ret;
1947
2191
}
1948
2192
 
1949
 
static void show_programs(WriterContext *w, AVFormatContext *fmt_ctx)
 
2193
static int show_programs(WriterContext *w, AVFormatContext *fmt_ctx)
1950
2194
{
1951
 
    int i;
 
2195
    int i, ret = 0;
1952
2196
 
1953
2197
    writer_print_section_header(w, SECTION_ID_PROGRAMS);
1954
2198
    for (i = 0; i < fmt_ctx->nb_programs; i++) {
1955
2199
        AVProgram *program = fmt_ctx->programs[i];
1956
2200
        if (!program)
1957
2201
            continue;
1958
 
        show_program(w, fmt_ctx, program);
 
2202
        ret = show_program(w, fmt_ctx, program);
 
2203
        if (ret < 0)
 
2204
            break;
1959
2205
    }
1960
2206
    writer_print_section_footer(w);
 
2207
    return ret;
1961
2208
}
1962
2209
 
1963
 
static void show_chapters(WriterContext *w, AVFormatContext *fmt_ctx)
 
2210
static int show_chapters(WriterContext *w, AVFormatContext *fmt_ctx)
1964
2211
{
1965
 
    int i;
 
2212
    int i, ret = 0;
1966
2213
 
1967
2214
    writer_print_section_header(w, SECTION_ID_CHAPTERS);
1968
2215
    for (i = 0; i < fmt_ctx->nb_chapters; i++) {
1975
2222
        print_time("start_time", chapter->start, &chapter->time_base);
1976
2223
        print_int("end", chapter->end);
1977
2224
        print_time("end_time", chapter->end, &chapter->time_base);
1978
 
        show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
 
2225
        if (do_show_chapter_tags)
 
2226
            ret = show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
1979
2227
        writer_print_section_footer(w);
1980
2228
    }
1981
2229
    writer_print_section_footer(w);
 
2230
 
 
2231
    return ret;
1982
2232
}
1983
2233
 
1984
 
static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
 
2234
static int show_format(WriterContext *w, AVFormatContext *fmt_ctx)
1985
2235
{
1986
2236
    char val_str[128];
1987
2237
    int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
 
2238
    int ret = 0;
1988
2239
 
1989
2240
    writer_print_section_header(w, SECTION_ID_FORMAT);
1990
 
    print_str("filename",         fmt_ctx->filename);
 
2241
    print_str_validate("filename", fmt_ctx->filename);
1991
2242
    print_int("nb_streams",       fmt_ctx->nb_streams);
1992
2243
    print_int("nb_programs",      fmt_ctx->nb_programs);
1993
2244
    print_str("format_name",      fmt_ctx->iformat->name);
2002
2253
    if (fmt_ctx->bit_rate > 0) print_val    ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
2003
2254
    else                       print_str_opt("bit_rate", "N/A");
2004
2255
    print_int("probe_score", av_format_get_probe_score(fmt_ctx));
2005
 
    show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
 
2256
    if (do_show_format_tags)
 
2257
        ret = show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
2006
2258
 
2007
2259
    writer_print_section_footer(w);
2008
2260
    fflush(stdout);
 
2261
    return ret;
2009
2262
}
2010
2263
 
2011
2264
static void show_error(WriterContext *w, int err)
2111
2364
    if (ret < 0)
2112
2365
        return ret;
2113
2366
 
 
2367
#define CHECK_END if (ret < 0) goto end
 
2368
 
2114
2369
    nb_streams_frames  = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
2115
2370
    nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
2116
2371
    selected_streams   = av_calloc(fmt_ctx->nb_streams, sizeof(*selected_streams));
2120
2375
            ret = avformat_match_stream_specifier(fmt_ctx,
2121
2376
                                                  fmt_ctx->streams[i],
2122
2377
                                                  stream_specifier);
2123
 
            if (ret < 0)
2124
 
                goto end;
 
2378
            CHECK_END;
2125
2379
            else
2126
2380
                selected_streams[i] = ret;
2127
2381
            ret = 0;
2140
2394
            section_id = SECTION_ID_FRAMES;
2141
2395
        if (do_show_frames || do_show_packets)
2142
2396
            writer_print_section_header(wctx, section_id);
2143
 
        read_packets(wctx, fmt_ctx);
 
2397
        ret = read_packets(wctx, fmt_ctx);
2144
2398
        if (do_show_frames || do_show_packets)
2145
2399
            writer_print_section_footer(wctx);
2146
 
    }
2147
 
    if (do_show_programs)
2148
 
        show_programs(wctx, fmt_ctx);
2149
 
    if (do_show_streams)
2150
 
        show_streams(wctx, fmt_ctx);
2151
 
    if (do_show_chapters)
2152
 
        show_chapters(wctx, fmt_ctx);
2153
 
    if (do_show_format)
2154
 
        show_format(wctx, fmt_ctx);
 
2400
        CHECK_END;
 
2401
    }
 
2402
 
 
2403
    if (do_show_programs) {
 
2404
        ret = show_programs(wctx, fmt_ctx);
 
2405
        CHECK_END;
 
2406
    }
 
2407
 
 
2408
    if (do_show_streams) {
 
2409
        ret = show_streams(wctx, fmt_ctx);
 
2410
        CHECK_END;
 
2411
    }
 
2412
    if (do_show_chapters) {
 
2413
        ret = show_chapters(wctx, fmt_ctx);
 
2414
        CHECK_END;
 
2415
    }
 
2416
    if (do_show_format) {
 
2417
        ret = show_format(wctx, fmt_ctx);
 
2418
        CHECK_END;
 
2419
    }
2155
2420
 
2156
2421
end:
2157
2422
    close_input_file(&fmt_ctx);
2177
2442
    writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
2178
2443
    print_str("version", FFMPEG_VERSION);
2179
2444
    print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
2180
 
              program_birth_year, this_year);
 
2445
              program_birth_year, CONFIG_THIS_YEAR);
2181
2446
    print_str("build_date", __DATE__);
2182
2447
    print_str("build_time", __TIME__);
2183
2448
    print_str("compiler_ident", CC_IDENT);
2460
2725
 
2461
2726
    /* parse intervals */
2462
2727
    p = spec;
2463
 
    for (i = 0; i < n; i++) {
2464
 
        char *next = strchr(p, ',');
 
2728
    for (i = 0; p; i++) {
 
2729
        char *next;
 
2730
 
 
2731
        av_assert0(i < read_intervals_nb);
 
2732
        next = strchr(p, ',');
2465
2733
        if (next)
2466
2734
            *next++ = 0;
2467
2735
 
2475
2743
        av_log(NULL, AV_LOG_VERBOSE, "Parsed log interval ");
2476
2744
        log_read_interval(&read_intervals[i], NULL, AV_LOG_VERBOSE);
2477
2745
        p = next;
2478
 
        av_assert0(i <= read_intervals_nb);
2479
2746
    }
2480
2747
    av_assert0(i == read_intervals_nb);
2481
2748
 
2646
2913
    SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
2647
2914
    SET_DO_SHOW(PROGRAM_STREAM_DISPOSITION, stream_disposition);
2648
2915
 
 
2916
    SET_DO_SHOW(CHAPTER_TAGS, chapter_tags);
 
2917
    SET_DO_SHOW(FORMAT_TAGS, format_tags);
 
2918
    SET_DO_SHOW(FRAME_TAGS, frame_tags);
 
2919
    SET_DO_SHOW(PROGRAM_TAGS, program_tags);
 
2920
    SET_DO_SHOW(STREAM_TAGS, stream_tags);
 
2921
 
2649
2922
    if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
2650
2923
        av_log(NULL, AV_LOG_ERROR,
2651
2924
               "-bitexact and -show_program_version or -show_library_versions "
2674
2947
 
2675
2948
    if ((ret = writer_open(&wctx, w, w_args,
2676
2949
                           sections, FF_ARRAY_ELEMS(sections))) >= 0) {
 
2950
        if (w == &xml_writer)
 
2951
            wctx->string_validation_utf8_flags |= AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES;
 
2952
 
2677
2953
        writer_print_section_header(wctx, SECTION_ID_ROOT);
2678
2954
 
2679
2955
        if (do_show_program_version)