2 * This file is part of FFmpeg.
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.
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.
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
19 #include "bsf_internal.h"
22 static int cbs_bsf_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
24 CBSBSFContext *ctx = bsf->priv_data;
25 CodedBitstreamFragment *frag = &ctx->fragment;
30 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
35 err = ff_cbs_read(ctx->input, frag, side_data, side_data_size);
37 av_log(bsf, AV_LOG_ERROR,
38 "Failed to read extradata from packet side data.\n");
42 err = ctx->type->update_fragment(bsf, NULL, frag);
46 err = ff_cbs_write_fragment_data(ctx->output, frag);
48 av_log(bsf, AV_LOG_ERROR,
49 "Failed to write extradata into packet side data.\n");
53 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
56 return AVERROR(ENOMEM);
57 memcpy(side_data, frag->data, frag->data_size);
59 ff_cbs_fragment_reset(frag);
63 int ff_cbs_bsf_generic_filter(AVBSFContext *bsf, AVPacket *pkt)
65 CBSBSFContext *ctx = bsf->priv_data;
66 CodedBitstreamFragment *frag = &ctx->fragment;
69 err = ff_bsf_get_packet_ref(bsf, pkt);
73 err = cbs_bsf_update_side_data(bsf, pkt);
77 err = ff_cbs_read_packet(ctx->input, frag, pkt);
79 av_log(bsf, AV_LOG_ERROR, "Failed to read %s from packet.\n",
80 ctx->type->fragment_name);
84 if (frag->nb_units == 0) {
85 av_log(bsf, AV_LOG_ERROR, "No %s found in packet.\n",
86 ctx->type->unit_name);
87 err = AVERROR_INVALIDDATA;
91 err = ctx->type->update_fragment(bsf, pkt, frag);
95 err = ff_cbs_write_packet(ctx->output, pkt, frag);
97 av_log(bsf, AV_LOG_ERROR, "Failed to write %s into packet.\n",
98 ctx->type->fragment_name);
104 ff_cbs_fragment_reset(frag);
107 av_packet_unref(pkt);
112 int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type)
114 CBSBSFContext *ctx = bsf->priv_data;
115 CodedBitstreamFragment *frag = &ctx->fragment;
120 err = ff_cbs_init(&ctx->input, type->codec_id, bsf);
124 err = ff_cbs_init(&ctx->output, type->codec_id, bsf);
128 if (bsf->par_in->extradata) {
129 err = ff_cbs_read_extradata(ctx->input, frag, bsf->par_in);
131 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
135 err = type->update_fragment(bsf, NULL, frag);
139 err = ff_cbs_write_extradata(ctx->output, bsf->par_out, frag);
141 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
148 ff_cbs_fragment_reset(frag);
152 void ff_cbs_bsf_generic_close(AVBSFContext *bsf)
154 CBSBSFContext *ctx = bsf->priv_data;
156 ff_cbs_fragment_free(&ctx->fragment);
157 ff_cbs_close(&ctx->input);
158 ff_cbs_close(&ctx->output);