Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / 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 <inttypes.h>
20
21 #include "libavutil/avstring.h"
22 #include "libavutil/avutil.h"
23 #include "libavutil/log.h"
24
25 #include "bsf.h"
26 #include "bsf_internal.h"
27 #include "cbs.h"
28
29
30 typedef struct TraceHeadersContext {
31     CodedBitstreamContext *cbc;
32     CodedBitstreamFragment fragment;
33 } TraceHeadersContext;
34
35
36 static int trace_headers_init(AVBSFContext *bsf)
37 {
38     TraceHeadersContext *ctx = bsf->priv_data;
39     int err;
40
41     err = ff_cbs_init(&ctx->cbc, bsf->par_in->codec_id, bsf);
42     if (err < 0)
43         return err;
44
45     ctx->cbc->trace_enable = 1;
46     ctx->cbc->trace_level  = AV_LOG_INFO;
47     ctx->cbc->trace_context = ctx->cbc;
48     ctx->cbc->trace_read_callback = ff_cbs_trace_read_log;
49
50     if (bsf->par_in->extradata) {
51         CodedBitstreamFragment *frag = &ctx->fragment;
52
53         av_log(bsf, AV_LOG_INFO, "Extradata\n");
54
55         err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in);
56
57         ff_cbs_fragment_reset(frag);
58     }
59
60     return err;
61 }
62
63 static void trace_headers_close(AVBSFContext *bsf)
64 {
65     TraceHeadersContext *ctx = bsf->priv_data;
66
67     ff_cbs_fragment_free(&ctx->fragment);
68     ff_cbs_close(&ctx->cbc);
69 }
70
71 static int trace_headers(AVBSFContext *bsf, AVPacket *pkt)
72 {
73     TraceHeadersContext *ctx = bsf->priv_data;
74     CodedBitstreamFragment *frag = &ctx->fragment;
75     char tmp[256] = { 0 };
76     int err;
77
78     err = ff_bsf_get_packet_ref(bsf, pkt);
79     if (err < 0)
80         return err;
81
82     if (pkt->flags & AV_PKT_FLAG_KEY)
83         av_strlcat(tmp, ", key frame", sizeof(tmp));
84     if (pkt->flags & AV_PKT_FLAG_CORRUPT)
85         av_strlcat(tmp, ", corrupt", sizeof(tmp));
86
87     if (pkt->pts != AV_NOPTS_VALUE)
88         av_strlcatf(tmp, sizeof(tmp), ", pts %"PRId64, pkt->pts);
89     else
90         av_strlcat(tmp, ", no pts", sizeof(tmp));
91     if (pkt->dts != AV_NOPTS_VALUE)
92         av_strlcatf(tmp, sizeof(tmp), ", dts %"PRId64, pkt->dts);
93     else
94         av_strlcat(tmp, ", no dts", sizeof(tmp));
95     if (pkt->duration > 0)
96         av_strlcatf(tmp, sizeof(tmp), ", duration %"PRId64, pkt->duration);
97
98     av_log(bsf, AV_LOG_INFO, "Packet: %d bytes%s.\n", pkt->size, tmp);
99
100     if (av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, NULL)) {
101         av_log(bsf, AV_LOG_INFO, "Side data:\n");
102
103         err = ff_cbs_read_packet_side_data(ctx->cbc, frag, pkt);
104         ff_cbs_fragment_reset(frag);
105
106         if (err < 0) {
107             av_packet_unref(pkt);
108             return err;
109         }
110         av_log(bsf, AV_LOG_INFO, "Payload:\n");
111     }
112
113     err = ff_cbs_read_packet(ctx->cbc, frag, pkt);
114
115     ff_cbs_fragment_reset(frag);
116
117     if (err < 0)
118         av_packet_unref(pkt);
119     return err;
120 }
121
122 const FFBitStreamFilter ff_trace_headers_bsf = {
123     .p.name         = "trace_headers",
124     .p.codec_ids    = ff_cbs_all_codec_ids,
125     .priv_data_size = sizeof(TraceHeadersContext),
126     .init           = &trace_headers_init,
127     .close          = &trace_headers_close,
128     .filter         = &trace_headers,
129 };