]> git.sesse.net Git - ffmpeg/blob - libavcodec/extract_extradata_bsf.c
avcodec/extract_extradata: Move the reference in the bsf internal buffer
[ffmpeg] / libavcodec / extract_extradata_bsf.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <stdint.h>
20
21 #include "libavutil/common.h"
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/log.h"
24 #include "libavutil/opt.h"
25
26 #include "avcodec.h"
27 #include "bsf.h"
28 #include "h2645_parse.h"
29 #include "h264.h"
30 #include "hevc.h"
31 #include "vc1_common.h"
32
33 typedef struct ExtractExtradataContext {
34     const AVClass *class;
35
36     int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
37                    uint8_t **data, int *size);
38
39     /* AVOptions */
40     int remove;
41 } ExtractExtradataContext;
42
43 static int val_in_array(const int *arr, int len, int val)
44 {
45     int i;
46     for (i = 0; i < len; i++)
47         if (arr[i] == val)
48             return 1;
49     return 0;
50 }
51
52 static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
53                                    uint8_t **data, int *size)
54 {
55     static const int extradata_nal_types_hevc[] = {
56         HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS,
57     };
58     static const int extradata_nal_types_h264[] = {
59         H264_NAL_SPS, H264_NAL_PPS,
60     };
61
62     ExtractExtradataContext *s = ctx->priv_data;
63
64     H2645Packet h2645_pkt = { 0 };
65     int extradata_size = 0, filtered_size = 0;
66     const int *extradata_nal_types;
67     int nb_extradata_nal_types;
68     int i, has_sps = 0, has_vps = 0, ret = 0;
69
70     if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
71         extradata_nal_types    = extradata_nal_types_hevc;
72         nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc);
73     } else {
74         extradata_nal_types    = extradata_nal_types_h264;
75         nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
76     }
77
78     ret = ff_h2645_packet_split(&h2645_pkt, pkt->data, pkt->size,
79                                 ctx, 0, 0, ctx->par_in->codec_id);
80     if (ret < 0)
81         goto fail;
82
83     for (i = 0; i < h2645_pkt.nb_nals; i++) {
84         H2645NAL *nal = &h2645_pkt.nals[i];
85         if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {
86             extradata_size += nal->raw_size + 3;
87             if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
88                 if (nal->type == HEVC_NAL_SPS) has_sps = 1;
89                 if (nal->type == HEVC_NAL_VPS) has_vps = 1;
90             } else {
91                 if (nal->type == H264_NAL_SPS) has_sps = 1;
92             }
93         } else if (s->remove) {
94             filtered_size += nal->raw_size + 3;
95         }
96     }
97
98     if (extradata_size &&
99         ((ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) ||
100          (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
101         AVBufferRef *filtered_buf;
102         uint8_t *extradata, *filtered_data;
103
104         if (s->remove) {
105             filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
106             if (!filtered_buf) {
107                 ret = AVERROR(ENOMEM);
108                 goto fail;
109             }
110             memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
111
112             filtered_data = filtered_buf->data;
113         }
114
115         extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
116         if (!extradata) {
117             av_buffer_unref(&filtered_buf);
118             ret = AVERROR(ENOMEM);
119             goto fail;
120         }
121         memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
122
123         *data = extradata;
124         *size = extradata_size;
125
126         for (i = 0; i < h2645_pkt.nb_nals; i++) {
127             H2645NAL *nal = &h2645_pkt.nals[i];
128             if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
129                              nal->type)) {
130                 AV_WB24(extradata, 1); // startcode
131                 memcpy(extradata + 3, nal->raw_data, nal->raw_size);
132                 extradata += 3 + nal->raw_size;
133             } else if (s->remove) {
134                 AV_WB24(filtered_data, 1); // startcode
135                 memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
136                 filtered_data += 3 + nal->raw_size;
137             }
138         }
139
140         if (s->remove) {
141             av_buffer_unref(&pkt->buf);
142             pkt->buf  = filtered_buf;
143             pkt->data = filtered_buf->data;
144             pkt->size = filtered_size;
145         }
146     }
147
148 fail:
149     ff_h2645_packet_uninit(&h2645_pkt);
150     return ret;
151 }
152
153 static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
154                                  uint8_t **data, int *size)
155 {
156     ExtractExtradataContext *s = ctx->priv_data;
157     uint32_t state = UINT32_MAX;
158     int has_extradata = 0, extradata_size = 0;
159     int i;
160
161     for (i = 0; i < pkt->size; i++) {
162         state = (state << 8) | pkt->data[i];
163         if (IS_MARKER(state)) {
164             if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
165                 has_extradata = 1;
166             } else if (has_extradata) {
167                 extradata_size = i - 3;
168                 break;
169             }
170         }
171     }
172
173     if (extradata_size) {
174         *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
175         if (!*data)
176             return AVERROR(ENOMEM);
177
178         memcpy(*data, pkt->data, extradata_size);
179         memset(*data + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
180         *size = extradata_size;
181
182         if (s->remove) {
183             pkt->data += extradata_size;
184             pkt->size -= extradata_size;
185         }
186     }
187
188     return 0;
189 }
190
191 static int extract_extradata_mpeg124(AVBSFContext *ctx, AVPacket *pkt,
192                                      uint8_t **data, int *size)
193 {
194     ExtractExtradataContext *s = ctx->priv_data;
195     int is_mpeg12 = ctx->par_in->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
196                     ctx->par_in->codec_id == AV_CODEC_ID_MPEG2VIDEO;
197     uint32_t state = UINT32_MAX;
198     int i;
199
200     for (i = 0; i < pkt->size; i++) {
201         state = (state << 8) | pkt->data[i];
202         if ((is_mpeg12 && state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100) ||
203             (!is_mpeg12 && (state == 0x1B3 || state == 0x1B6))) {
204             if (i > 3) {
205                 *size = i - 3;
206                 *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
207                 if (!*data)
208                     return AVERROR(ENOMEM);
209
210                 memcpy(*data, pkt->data, *size);
211                 memset(*data + *size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
212
213                 if (s->remove) {
214                     pkt->data += *size;
215                     pkt->size -= *size;
216                 }
217             }
218             break;
219         }
220     }
221     return 0;
222 }
223
224 static const struct {
225     enum AVCodecID id;
226     int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
227                    uint8_t **data, int *size);
228 } extract_tab[] = {
229     { AV_CODEC_ID_CAVS,       extract_extradata_mpeg124 },
230     { AV_CODEC_ID_H264,       extract_extradata_h2645   },
231     { AV_CODEC_ID_HEVC,       extract_extradata_h2645   },
232     { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg124 },
233     { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg124 },
234     { AV_CODEC_ID_MPEG4,      extract_extradata_mpeg124 },
235     { AV_CODEC_ID_VC1,        extract_extradata_vc1     },
236 };
237
238 static int extract_extradata_init(AVBSFContext *ctx)
239 {
240     ExtractExtradataContext *s = ctx->priv_data;
241     int i;
242
243     for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
244         if (extract_tab[i].id == ctx->par_in->codec_id) {
245             s->extract = extract_tab[i].extract;
246             break;
247         }
248     }
249     if (!s->extract)
250         return AVERROR_BUG;
251
252     return 0;
253 }
254
255 static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *pkt)
256 {
257     ExtractExtradataContext *s = ctx->priv_data;
258     uint8_t *extradata = NULL;
259     int extradata_size;
260     int ret = 0;
261
262     ret = ff_bsf_get_packet_ref(ctx, pkt);
263     if (ret < 0)
264         return ret;
265
266     ret = s->extract(ctx, pkt, &extradata, &extradata_size);
267     if (ret < 0)
268         goto fail;
269
270     if (extradata) {
271         ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
272                                       extradata, extradata_size);
273         if (ret < 0) {
274             av_freep(&extradata);
275             goto fail;
276         }
277     }
278
279     return 0;
280
281 fail:
282     av_packet_unref(pkt);
283     return ret;
284 }
285
286 static const enum AVCodecID codec_ids[] = {
287     AV_CODEC_ID_CAVS,
288     AV_CODEC_ID_H264,
289     AV_CODEC_ID_HEVC,
290     AV_CODEC_ID_MPEG1VIDEO,
291     AV_CODEC_ID_MPEG2VIDEO,
292     AV_CODEC_ID_MPEG4,
293     AV_CODEC_ID_VC1,
294     AV_CODEC_ID_NONE,
295 };
296
297 #define OFFSET(x) offsetof(ExtractExtradataContext, x)
298 static const AVOption options[] = {
299     { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
300         { .i64 = 0 }, 0, 1 },
301     { NULL },
302 };
303
304 static const AVClass extract_extradata_class = {
305     .class_name = "extract_extradata",
306     .item_name  = av_default_item_name,
307     .option     = options,
308     .version    = LIBAVUTIL_VERSION_INT,
309 };
310
311 const AVBitStreamFilter ff_extract_extradata_bsf = {
312     .name           = "extract_extradata",
313     .codec_ids      = codec_ids,
314     .priv_data_size = sizeof(ExtractExtradataContext),
315     .priv_class     = &extract_extradata_class,
316     .init           = extract_extradata_init,
317     .filter         = extract_extradata_filter,
318 };