f973f2fccb4129cd5311608da4dfd5f4932a891c
[goodguy/cinelerra.git] / cinelerra-5.1 / thirdparty / src / ffmpeg-4.4.patch_9
1 --- /dev/null   2021-12-05 17:02:04.576000000 +0300
2 +++ ./libavcodec/pcm-bluenc.c   2021-12-08 16:22:32.519865993 +0300
3 @@ -0,0 +1,332 @@
4 +/*
5 + * LPCM codecs for PCM formats found in Blu-ray m2ts streams
6 + * Copyright (c) 2018 Paul B Mahol
7 + *
8 + * This file is part of FFmpeg.
9 + *
10 + * FFmpeg is free software; you can redistribute it and/or
11 + * modify it under the terms of the GNU Lesser General Public
12 + * License as published by the Free Software Foundation; either
13 + * version 2.1 of the License, or (at your option) any later version.
14 + *
15 + * FFmpeg is distributed in the hope that it will be useful,
16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 + * Lesser General Public License for more details.
19 + *
20 + * You should have received a copy of the GNU Lesser General Public
21 + * License along with FFmpeg; if not, write to the Free Software
22 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 + */
24 +
25 +#include "avcodec.h"
26 +#include "bytestream.h"
27 +#include "internal.h"
28 +
29 +typedef struct PCMBDContext {
30 +    uint8_t header[4];       // 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 +} PCMBDContext;
37 +
38 +static av_cold int pcm_bd_encode_init(AVCodecContext *avctx)
39 +{
40 +    PCMBDContext *s = avctx->priv_data;
41 +    int quant, freq;
42 +    uint16_t frame_size;
43 +    uint8_t ch_layout = 0;
44 +
45 +    switch (avctx->sample_rate) {
46 +    case 48000:
47 +        freq = 1;
48 +        break;
49 +    case 96000:
50 +        freq = 4;
51 +        break;
52 +    case 192000:
53 +       freq = 5;
54 +       break;
55 +    }
56 +
57 +    switch (avctx->sample_fmt) {
58 +    case AV_SAMPLE_FMT_S16:
59 +        avctx->bits_per_coded_sample = 16;
60 +        quant = 1;
61 +        break;
62 +/*    case AV_SAMPLE_FMT_S20:
63 +        avctx->bits_per_coded_sample = 20;
64 +        quant = 2;
65 +        break;
66 +*/
67 +    case AV_SAMPLE_FMT_S32:
68 +        avctx->bits_per_coded_sample = 24;
69 +        quant = 3;
70 +        break;
71 +    }
72 +
73 +    avctx->block_align           = avctx->channels * avctx->bits_per_coded_sample / 8;
74 +    avctx->bit_rate              = avctx->block_align * 8LL * avctx->sample_rate;
75 +    if (avctx->bit_rate > 19800000) {
76 +        av_log(avctx, AV_LOG_ERROR, "Too big bitrate: reduce sample rate, bitdepth or channels.\n");
77 +        return AVERROR(EINVAL);
78 +    }
79 +
80 +    if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
81 +       switch (avctx->channels) {
82 +       case 1:
83 +           s->block_size = avctx->channels * 4;
84 +           break;
85 +       default:
86 +        s->block_size        = avctx->channels * 2;
87 +        break;
88 +        }
89 +        s->samples_per_block = 1;
90 +        frame_size           = 2008 / s->block_size;
91 +    } else {
92 +        switch (avctx->channels) {
93 +        case 1:
94 +            s->block_size        = 2 * avctx->channels * avctx->bits_per_coded_sample / 8;
95 +           s->samples_per_block = 1;
96 +        break;
97 +        case 2:
98 +        case 4:
99 +            /* one group has all the samples needed */
100 +            s->block_size        = avctx->channels * avctx->bits_per_coded_sample / 8;
101 +            s->samples_per_block = 1;
102 +            s->groups_per_block  = 2;
103 +            break;
104 +        case 8:
105 +        case 6:
106 +            /* two groups have all the samples needed */
107 +            s->block_size        = avctx->channels * avctx->bits_per_coded_sample / 8;
108 +            s->samples_per_block = 1;
109 +            // s->groups_per_block  = 2;
110 +            break;
111 +        default:
112 +            /* need avctx->channels groups */
113 +            s->block_size        = 4 * avctx->channels *
114 +                                   avctx->bits_per_coded_sample / 8;
115 +            s->samples_per_block = 4;
116 +            s->groups_per_block  = avctx->channels;
117 +            break;
118 +        }
119 +
120 +        frame_size = FFALIGN(2008 / s->block_size, s->samples_per_block);
121 +    }
122 +
123 +    switch(avctx->channel_layout) {
124 +       case AV_CH_LAYOUT_MONO:
125 +               ch_layout = 1;
126 +               break;
127 +       case AV_CH_LAYOUT_STEREO:
128 +               ch_layout = 3;
129 +               break;
130 +       case AV_CH_LAYOUT_5POINT1:
131 +       case AV_CH_LAYOUT_5POINT1_BACK:
132 +               ch_layout = 9;
133 +               break;
134 +       case AV_CH_LAYOUT_7POINT1:
135 +               ch_layout = 11;
136 +               break;
137 +       default:
138 +               av_log(avctx, AV_LOG_ERROR, "Not yet implemented ch layout!\n");
139 +       }
140 +// description on the web:
141 +/* http://forum.doom9.org/showthread.php?t=152897
142 +
143 +It's a header.
144 +
145 +size in bytes = 16 bits (big endian)
146 +channel assignment = 4 bits
147 +sampling frequency = 4 bits
148 +bits per sample = 2 bits
149 +start flag = 1 bit
150 +reserved = 5 bits
151 +
152 +channel assignment
153 +1 = mono
154 +3 = stereo
155 +4 = 3/0
156 +5 = 2/1
157 +6 = 3/1
158 +7 = 2/2
159 +8 = 3/2
160 +9 = 3/2+lfe
161 +10 = 3/4
162 +11 = 3/4+lfe
163 +
164 +sampling frequency
165 +1 = 48 kHz
166 +4 = 96 kHz
167 +5 = 192 kHz
168 +
169 +bits per sample
170 +1 = 16
171 +2 = 20
172 +3 = 24
173 +*/
174 +
175 +    s->header[2] = (ch_layout << 4) | (freq);
176 +    s->header[3] = (quant << 6) | 0x1 ;
177 +
178 +
179 +        avctx->frame_size = frame_size; // in num. of samples
180 +
181 +    return 0;
182 +}
183 +
184 +static int pcm_bd_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
185 +                                const AVFrame *frame, int *got_packet_ptr)
186 +{
187 +    PCMBDContext *s = avctx->priv_data;
188 +    int samples, channels;
189 +    int64_t pkt_size = (frame->nb_samples / s->samples_per_block) * s->block_size + 4;
190 +    const int16_t *src16;
191 +    const int32_t *src32;
192 +    PutByteContext pb;
193 +    int ret;
194 +
195 +    if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
196 +        return ret;
197 +
198 +    AV_WB16(s->header, pkt_size - 4);
199 +    memcpy(avpkt->data, s->header, 4);
200 +
201 +    src16 = (const int16_t *)frame->data[0];
202 +    src32 = (const int32_t *)frame->data[0];
203 +
204 +    bytestream2_init_writer(&pb, avpkt->data + 4, avpkt->size - 4);
205 +
206 +    int num_source_channels = FFALIGN(avctx->channels, 2);
207 +    // int num_source_channels = avctx->channels;
208 +    // int sample_size = (num_source_channels *
209 +    //               (avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 16 : 24)) >> 3;
210 +    samples = frame->nb_samples * num_source_channels;
211 +
212 +    switch (avctx->sample_fmt) {
213 +    case AV_SAMPLE_FMT_S16:
214 +    switch (avctx->channels) {
215 +       case 1:
216 +           do {
217 +                   do {
218 +                       channels = avctx->channels;
219 +                           bytestream2_put_be16(&pb, *src16++);
220 +               } while (--channels);
221 +               bytestream2_put_be16(&pb, 0);
222 +               } while (--samples);
223 +        break;
224 +       case 2:
225 +       do {
226 +            bytestream2_put_be16(&pb, *src16++);
227 +        } while (--samples);
228 +        break;
229 +       case 6:
230 +       do {
231 +           bytestream2_put_be16(&pb, src16[0]);
232 +           bytestream2_put_be16(&pb, src16[1]);
233 +           bytestream2_put_be16(&pb, src16[2]);
234 +           bytestream2_put_be16(&pb, src16[4]);
235 +           bytestream2_put_be16(&pb, src16[5]);
236 +           bytestream2_put_be16(&pb, src16[3]);
237 +           src16+=6;
238 +        } while (--samples);
239 +       break;
240 +       case 8:
241 +       do {
242 +           bytestream2_put_be16(&pb, src16[0]);
243 +           bytestream2_put_be16(&pb, src16[1]);
244 +           bytestream2_put_be16(&pb, src16[2]);
245 +           bytestream2_put_be16(&pb, src16[6]);
246 +           bytestream2_put_be16(&pb, src16[4]);
247 +           bytestream2_put_be16(&pb, src16[5]);
248 +           bytestream2_put_be16(&pb, src16[7]);
249 +           bytestream2_put_be16(&pb, src16[3]);
250 +           src16+=8;
251 +        } while (--samples);
252 +       break;
253 +
254 +       default:
255 +       av_log(avctx, AV_LOG_ERROR, "this ch config not implemented for s16!\n");
256 +        break;
257 +        }
258 +        break;
259 +    case AV_SAMPLE_FMT_S32:
260 +               switch (avctx->channels) {
261 +                       case 2:
262 +                       case 4:
263 +                           do {
264 +                               bytestream2_put_be24(&pb, (*src32++) >> 8);
265 +                           } while (--samples);
266 +                    break;
267 +                        case 8:
268 +                            do {
269 +                           bytestream2_put_be24(&pb, src32[0] >> 8);
270 +                           bytestream2_put_be24(&pb, src32[1] >> 8);
271 +                           bytestream2_put_be24(&pb, src32[2] >> 8);
272 +                           bytestream2_put_be24(&pb, src32[6] >> 8);
273 +                           bytestream2_put_be24(&pb, src32[4] >> 8);
274 +                           bytestream2_put_be24(&pb, src32[5] >> 8);
275 +                           bytestream2_put_be24(&pb, src32[7] >> 8);
276 +                           bytestream2_put_be24(&pb, src32[3] >> 8);
277 +                           src32+=8;
278 +                           } while (--samples);
279 +                   break;
280 +                       case 6:
281 +                       do {
282 +                           bytestream2_put_be24(&pb, src32[0] >> 8);
283 +                           bytestream2_put_be24(&pb, src32[1] >> 8);
284 +                           bytestream2_put_be24(&pb, src32[2] >> 8);
285 +                           bytestream2_put_be24(&pb, src32[4] >> 8);
286 +                           bytestream2_put_be24(&pb, src32[5] >> 8);
287 +                           bytestream2_put_be24(&pb, src32[3] >> 8);
288 +                           src32+=6;
289 +                           } while (--samples);
290 +                   break;
291 +                       case 1:
292 +                           do {
293 +                              do {
294 +                                   channels = avctx->channels;
295 +                               bytestream2_put_be24(&pb, (*src32++) >> 8);
296 +                               } while (--channels);
297 +                           bytestream2_put_be24(&pb, 0);
298 +                           } while (--samples);
299 +                    break;
300 +                       default:
301 +                       av_log(avctx, AV_LOG_ERROR, "this bitdepth not implemented!\n");
302 +                    break;
303 +                }
304 +    break;
305 +    }
306 +
307 +    avpkt->pts      = frame->pts;
308 +    avpkt->size     = pkt_size;
309 +    avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
310 +    *got_packet_ptr = 1;
311 +
312 +    return 0;
313 +}
314 +
315 +AVCodec ff_pcm_bluray_encoder = {
316 +    .name           = "pcm_bluray",
317 +    .long_name      = NULL_IF_CONFIG_SMALL("PCM signed 16|24-bit big-endian for bluray media"),
318 +    .type           = AVMEDIA_TYPE_AUDIO,
319 +    .id             = AV_CODEC_ID_PCM_BLURAY,
320 +    .priv_data_size = sizeof(PCMBDContext),
321 +    .init           = pcm_bd_encode_init,
322 +    .encode2        = pcm_bd_encode_frame,
323 +    .capabilities   = AV_CODEC_CAP_SMALL_LAST_FRAME,
324 +    .supported_samplerates = (const int[]) { 48000, 96000, 192000, 0},
325 +    .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
326 +                                            AV_CH_LAYOUT_STEREO,
327 +                                            AV_CH_LAYOUT_5POINT1,
328 +                                            AV_CH_LAYOUT_5POINT1_BACK,
329 +                                            AV_CH_LAYOUT_7POINT1,
330 +                                            0 },
331 +    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
332 +                                                     AV_SAMPLE_FMT_S32,
333 +                                                     AV_SAMPLE_FMT_NONE },
334 +    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
335 +};
336 --- ./libavcodec/allcodecs.orig 2021-04-09 00:28:39.000000000 +0300
337 +++ ./libavcodec/allcodecs.c    2021-12-06 15:45:03.333762281 +0300
338 @@ -523,6 +523,7 @@
339  /* PCM codecs */
340  extern AVCodec ff_pcm_alaw_encoder;
341  extern AVCodec ff_pcm_alaw_decoder;
342 +extern AVCodec ff_pcm_bluray_encoder;
343  extern AVCodec ff_pcm_bluray_decoder;
344  extern AVCodec ff_pcm_dvd_encoder;
345  extern AVCodec ff_pcm_dvd_decoder;
346 --- ./libavcodec/Makefile.orig  2021-04-09 00:28:39.000000000 +0300
347 +++ ./libavcodec/Makefile       2021-12-06 21:11:19.365842066 +0300
348 @@ -789,6 +789,7 @@
349  OBJS-$(CONFIG_PCM_ALAW_DECODER)           += pcm.o
350  OBJS-$(CONFIG_PCM_ALAW_ENCODER)           += pcm.o
351  OBJS-$(CONFIG_PCM_BLURAY_DECODER)         += pcm-bluray.o
352 +OBJS-$(CONFIG_PCM_BLURAY_ENCODER)         += pcm-bluenc.o
353  OBJS-$(CONFIG_PCM_DVD_DECODER)            += pcm-dvd.o
354  OBJS-$(CONFIG_PCM_DVD_ENCODER)            += pcm-dvdenc.o
355  OBJS-$(CONFIG_PCM_F16LE_DECODER)          += pcm.o