2 * Copyright (c) 2020 Marton Balint
4 * This file is part of FFmpeg.
6 * FFmpeg 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.
11 * FFmpeg 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "bsf_internal.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/opt.h"
26 typedef struct PCMContext {
31 AVRational frame_rate;
39 static int init(AVBSFContext *ctx)
41 PCMContext *s = ctx->priv_data;
42 AVRational sr = av_make_q(ctx->par_in->sample_rate, 1);
45 if (ctx->par_in->ch_layout.nb_channels <= 0 || ctx->par_in->sample_rate <= 0)
46 return AVERROR(EINVAL);
48 ctx->time_base_out = av_inv_q(sr);
49 s->sample_size = ctx->par_in->ch_layout.nb_channels *
50 av_get_bits_per_sample(ctx->par_in->codec_id) / 8;
52 if (s->frame_rate.num) {
53 min_samples = av_rescale_q_rnd(1, sr, s->frame_rate, AV_ROUND_DOWN);
55 min_samples = s->nb_out_samples;
57 if (min_samples <= 0 || min_samples > INT_MAX / s->sample_size - 1)
58 return AVERROR(EINVAL);
60 s->in_pkt = av_packet_alloc();
61 s->out_pkt = av_packet_alloc();
62 if (!s->in_pkt || !s->out_pkt)
63 return AVERROR(ENOMEM);
68 static void uninit(AVBSFContext *ctx)
70 PCMContext *s = ctx->priv_data;
71 av_packet_free(&s->in_pkt);
72 av_packet_free(&s->out_pkt);
75 static void flush(AVBSFContext *ctx)
77 PCMContext *s = ctx->priv_data;
78 av_packet_unref(s->in_pkt);
79 av_packet_unref(s->out_pkt);
83 static int send_packet(PCMContext *s, int nb_samples, AVPacket *pkt)
85 pkt->duration = nb_samples;
90 static void drain_packet(AVPacket *pkt, int drain_data, int drain_samples)
92 pkt->size -= drain_data;
93 pkt->data += drain_data;
94 if (pkt->dts != AV_NOPTS_VALUE)
95 pkt->dts += drain_samples;
96 if (pkt->pts != AV_NOPTS_VALUE)
97 pkt->pts += drain_samples;
100 static int get_next_nb_samples(AVBSFContext *ctx)
102 PCMContext *s = ctx->priv_data;
103 if (s->frame_rate.num) {
104 AVRational sr = av_make_q(ctx->par_in->sample_rate, 1);
105 return av_rescale_q(s->n + 1, sr, s->frame_rate) - av_rescale_q(s->n, sr, s->frame_rate);
107 return s->nb_out_samples;
111 static int rechunk_filter(AVBSFContext *ctx, AVPacket *pkt)
113 PCMContext *s = ctx->priv_data;
114 int nb_samples = get_next_nb_samples(ctx);
115 int data_size = nb_samples * s->sample_size;
119 if (s->in_pkt->size) {
120 if (s->out_pkt->size || s->in_pkt->size < data_size) {
121 int drain = FFMIN(s->in_pkt->size, data_size - s->out_pkt->size);
122 if (!s->out_pkt->size) {
123 ret = av_new_packet(s->out_pkt, data_size);
126 ret = av_packet_copy_props(s->out_pkt, s->in_pkt);
128 av_packet_unref(s->out_pkt);
131 s->out_pkt->size = 0;
133 memcpy(s->out_pkt->data + s->out_pkt->size, s->in_pkt->data, drain);
134 s->out_pkt->size += drain;
135 drain_packet(s->in_pkt, drain, drain / s->sample_size);
136 if (!s->in_pkt->size)
137 av_packet_unref(s->in_pkt);
138 if (s->out_pkt->size == data_size) {
139 av_packet_move_ref(pkt, s->out_pkt);
140 return send_packet(s, nb_samples, pkt);
142 av_assert0(!s->in_pkt->size);
143 } else if (s->in_pkt->size > data_size) {
144 ret = av_packet_ref(pkt, s->in_pkt);
147 pkt->size = data_size;
148 drain_packet(s->in_pkt, data_size, nb_samples);
149 return send_packet(s, nb_samples, pkt);
151 av_assert0(s->in_pkt->size == data_size);
152 av_packet_move_ref(pkt, s->in_pkt);
153 return send_packet(s, nb_samples, pkt);
156 av_packet_unref(s->in_pkt);
158 ret = ff_bsf_get_packet_ref(ctx, s->in_pkt);
159 if (ret == AVERROR_EOF && s->out_pkt->size) {
161 memset(s->out_pkt->data + s->out_pkt->size, 0, data_size - s->out_pkt->size);
162 s->out_pkt->size = data_size;
164 nb_samples = s->out_pkt->size / s->sample_size;
166 av_packet_move_ref(pkt, s->out_pkt);
167 return send_packet(s, nb_samples, pkt);
170 av_packet_rescale_ts(s->in_pkt, ctx->time_base_in, ctx->time_base_out);
176 #define OFFSET(x) offsetof(PCMContext, x)
177 #define FLAGS (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_BSF_PARAM)
178 static const AVOption options[] = {
179 { "nb_out_samples", "set the number of per-packet output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
180 { "n", "set the number of per-packet output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
181 { "pad", "pad last packet with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1} , 0, 1, FLAGS },
182 { "p", "pad last packet with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1} , 0, 1, FLAGS },
183 { "frame_rate", "set number of packets per second", OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
184 { "r", "set number of packets per second", OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
188 static const AVClass pcm_rechunk_class = {
189 .class_name = "pcm_rechunk_bsf",
190 .item_name = av_default_item_name,
192 .version = LIBAVUTIL_VERSION_INT,
195 static const enum AVCodecID codec_ids[] = {
196 AV_CODEC_ID_PCM_S16LE,
197 AV_CODEC_ID_PCM_S16BE,
199 AV_CODEC_ID_PCM_S32LE,
200 AV_CODEC_ID_PCM_S32BE,
201 AV_CODEC_ID_PCM_S24LE,
202 AV_CODEC_ID_PCM_S24BE,
203 AV_CODEC_ID_PCM_F32BE,
204 AV_CODEC_ID_PCM_F32LE,
205 AV_CODEC_ID_PCM_F64BE,
206 AV_CODEC_ID_PCM_F64LE,
207 AV_CODEC_ID_PCM_S64LE,
208 AV_CODEC_ID_PCM_S64BE,
209 AV_CODEC_ID_PCM_F16LE,
210 AV_CODEC_ID_PCM_F24LE,
214 const FFBitStreamFilter ff_pcm_rechunk_bsf = {
215 .p.name = "pcm_rechunk",
216 .p.codec_ids = codec_ids,
217 .p.priv_class = &pcm_rechunk_class,
218 .priv_data_size = sizeof(PCMContext),
219 .filter = rechunk_filter,