| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2025 Romain Beauxis | ||
| 3 | * | ||
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 5 | * of this software and associated documentation files (the "Software"), to deal | ||
| 6 | * in the Software without restriction, including without limitation the rights | ||
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 8 | * copies of the Software, and to permit persons to whom the Software is | ||
| 9 | * furnished to do so, subject to the following conditions: | ||
| 10 | * | ||
| 11 | * The above copyright notice and this permission notice shall be included in | ||
| 12 | * all copies or substantial portions of the Software. | ||
| 13 | * | ||
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
| 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 20 | * THE SOFTWARE. | ||
| 21 | */ | ||
| 22 | |||
| 23 | /** | ||
| 24 | * Dump stream metadata | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "libavcodec/avcodec.h" | ||
| 28 | #include "libavformat/avformat.h" | ||
| 29 | #include "libavutil/timestamp.h" | ||
| 30 | |||
| 31 | 6 | static int dump_stream_meta(const char *input_filename) { | |
| 32 | 6 | const AVCodec *codec = NULL; | |
| 33 | 6 | AVPacket *pkt = NULL; | |
| 34 | 6 | AVFrame *fr = NULL; | |
| 35 | 6 | AVFormatContext *fmt_ctx = NULL; | |
| 36 | 6 | AVCodecContext *ctx = NULL; | |
| 37 | 6 | AVCodecParameters *origin_par = NULL; | |
| 38 | AVStream *st; | ||
| 39 | 6 | int stream_idx = 0; | |
| 40 | int result; | ||
| 41 | char *metadata; | ||
| 42 | |||
| 43 | 6 | result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL); | |
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (result < 0) { |
| 45 | ✗ | av_log(NULL, AV_LOG_ERROR, "Can't open file\n"); | |
| 46 | ✗ | return result; | |
| 47 | } | ||
| 48 | |||
| 49 | 6 | result = avformat_find_stream_info(fmt_ctx, NULL); | |
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (result < 0) { |
| 51 | ✗ | av_log(NULL, AV_LOG_ERROR, "Can't get stream info\n"); | |
| 52 | ✗ | goto end; | |
| 53 | } | ||
| 54 | |||
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (fmt_ctx->nb_streams > 1) { |
| 56 | ✗ | av_log(NULL, AV_LOG_ERROR, "More than one stream found in input!\n"); | |
| 57 | ✗ | goto end; | |
| 58 | } | ||
| 59 | |||
| 60 | 6 | origin_par = fmt_ctx->streams[stream_idx]->codecpar; | |
| 61 | 6 | st = fmt_ctx->streams[stream_idx]; | |
| 62 | |||
| 63 | 6 | result = av_dict_get_string(st->metadata, &metadata, '=', ':'); | |
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (result < 0) |
| 65 | ✗ | goto end; | |
| 66 | |||
| 67 | 6 | printf("Stream ID: %d, codec name: %s, metadata: %s\n", stream_idx, | |
| 68 | avcodec_get_name(origin_par->codec_id), | ||
| 69 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | strlen(metadata) ? metadata : "N/A"); |
| 70 | 6 | av_free(metadata); | |
| 71 | |||
| 72 | 6 | codec = avcodec_find_decoder(origin_par->codec_id); | |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!codec) { |
| 74 | ✗ | av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n"); | |
| 75 | ✗ | result = AVERROR_DECODER_NOT_FOUND; | |
| 76 | ✗ | goto end; | |
| 77 | } | ||
| 78 | |||
| 79 | 6 | ctx = avcodec_alloc_context3(codec); | |
| 80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!ctx) { |
| 81 | ✗ | av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n"); | |
| 82 | ✗ | result = AVERROR(ENOMEM); | |
| 83 | ✗ | goto end; | |
| 84 | } | ||
| 85 | |||
| 86 | 6 | result = avcodec_parameters_to_context(ctx, origin_par); | |
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (result) { |
| 88 | ✗ | av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n"); | |
| 89 | ✗ | goto end; | |
| 90 | } | ||
| 91 | |||
| 92 | 6 | result = avcodec_open2(ctx, codec, NULL); | |
| 93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (result < 0) { |
| 94 | ✗ | av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n"); | |
| 95 | ✗ | goto end; | |
| 96 | } | ||
| 97 | |||
| 98 | 6 | pkt = av_packet_alloc(); | |
| 99 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!pkt) { |
| 100 | ✗ | av_log(NULL, AV_LOG_ERROR, "Cannot allocate packet\n"); | |
| 101 | ✗ | result = AVERROR(ENOMEM); | |
| 102 | ✗ | goto end; | |
| 103 | } | ||
| 104 | |||
| 105 | 6 | fr = av_frame_alloc(); | |
| 106 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (!fr) { |
| 107 | ✗ | av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n"); | |
| 108 | ✗ | result = AVERROR(ENOMEM); | |
| 109 | ✗ | goto end; | |
| 110 | } | ||
| 111 | |||
| 112 | 44 | for (;;) { | |
| 113 | 50 | result = av_read_frame(fmt_ctx, pkt); | |
| 114 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 44 times.
|
50 | if (result) |
| 115 | 6 | goto end; | |
| 116 | |||
| 117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (pkt->stream_index != stream_idx) { |
| 118 | ✗ | av_packet_unref(pkt); | |
| 119 | ✗ | continue; | |
| 120 | } | ||
| 121 | |||
| 122 | 132 | printf("Stream ID: %d, packet PTS: %s, packet DTS: %s\n", | |
| 123 | 44 | pkt->stream_index, av_ts2str(pkt->pts), av_ts2str(pkt->dts)); | |
| 124 | |||
| 125 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 32 times.
|
44 | if (st->event_flags & AVSTREAM_EVENT_FLAG_METADATA_UPDATED) { |
| 126 | 12 | result = av_dict_get_string(st->metadata, &metadata, '=', ':'); | |
| 127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (result < 0) |
| 128 | ✗ | goto end; | |
| 129 | |||
| 130 | 12 | printf("Stream ID: %d, new metadata: %s\n", pkt->stream_index, | |
| 131 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | strlen(metadata) ? metadata : "N/A"); |
| 132 | 12 | av_free(metadata); | |
| 133 | |||
| 134 | 12 | st->event_flags &= ~AVSTREAM_EVENT_FLAG_METADATA_UPDATED; | |
| 135 | } | ||
| 136 | |||
| 137 | 44 | result = avcodec_send_packet(ctx, pkt); | |
| 138 | 44 | av_packet_unref(pkt); | |
| 139 | |||
| 140 |
1/2✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
|
44 | if (result < 0) |
| 141 | ✗ | goto end; | |
| 142 | |||
| 143 | 42 | do { | |
| 144 | 86 | result = avcodec_receive_frame(ctx, fr); | |
| 145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
|
86 | if (result == AVERROR_EOF) { |
| 146 | ✗ | result = 0; | |
| 147 | ✗ | goto end; | |
| 148 | } | ||
| 149 | |||
| 150 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 42 times.
|
86 | if (result == AVERROR(EAGAIN)) |
| 151 | 44 | break; | |
| 152 | |||
| 153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (result < 0) |
| 154 | ✗ | goto end; | |
| 155 | |||
| 156 | 42 | result = av_dict_get_string(fr->metadata, &metadata, '=', ':'); | |
| 157 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (result < 0) |
| 158 | ✗ | goto end; | |
| 159 | |||
| 160 | 42 | printf("Stream ID: %d, frame PTS: %s, metadata: %s\n", | |
| 161 | 42 | pkt->stream_index, av_ts2str(fr->pts), | |
| 162 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 36 times.
|
42 | strlen(metadata) ? metadata : "N/A"); |
| 163 | 42 | av_free(metadata); | |
| 164 | } while (1); | ||
| 165 | } | ||
| 166 | |||
| 167 | 6 | end: | |
| 168 | 6 | av_packet_free(&pkt); | |
| 169 | 6 | av_frame_free(&fr); | |
| 170 | 6 | avformat_close_input(&fmt_ctx); | |
| 171 | 6 | avcodec_free_context(&ctx); | |
| 172 | 6 | return result; | |
| 173 | } | ||
| 174 | |||
| 175 | 6 | int main(int argc, char **argv) { | |
| 176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (argc < 2) { |
| 177 | ✗ | av_log(NULL, AV_LOG_ERROR, "Incorrect input\n"); | |
| 178 | ✗ | return 1; | |
| 179 | } | ||
| 180 | |||
| 181 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (dump_stream_meta(argv[1]) != AVERROR_EOF) |
| 182 | ✗ | return 1; | |
| 183 | |||
| 184 | 6 | return 0; | |
| 185 | } | ||
| 186 |