Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* LPCM codecs for PCM formats found in Video DVD streams |
3 |
|
|
* Copyright (c) 2018 Paul B Mahol |
4 |
|
|
* |
5 |
|
|
* This file is part of FFmpeg. |
6 |
|
|
* |
7 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
8 |
|
|
* modify it under the terms of the GNU Lesser General Public |
9 |
|
|
* License as published by the Free Software Foundation; either |
10 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
|
|
* Lesser General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU Lesser General Public |
18 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#include "libavutil/channel_layout.h" |
23 |
|
|
#include "avcodec.h" |
24 |
|
|
#include "bytestream.h" |
25 |
|
|
#include "codec_internal.h" |
26 |
|
|
#include "encode.h" |
27 |
|
|
#include "internal.h" |
28 |
|
|
|
29 |
|
|
typedef struct PCMDVDContext { |
30 |
|
|
uint8_t header[3]; // Header added to every frame |
31 |
|
|
int block_size; // Size of a block of samples in bytes |
32 |
|
|
int samples_per_block; // Number of samples per channel per block |
33 |
|
|
int groups_per_block; // Number of 20/24-bit sample groups per block |
34 |
|
|
uint8_t *extra_samples; // Pointer to leftover samples from a frame |
35 |
|
|
int extra_sample_count; // Number of leftover samples in the buffer |
36 |
|
|
} PCMDVDContext; |
37 |
|
|
|
38 |
|
✗ |
static av_cold int pcm_dvd_encode_init(AVCodecContext *avctx) |
39 |
|
|
{ |
40 |
|
✗ |
PCMDVDContext *s = avctx->priv_data; |
41 |
|
|
int quant, freq, frame_size; |
42 |
|
|
|
43 |
|
✗ |
switch (avctx->sample_rate) { |
44 |
|
✗ |
case 48000: |
45 |
|
✗ |
freq = 0; |
46 |
|
✗ |
break; |
47 |
|
✗ |
case 96000: |
48 |
|
✗ |
freq = 1; |
49 |
|
✗ |
break; |
50 |
|
✗ |
default: |
51 |
|
|
av_assert1(0); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
✗ |
switch (avctx->sample_fmt) { |
55 |
|
✗ |
case AV_SAMPLE_FMT_S16: |
56 |
|
✗ |
avctx->bits_per_coded_sample = 16; |
57 |
|
✗ |
quant = 0; |
58 |
|
✗ |
break; |
59 |
|
✗ |
case AV_SAMPLE_FMT_S32: |
60 |
|
✗ |
avctx->bits_per_coded_sample = 24; |
61 |
|
✗ |
quant = 2; |
62 |
|
✗ |
break; |
63 |
|
✗ |
default: |
64 |
|
|
av_assert1(0); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
✗ |
avctx->bits_per_coded_sample = 16 + quant * 4; |
68 |
|
✗ |
avctx->block_align = avctx->ch_layout.nb_channels * avctx->bits_per_coded_sample / 8; |
69 |
|
✗ |
avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate; |
70 |
|
✗ |
if (avctx->bit_rate > 9800000) { |
71 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Too big bitrate: reduce sample rate, bitdepth or channels.\n"); |
72 |
|
✗ |
return AVERROR(EINVAL); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
✗ |
if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) { |
76 |
|
✗ |
s->samples_per_block = 1; |
77 |
|
✗ |
s->block_size = avctx->ch_layout.nb_channels * 2; |
78 |
|
✗ |
frame_size = 2008 / s->block_size; |
79 |
|
|
} else { |
80 |
|
✗ |
switch (avctx->ch_layout.nb_channels) { |
81 |
|
✗ |
case 1: |
82 |
|
|
case 2: |
83 |
|
|
case 4: |
84 |
|
|
/* one group has all the samples needed */ |
85 |
|
✗ |
s->block_size = 4 * avctx->bits_per_coded_sample / 8; |
86 |
|
✗ |
s->samples_per_block = 4 / avctx->ch_layout.nb_channels; |
87 |
|
✗ |
s->groups_per_block = 1; |
88 |
|
✗ |
break; |
89 |
|
✗ |
case 8: |
90 |
|
|
/* two groups have all the samples needed */ |
91 |
|
✗ |
s->block_size = 8 * avctx->bits_per_coded_sample / 8; |
92 |
|
✗ |
s->samples_per_block = 1; |
93 |
|
✗ |
s->groups_per_block = 2; |
94 |
|
✗ |
break; |
95 |
|
✗ |
default: |
96 |
|
|
/* need avctx->ch_layout.nb_channels groups */ |
97 |
|
✗ |
s->block_size = 4 * avctx->ch_layout.nb_channels * |
98 |
|
✗ |
avctx->bits_per_coded_sample / 8; |
99 |
|
✗ |
s->samples_per_block = 4; |
100 |
|
✗ |
s->groups_per_block = avctx->ch_layout.nb_channels; |
101 |
|
✗ |
break; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
✗ |
frame_size = FFALIGN(2008 / s->block_size, s->samples_per_block); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
✗ |
s->header[0] = 0x0c; |
108 |
|
✗ |
s->header[1] = (quant << 6) | (freq << 4) | (avctx->ch_layout.nb_channels - 1); |
109 |
|
✗ |
s->header[2] = 0x80; |
110 |
|
|
|
111 |
|
✗ |
if (!avctx->frame_size) |
112 |
|
✗ |
avctx->frame_size = frame_size; |
113 |
|
|
|
114 |
|
✗ |
return 0; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
✗ |
static int pcm_dvd_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, |
118 |
|
|
const AVFrame *frame, int *got_packet_ptr) |
119 |
|
|
{ |
120 |
|
✗ |
PCMDVDContext *s = avctx->priv_data; |
121 |
|
✗ |
int samples = frame->nb_samples * avctx->ch_layout.nb_channels; |
122 |
|
✗ |
int64_t pkt_size = (frame->nb_samples / s->samples_per_block) * s->block_size + 3; |
123 |
|
✗ |
int blocks = (pkt_size - 3) / s->block_size; |
124 |
|
|
const int16_t *src16; |
125 |
|
|
const int32_t *src32; |
126 |
|
|
PutByteContext pb; |
127 |
|
|
int ret; |
128 |
|
|
|
129 |
|
✗ |
if ((ret = ff_get_encode_buffer(avctx, avpkt, pkt_size, 0)) < 0) |
130 |
|
✗ |
return ret; |
131 |
|
|
|
132 |
|
✗ |
memcpy(avpkt->data, s->header, 3); |
133 |
|
|
|
134 |
|
✗ |
src16 = (const int16_t *)frame->data[0]; |
135 |
|
✗ |
src32 = (const int32_t *)frame->data[0]; |
136 |
|
|
|
137 |
|
✗ |
bytestream2_init_writer(&pb, avpkt->data + 3, avpkt->size - 3); |
138 |
|
|
|
139 |
|
✗ |
switch (avctx->sample_fmt) { |
140 |
|
✗ |
case AV_SAMPLE_FMT_S16: |
141 |
|
|
do { |
142 |
|
✗ |
bytestream2_put_be16(&pb, *src16++); |
143 |
|
✗ |
} while (--samples); |
144 |
|
✗ |
break; |
145 |
|
✗ |
case AV_SAMPLE_FMT_S32: |
146 |
|
✗ |
if (avctx->ch_layout.nb_channels == 1) { |
147 |
|
|
do { |
148 |
|
✗ |
for (int i = 2; i; i--) { |
149 |
|
✗ |
bytestream2_put_be16(&pb, src32[0] >> 16); |
150 |
|
✗ |
bytestream2_put_be16(&pb, src32[1] >> 16); |
151 |
|
✗ |
bytestream2_put_byte(&pb, (*src32++) >> 24); |
152 |
|
✗ |
bytestream2_put_byte(&pb, (*src32++) >> 24); |
153 |
|
|
} |
154 |
|
✗ |
} while (--blocks); |
155 |
|
|
} else { |
156 |
|
|
do { |
157 |
|
✗ |
for (int i = s->groups_per_block; i; i--) { |
158 |
|
✗ |
bytestream2_put_be16(&pb, src32[0] >> 16); |
159 |
|
✗ |
bytestream2_put_be16(&pb, src32[1] >> 16); |
160 |
|
✗ |
bytestream2_put_be16(&pb, src32[2] >> 16); |
161 |
|
✗ |
bytestream2_put_be16(&pb, src32[3] >> 16); |
162 |
|
✗ |
bytestream2_put_byte(&pb, (*src32++) >> 24); |
163 |
|
✗ |
bytestream2_put_byte(&pb, (*src32++) >> 24); |
164 |
|
✗ |
bytestream2_put_byte(&pb, (*src32++) >> 24); |
165 |
|
✗ |
bytestream2_put_byte(&pb, (*src32++) >> 24); |
166 |
|
|
} |
167 |
|
✗ |
} while (--blocks); |
168 |
|
|
} |
169 |
|
✗ |
break; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
✗ |
avpkt->pts = frame->pts; |
173 |
|
✗ |
avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples); |
174 |
|
✗ |
*got_packet_ptr = 1; |
175 |
|
|
|
176 |
|
✗ |
return 0; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
const FFCodec ff_pcm_dvd_encoder = { |
180 |
|
|
.p.name = "pcm_dvd", |
181 |
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for DVD media"), |
182 |
|
|
.p.type = AVMEDIA_TYPE_AUDIO, |
183 |
|
|
.p.id = AV_CODEC_ID_PCM_DVD, |
184 |
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SMALL_LAST_FRAME, |
185 |
|
|
.priv_data_size = sizeof(PCMDVDContext), |
186 |
|
|
.init = pcm_dvd_encode_init, |
187 |
|
|
FF_CODEC_ENCODE_CB(pcm_dvd_encode_frame), |
188 |
|
|
.p.supported_samplerates = (const int[]) { 48000, 96000, 0}, |
189 |
|
|
#if FF_API_OLD_CHANNEL_LAYOUT |
190 |
|
|
.p.channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO, |
191 |
|
|
AV_CH_LAYOUT_STEREO, |
192 |
|
|
AV_CH_LAYOUT_5POINT1, |
193 |
|
|
AV_CH_LAYOUT_7POINT1, |
194 |
|
|
0 }, |
195 |
|
|
#endif |
196 |
|
|
.p.ch_layouts = (const AVChannelLayout[]) { AV_CHANNEL_LAYOUT_MONO, |
197 |
|
|
AV_CHANNEL_LAYOUT_STEREO, |
198 |
|
|
AV_CHANNEL_LAYOUT_5POINT1, |
199 |
|
|
AV_CHANNEL_LAYOUT_7POINT1, |
200 |
|
|
{ 0 } }, |
201 |
|
|
.p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, |
202 |
|
|
AV_SAMPLE_FMT_S32, |
203 |
|
|
AV_SAMPLE_FMT_NONE }, |
204 |
|
|
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, |
205 |
|
|
}; |
206 |
|
|
|