]> git.sesse.net Git - ffmpeg/blob - libavcodec/trace_headers_bsf.c
Merge commit '156ea66c91b1986a87916f187216978d686725f6'
[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 } TraceHeadersContext;
32
33
34 static int trace_headers_init(AVBSFContext *bsf)
35 {
36     TraceHeadersContext *ctx = bsf->priv_data;
37     int err;
38
39     err = ff_cbs_init(&ctx->cbc, bsf->par_in->codec_id, bsf);
40     if (err < 0)
41         return err;
42
43     ctx->cbc->trace_enable = 1;
44     ctx->cbc->trace_level  = AV_LOG_INFO;
45
46     if (bsf->par_in->extradata) {
47         CodedBitstreamFragment ps;
48
49         av_log(bsf, AV_LOG_INFO, "Extradata\n");
50
51         err = ff_cbs_read_extradata(ctx->cbc, &ps, bsf->par_in);
52
53         ff_cbs_fragment_uninit(ctx->cbc, &ps);
54     }
55
56     return err;
57 }
58
59 static void trace_headers_close(AVBSFContext *bsf)
60 {
61     TraceHeadersContext *ctx = bsf->priv_data;
62
63     ff_cbs_close(&ctx->cbc);
64 }
65
66 static int trace_headers(AVBSFContext *bsf, AVPacket *pkt)
67 {
68     TraceHeadersContext *ctx = bsf->priv_data;
69     CodedBitstreamFragment au;
70     char tmp[256] = { 0 };
71     int err;
72
73     err = ff_bsf_get_packet_ref(bsf, pkt);
74     if (err < 0)
75         return err;
76
77     if (pkt->flags & AV_PKT_FLAG_KEY)
78         av_strlcat(tmp, ", key frame", sizeof(tmp));
79     if (pkt->flags & AV_PKT_FLAG_CORRUPT)
80         av_strlcat(tmp, ", corrupt", sizeof(tmp));
81
82     if (pkt->pts != AV_NOPTS_VALUE)
83         av_strlcatf(tmp, sizeof(tmp), ", pts %"PRId64, pkt->pts);
84     else
85         av_strlcat(tmp, ", no pts", sizeof(tmp));
86     if (pkt->dts != AV_NOPTS_VALUE)
87         av_strlcatf(tmp, sizeof(tmp), ", dts %"PRId64, pkt->dts);
88     else
89         av_strlcat(tmp, ", no dts", sizeof(tmp));
90     if (pkt->duration > 0)
91         av_strlcatf(tmp, sizeof(tmp), ", duration %"PRId64, pkt->duration);
92
93     av_log(bsf, AV_LOG_INFO, "Packet: %d bytes%s.\n", pkt->size, tmp);
94
95     err = ff_cbs_read_packet(ctx->cbc, &au, pkt);
96
97     ff_cbs_fragment_uninit(ctx->cbc, &au);
98
99     if (err < 0)
100         av_packet_unref(pkt);
101     return err;
102 }
103
104 const AVBitStreamFilter ff_trace_headers_bsf = {
105     .name           = "trace_headers",
106     .priv_data_size = sizeof(TraceHeadersContext),
107     .init           = &trace_headers_init,
108     .close          = &trace_headers_close,
109     .filter         = &trace_headers,
110     .codec_ids      = ff_cbs_all_codec_ids,
111 };