GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/pcm_rechunk_bsf.c Lines: 73 91 80.2 %
Date: 2020-10-23 17:01:47 Branches: 37 52 71.2 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2020 Marton Balint
3
 *
4
 * This file is part of FFmpeg.
5
 *
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.
10
 *
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.
15
 *
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
19
 */
20
21
#include "avcodec.h"
22
#include "bsf_internal.h"
23
#include "libavutil/avassert.h"
24
#include "libavutil/opt.h"
25
26
typedef struct PCMContext {
27
    const AVClass *class;
28
29
    int nb_out_samples;
30
    int pad;
31
    AVRational frame_rate;
32
33
    AVPacket *in_pkt;
34
    AVPacket *out_pkt;
35
    int sample_size;
36
    int64_t n;
37
} PCMContext;
38
39
14
static int init(AVBSFContext *ctx)
40
{
41
14
    PCMContext *s = ctx->priv_data;
42
14
    AVRational sr = av_make_q(ctx->par_in->sample_rate, 1);
43
    int64_t min_samples;
44
45

14
    if (ctx->par_in->channels <= 0 || ctx->par_in->sample_rate <= 0)
46
        return AVERROR(EINVAL);
47
48
14
    ctx->time_base_out = av_inv_q(sr);
49
14
    s->sample_size = ctx->par_in->channels * av_get_bits_per_sample(ctx->par_in->codec_id) / 8;
50
51
14
    if (s->frame_rate.num) {
52
11
        min_samples = av_rescale_q_rnd(1, sr, s->frame_rate, AV_ROUND_DOWN);
53
    } else {
54
3
        min_samples = s->nb_out_samples;
55
    }
56

14
    if (min_samples <= 0 || min_samples > INT_MAX / s->sample_size - 1)
57
        return AVERROR(EINVAL);
58
59
14
    s->in_pkt  = av_packet_alloc();
60
14
    s->out_pkt = av_packet_alloc();
61

14
    if (!s->in_pkt || !s->out_pkt)
62
        return AVERROR(ENOMEM);
63
64
14
    return 0;
65
}
66
67
14
static void uninit(AVBSFContext *ctx)
68
{
69
14
    PCMContext *s = ctx->priv_data;
70
14
    av_packet_free(&s->in_pkt);
71
14
    av_packet_free(&s->out_pkt);
72
14
}
73
74
static void flush(AVBSFContext *ctx)
75
{
76
    PCMContext *s = ctx->priv_data;
77
    av_packet_unref(s->in_pkt);
78
    av_packet_unref(s->out_pkt);
79
    s->n = 0;
80
}
81
82
226
static int send_packet(PCMContext *s, int nb_samples, AVPacket *pkt)
83
{
84
226
    pkt->duration = nb_samples;
85
226
    s->n++;
86
226
    return 0;
87
}
88
89
690
static void drain_packet(AVPacket *pkt, int drain_data, int drain_samples)
90
{
91
690
    pkt->size -= drain_data;
92
690
    pkt->data += drain_data;
93
690
    if (pkt->dts != AV_NOPTS_VALUE)
94
690
        pkt->dts += drain_samples;
95
690
    if (pkt->pts != AV_NOPTS_VALUE)
96
690
        pkt->pts += drain_samples;
97
690
}
98
99
745
static int get_next_nb_samples(AVBSFContext *ctx)
100
{
101
745
    PCMContext *s = ctx->priv_data;
102
745
    if (s->frame_rate.num) {
103
601
        AVRational sr = av_make_q(ctx->par_in->sample_rate, 1);
104
601
        return av_rescale_q(s->n + 1, sr, s->frame_rate) - av_rescale_q(s->n, sr, s->frame_rate);
105
    } else {
106
144
        return s->nb_out_samples;
107
    }
108
}
109
110
745
static int rechunk_filter(AVBSFContext *ctx, AVPacket *pkt)
111
{
112
745
    PCMContext *s = ctx->priv_data;
113
745
    int nb_samples = get_next_nb_samples(ctx);
114
745
    int data_size = nb_samples * s->sample_size;
115
    int ret;
116
117
    do {
118
1251
        if (s->in_pkt->size) {
119

1177
            if (s->out_pkt->size || s->in_pkt->size < data_size) {
120
690
                int drain = FFMIN(s->in_pkt->size, data_size - s->out_pkt->size);
121
690
                if (!s->out_pkt->size) {
122
217
                    ret = av_new_packet(s->out_pkt, data_size);
123
217
                    if (ret < 0)
124
                        return ret;
125
217
                    ret = av_packet_copy_props(s->out_pkt, s->in_pkt);
126
217
                    if (ret < 0) {
127
                        av_packet_unref(s->out_pkt);
128
                        return ret;
129
                    }
130
217
                    s->out_pkt->size = 0;
131
                }
132
690
                memcpy(s->out_pkt->data + s->out_pkt->size, s->in_pkt->data, drain);
133
690
                s->out_pkt->size += drain;
134
690
                drain_packet(s->in_pkt, drain, drain / s->sample_size);
135
690
                if (!s->in_pkt->size)
136
497
                    av_packet_unref(s->in_pkt);
137
690
                if (s->out_pkt->size == data_size) {
138
212
                    av_packet_move_ref(pkt, s->out_pkt);
139
212
                    return send_packet(s, nb_samples, pkt);
140
                }
141
9
            } else if (s->in_pkt->size > data_size) {
142
                ret = av_packet_ref(pkt, s->in_pkt);
143
                if (ret < 0)
144
                    return ret;
145
                pkt->size = data_size;
146
                drain_packet(s->in_pkt, data_size, nb_samples);
147
                return send_packet(s, nb_samples, pkt);
148
            } else {
149
9
                av_assert0(s->in_pkt->size == data_size);
150
9
                av_packet_move_ref(pkt, s->in_pkt);
151
9
                return send_packet(s, nb_samples, pkt);
152
            }
153
        }
154
155
1030
        ret = ff_bsf_get_packet_ref(ctx, s->in_pkt);
156

1030
        if (ret == AVERROR_EOF && s->out_pkt->size) {
157
5
            if (s->pad) {
158
5
                memset(s->out_pkt->data + s->out_pkt->size, 0, data_size - s->out_pkt->size);
159
5
                s->out_pkt->size = data_size;
160
            } else {
161
                nb_samples = s->out_pkt->size / s->sample_size;
162
            }
163
5
            av_packet_move_ref(pkt, s->out_pkt);
164
5
            return send_packet(s, nb_samples, pkt);
165
        }
166
1025
        if (ret >= 0)
167
506
            av_packet_rescale_ts(s->in_pkt, ctx->time_base_in, ctx->time_base_out);
168
1025
    } while (ret >= 0);
169
170
519
    return ret;
171
}
172
173
#define OFFSET(x) offsetof(PCMContext, x)
174
#define FLAGS (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_BSF_PARAM)
175
static const AVOption options[] = {
176
    { "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 },
177
    { "n",              "set the number of per-packet output samples", OFFSET(nb_out_samples),   AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
178
    { "pad",            "pad last packet with zeros",                  OFFSET(pad),             AV_OPT_TYPE_BOOL, {.i64=1} ,   0,       1, FLAGS },
179
    { "p",              "pad last packet with zeros",                  OFFSET(pad),             AV_OPT_TYPE_BOOL, {.i64=1} ,   0,       1, FLAGS },
180
    { "frame_rate",     "set number of packets per second",            OFFSET(frame_rate),  AV_OPT_TYPE_RATIONAL, {.dbl=0},    0, INT_MAX, FLAGS },
181
    { "r",              "set number of packets per second",            OFFSET(frame_rate),  AV_OPT_TYPE_RATIONAL, {.dbl=0},    0, INT_MAX, FLAGS },
182
    { NULL },
183
};
184
185
static const AVClass pcm_rechunk_class = {
186
    .class_name = "pcm_rechunk_bsf",
187
    .item_name  = av_default_item_name,
188
    .option     = options,
189
    .version    = LIBAVUTIL_VERSION_INT,
190
};
191
192
static const enum AVCodecID codec_ids[] = {
193
    AV_CODEC_ID_PCM_S16LE,
194
    AV_CODEC_ID_PCM_S16BE,
195
    AV_CODEC_ID_PCM_S8,
196
    AV_CODEC_ID_PCM_S32LE,
197
    AV_CODEC_ID_PCM_S32BE,
198
    AV_CODEC_ID_PCM_S24LE,
199
    AV_CODEC_ID_PCM_S24BE,
200
    AV_CODEC_ID_PCM_F32BE,
201
    AV_CODEC_ID_PCM_F32LE,
202
    AV_CODEC_ID_PCM_F64BE,
203
    AV_CODEC_ID_PCM_F64LE,
204
    AV_CODEC_ID_PCM_S64LE,
205
    AV_CODEC_ID_PCM_S64BE,
206
    AV_CODEC_ID_PCM_F16LE,
207
    AV_CODEC_ID_PCM_F24LE,
208
    AV_CODEC_ID_NONE,
209
};
210
211
const AVBitStreamFilter ff_pcm_rechunk_bsf = {
212
    .name           = "pcm_rechunk",
213
    .priv_data_size = sizeof(PCMContext),
214
    .priv_class     = &pcm_rechunk_class,
215
    .filter         = rechunk_filter,
216
    .init           = init,
217
    .flush          = flush,
218
    .close          = uninit,
219
    .codec_ids      = codec_ids,
220
};