]> git.sesse.net Git - ffmpeg/blob - libavcodec/trace_headers_bsf.c
Merge commit '7e42d5f0ab2aeac811fd01e122627c9198b13f01'
[ffmpeg] / libavcodec / trace_headers_bsf.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <stdio.h>
20
21 #include "libavutil/avstring.h"
22 #include "libavutil/common.h"
23 #include "libavutil/log.h"
24
25 #include "bsf.h"
26 #include "cbs.h"
27
28
29 typedef struct TraceHeadersContext {
30     CodedBitstreamContext *cbc;
31     CodedBitstreamFragment fragment;
32 } TraceHeadersContext;
33
34
35 static int trace_headers_init(AVBSFContext *bsf)
36 {
37     TraceHeadersContext *ctx = bsf->priv_data;
38     int err;
39
40     err = ff_cbs_init(&ctx->cbc, bsf->par_in->codec_id, bsf);
41     if (err < 0)
42         return err;
43
44     ctx->cbc->trace_enable = 1;
45     ctx->cbc->trace_level  = AV_LOG_INFO;
46
47     if (bsf->par_in->extradata) {
48         CodedBitstreamFragment *frag = &ctx->fragment;
49
50         av_log(bsf, AV_LOG_INFO, "Extradata\n");
51
52         err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in);
53
54         ff_cbs_fragment_reset(ctx->cbc, frag);
55     }
56
57     return err;
58 }
59
60 static void trace_headers_close(AVBSFContext *bsf)
61 {
62     TraceHeadersContext *ctx = bsf->priv_data;
63
64     ff_cbs_fragment_free(ctx->cbc, &ctx->fragment);
65     ff_cbs_close(&ctx->cbc);
66 }
67
68 static int trace_headers(AVBSFContext *bsf, AVPacket *pkt)
69 {
70     TraceHeadersContext *ctx = bsf->priv_data;
71     CodedBitstreamFragment *frag = &ctx->fragment;
72     char tmp[256] = { 0 };
73     int err;
74
75     err = ff_bsf_get_packet_ref(bsf, pkt);
76     if (err < 0)
77         return err;
78
79     if (pkt->flags & AV_PKT_FLAG_KEY)
80         av_strlcat(tmp, ", key frame", sizeof(tmp));
81     if (pkt->flags & AV_PKT_FLAG_CORRUPT)
82         av_strlcat(tmp, ", corrupt", sizeof(tmp));
83
84     if (pkt->pts != AV_NOPTS_VALUE)
85         av_strlcatf(tmp, sizeof(tmp), ", pts %"PRId64, pkt->pts);
86     else
87         av_strlcat(tmp, ", no pts", sizeof(tmp));
88     if (pkt->dts != AV_NOPTS_VALUE)
89         av_strlcatf(tmp, sizeof(tmp), ", dts %"PRId64, pkt->dts);
90     else
91         av_strlcat(tmp, ", no dts", sizeof(tmp));
92     if (pkt->duration > 0)
93         av_strlcatf(tmp, sizeof(tmp), ", duration %"PRId64, pkt->duration);
94
95     av_log(bsf, AV_LOG_INFO, "Packet: %d bytes%s.\n", pkt->size, tmp);
96
97     err = ff_cbs_read_packet(ctx->cbc, frag, pkt);
98
99     ff_cbs_fragment_reset(ctx->cbc, frag);
100
101     if (err < 0)
102         av_packet_unref(pkt);
103     return err;
104 }
105
106 const AVBitStreamFilter ff_trace_headers_bsf = {
107     .name           = "trace_headers",
108     .priv_data_size = sizeof(TraceHeadersContext),
109     .init           = &trace_headers_init,
110     .close          = &trace_headers_close,
111     .filter         = &trace_headers,
112     .codec_ids      = ff_cbs_all_codec_ids,
113 };