]> git.sesse.net Git - ffmpeg/blob - libavcodec/remove_extradata_bsf.c
mpeg12dec: move setting first_field to mpeg_field_start()
[ffmpeg] / libavcodec / remove_extradata_bsf.c
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/log.h"
22 #include "libavutil/opt.h"
23
24 #include "avcodec.h"
25 #include "bsf.h"
26
27 enum RemoveFreq {
28     REMOVE_FREQ_KEYFRAME,
29     REMOVE_FREQ_ALL,
30 };
31
32 typedef struct RemoveExtradataContext {
33     const AVClass *class;
34     int freq;
35
36     AVCodecParserContext *parser;
37     AVCodecContext *avctx;
38 } RemoveExtradataContext;
39
40 static int remove_extradata(AVBSFContext *ctx, AVPacket *out)
41 {
42     RemoveExtradataContext *s = ctx->priv_data;
43
44     AVPacket *in;
45     int ret;
46
47     ret = ff_bsf_get_packet(ctx, &in);
48     if (ret < 0)
49         return ret;
50
51     if (s->parser && s->parser->parser->split) {
52         if (s->freq == REMOVE_FREQ_ALL ||
53             (s->freq == REMOVE_FREQ_KEYFRAME && in->flags & AV_PKT_FLAG_KEY)) {
54             int i = s->parser->parser->split(s->avctx, in->data, in->size);
55             in->data += i;
56             in->size -= i;
57         }
58     }
59
60     av_packet_move_ref(out, in);
61     av_packet_free(&in);
62
63     return 0;
64 }
65
66 static int remove_extradata_init(AVBSFContext *ctx)
67 {
68     RemoveExtradataContext *s = ctx->priv_data;
69     int ret;
70
71     s->parser = av_parser_init(ctx->par_in->codec_id);
72
73     if (s->parser) {
74         s->avctx = avcodec_alloc_context3(NULL);
75         if (!s->avctx)
76             return AVERROR(ENOMEM);
77
78         ret = avcodec_parameters_to_context(s->avctx, ctx->par_in);
79         if (ret < 0)
80             return ret;
81     }
82
83     return 0;
84 }
85
86 static void remove_extradata_close(AVBSFContext *ctx)
87 {
88     RemoveExtradataContext *s = ctx->priv_data;
89
90     avcodec_free_context(&s->avctx);
91     av_parser_close(s->parser);
92 }
93
94 #define OFFSET(x) offsetof(RemoveExtradataContext, x)
95 static const AVOption options[] = {
96     { "freq", NULL, OFFSET(freq), AV_OPT_TYPE_INT, { .i64 = REMOVE_FREQ_KEYFRAME }, REMOVE_FREQ_KEYFRAME, REMOVE_FREQ_ALL, 0, "freq" },
97         { "keyframe", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_KEYFRAME }, .unit = "freq" },
98         { "all",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_ALL      }, .unit = "freq" },
99     { NULL },
100 };
101
102 static const AVClass remove_extradata_class = {
103     .class_name = "remove_extradata",
104     .item_name  = av_default_item_name,
105     .option     = options,
106     .version    = LIBAVUTIL_VERSION_INT,
107 };
108
109 const AVBitStreamFilter ff_remove_extradata_bsf = {
110     .name           = "remove_extra",
111     .priv_data_size = sizeof(RemoveExtradataContext),
112     .priv_class     = &remove_extradata_class,
113     .init           = remove_extradata_init,
114     .close          = remove_extradata_close,
115     .filter         = remove_extradata,
116 };