1 --- ffmpeg-4.4/libavcodec/pcm-bluenc.c 2022-04-24 09:45:43.921091116 +0300
2 +++ ffmpeg-4.4/libavcodec/pcm-bluenc.c 2022-04-24 16:07:28.537982120 +0300
5 * LPCM codecs for PCM formats found in Blu-ray m2ts streams
6 - * Copyright (c) 2018 Paul B Mahol
8 * This file is part of FFmpeg.
11 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14 +#include "libavutil/channel_layout.h"
16 #include "bytestream.h"
17 +//#include "codec_internal.h"
21 -typedef struct PCMBDContext {
22 - uint8_t header[4]; // Header added to every frame
23 - int block_size; // Size of a block of samples in bytes
24 - int samples_per_block; // Number of samples per channel per block
25 - int groups_per_block; // Number of 20/24-bit sample groups per block
26 - uint8_t *extra_samples; // Pointer to leftover samples from a frame
27 - int extra_sample_count; // Number of leftover samples in the buffer
29 +typedef struct BlurayPCMEncContext {
30 + uint16_t header; // Header added to every frame
31 +} BlurayPCMEncContext;
33 -static av_cold int pcm_bd_encode_init(AVCodecContext *avctx)
34 +static av_cold int pcm_bluray_encode_init(AVCodecContext *avctx)
36 - PCMBDContext *s = avctx->priv_data;
38 - uint16_t frame_size;
39 - uint8_t ch_layout = 0;
41 + BlurayPCMEncContext *s = avctx->priv_data;
43 + int quant, freq, frame_size;
45 + switch(avctx->sample_fmt) {
46 + case AV_SAMPLE_FMT_S16:
47 + avctx->bits_per_coded_sample = 16;
51 + case AV_SAMPLE_FMT_S32:
52 + avctx->bits_per_coded_sample = 24;
60 switch (avctx->sample_rate) {
78 - switch (avctx->sample_fmt) {
79 - case AV_SAMPLE_FMT_S16:
80 - avctx->bits_per_coded_sample = 16;
82 + //frame_size *= avctx->channels;
84 + switch (avctx->channel_layout) {
85 + case AV_CH_LAYOUT_MONO:
88 -/* case AV_SAMPLE_FMT_S20:
89 - avctx->bits_per_coded_sample = 20;
91 + case AV_CH_LAYOUT_STEREO:
95 - case AV_SAMPLE_FMT_S32:
96 - avctx->bits_per_coded_sample = 24;
98 + case AV_CH_LAYOUT_SURROUND:
103 - avctx->block_align = avctx->channels * avctx->bits_per_coded_sample / 8;
104 - avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate;
105 - if (avctx->bit_rate > 19800000) {
106 - av_log(avctx, AV_LOG_ERROR, "Too big bitrate: reduce sample rate, bitdepth or channels.\n");
107 - return AVERROR(EINVAL);
110 - if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
111 - switch (avctx->channels) {
113 - s->block_size = avctx->channels * 4;
116 - s->block_size = avctx->channels * 2;
117 + case AV_CH_LAYOUT_2_1:
121 - s->samples_per_block = 1;
122 - frame_size = 2008 / s->block_size;
124 - switch (avctx->channels) {
126 - s->block_size = 2 * avctx->channels * avctx->bits_per_coded_sample / 8;
127 - s->samples_per_block = 1;
131 - /* one group has all the samples needed */
132 - s->block_size = avctx->channels * avctx->bits_per_coded_sample / 8;
133 - s->samples_per_block = 1;
134 - s->groups_per_block = 2;
138 - /* two groups have all the samples needed */
139 - s->block_size = avctx->channels * avctx->bits_per_coded_sample / 8;
140 - s->samples_per_block = 1;
141 - // s->groups_per_block = 2;
144 - /* need avctx->channels groups */
145 - s->block_size = 4 * avctx->channels *
146 - avctx->bits_per_coded_sample / 8;
147 - s->samples_per_block = 4;
148 - s->groups_per_block = avctx->channels;
152 - frame_size = FFALIGN(2008 / s->block_size, s->samples_per_block);
153 + case AV_CH_LAYOUT_4POINT0:
156 + case AV_CH_LAYOUT_2_2:
159 + case AV_CH_LAYOUT_5POINT0:
162 + case AV_CH_LAYOUT_5POINT1_BACK:
165 + case AV_CH_LAYOUT_7POINT0:
168 + case AV_CH_LAYOUT_7POINT1:
172 + return AVERROR_BUG;
175 - switch(avctx->channel_layout) {
176 - case AV_CH_LAYOUT_MONO:
179 - case AV_CH_LAYOUT_STEREO:
182 - case AV_CH_LAYOUT_5POINT1:
183 - case AV_CH_LAYOUT_5POINT1_BACK:
186 - case AV_CH_LAYOUT_7POINT1:
190 - av_log(avctx, AV_LOG_ERROR, "Not yet implemented ch layout!\n");
192 -// description on the web:
193 -/* http://forum.doom9.org/showthread.php?t=152897
197 -size in bytes = 16 bits (big endian)
198 -channel assignment = 4 bits
199 -sampling frequency = 4 bits
200 -bits per sample = 2 bits
227 - s->header[2] = (ch_layout << 4) | (freq);
228 - s->header[3] = (quant << 6) | 0x1 ;
231 - avctx->frame_size = frame_size; // in num. of samples
232 + s->header = (((ch_layout << 4) | freq) << 8) | (quant << 6);
233 + avctx->frame_size = frame_size;
238 -static int pcm_bd_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
239 - const AVFrame *frame, int *got_packet_ptr)
240 +static int pcm_bluray_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
241 + const AVFrame *frame, int *got_packet_ptr)
243 - PCMBDContext *s = avctx->priv_data;
244 - int samples, channels;
245 - int64_t pkt_size = (frame->nb_samples / s->samples_per_block) * s->block_size + 4;
246 + BlurayPCMEncContext *s = avctx->priv_data;
247 + int sample_size, samples, channel, num_dest_channels;
248 const int16_t *src16;
249 const int32_t *src32;
254 - if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
255 + num_dest_channels = FFALIGN(avctx->channels, 2);
256 + sample_size = (num_dest_channels *
257 + (avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 16 : 24)) >> 3;
258 + samples = frame->nb_samples;
260 + pkt_size = sample_size * samples + 4;
262 + if ((ret = ff_get_encode_buffer(avctx, avpkt, pkt_size, 0)) < 0)
265 - AV_WB16(s->header, pkt_size - 4);
266 - memcpy(avpkt->data, s->header, 4);
267 + AV_WB16(avpkt->data, pkt_size - 4);
268 + AV_WB16(avpkt->data + 2, s->header);
270 src16 = (const int16_t *)frame->data[0];
271 src32 = (const int32_t *)frame->data[0];
273 bytestream2_init_writer(&pb, avpkt->data + 4, avpkt->size - 4);
275 - int num_source_channels = FFALIGN(avctx->channels, 2);
276 - // int num_source_channels = avctx->channels;
277 - // int sample_size = (num_source_channels *
278 - // (avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 16 : 24)) >> 3;
279 - samples = frame->nb_samples * num_source_channels;
281 - switch (avctx->sample_fmt) {
282 - case AV_SAMPLE_FMT_S16:
283 - switch (avctx->channels) {
287 - channels = avctx->channels;
288 - bytestream2_put_be16(&pb, *src16++);
289 - } while (--channels);
290 - bytestream2_put_be16(&pb, 0);
291 - } while (--samples);
295 - bytestream2_put_be16(&pb, *src16++);
296 - } while (--samples);
300 - bytestream2_put_be16(&pb, src16[0]);
301 - bytestream2_put_be16(&pb, src16[1]);
302 - bytestream2_put_be16(&pb, src16[2]);
303 - bytestream2_put_be16(&pb, src16[4]);
304 - bytestream2_put_be16(&pb, src16[5]);
305 - bytestream2_put_be16(&pb, src16[3]);
307 - } while (--samples);
311 - bytestream2_put_be16(&pb, src16[0]);
312 - bytestream2_put_be16(&pb, src16[1]);
313 - bytestream2_put_be16(&pb, src16[2]);
314 - bytestream2_put_be16(&pb, src16[6]);
315 - bytestream2_put_be16(&pb, src16[4]);
316 - bytestream2_put_be16(&pb, src16[5]);
317 - bytestream2_put_be16(&pb, src16[7]);
318 - bytestream2_put_be16(&pb, src16[3]);
320 - } while (--samples);
324 - av_log(avctx, AV_LOG_ERROR, "this ch config not implemented for s16!\n");
325 + switch (avctx->channel_layout) {
326 + /* cases with same number of source and coded channels */
327 + case AV_CH_LAYOUT_STEREO:
328 + case AV_CH_LAYOUT_4POINT0:
329 + case AV_CH_LAYOUT_2_2:
330 + samples *= num_dest_channels;
331 + if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
333 + bytestream2_put_bufferu(&pb, frame->data[0], samples * 2);
336 + bytestream2_put_be16u(&pb, *src16++);
337 + } while (--samples);
341 + bytestream2_put_be24u(&pb, (*src32++) >> 8);
342 + } while (--samples);
345 + /* cases where number of source channels = coded channels + 1 */
346 + case AV_CH_LAYOUT_MONO:
347 + case AV_CH_LAYOUT_SURROUND:
348 + case AV_CH_LAYOUT_2_1:
349 + case AV_CH_LAYOUT_5POINT0:
350 + if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
353 + bytestream2_put_bufferu(&pb, (const uint8_t *)src16, avctx->ch_layout.nb_channels * 2);
354 + src16 += avctx->channels;
356 + channel = avctx->channels;
358 + bytestream2_put_be16u(&pb, *src16++);
359 + } while (--channel);
361 + bytestream2_put_ne16(&pb, 0);
362 + } while (--samples);
365 + channel = avctx->channels;
367 + bytestream2_put_be24u(&pb, (*src32++) >> 8);
368 + } while (--channel);
369 + bytestream2_put_ne24(&pb, 0);
370 + } while (--samples);
373 - case AV_SAMPLE_FMT_S32:
374 - switch (avctx->channels) {
378 - bytestream2_put_be24(&pb, (*src32++) >> 8);
379 - } while (--samples);
383 - bytestream2_put_be24(&pb, src32[0] >> 8);
384 - bytestream2_put_be24(&pb, src32[1] >> 8);
385 - bytestream2_put_be24(&pb, src32[2] >> 8);
386 - bytestream2_put_be24(&pb, src32[6] >> 8);
387 - bytestream2_put_be24(&pb, src32[4] >> 8);
388 - bytestream2_put_be24(&pb, src32[5] >> 8);
389 - bytestream2_put_be24(&pb, src32[7] >> 8);
390 - bytestream2_put_be24(&pb, src32[3] >> 8);
392 - } while (--samples);
396 - bytestream2_put_be24(&pb, src32[0] >> 8);
397 - bytestream2_put_be24(&pb, src32[1] >> 8);
398 - bytestream2_put_be24(&pb, src32[2] >> 8);
399 - bytestream2_put_be24(&pb, src32[4] >> 8);
400 - bytestream2_put_be24(&pb, src32[5] >> 8);
401 - bytestream2_put_be24(&pb, src32[3] >> 8);
403 - } while (--samples);
408 - channels = avctx->channels;
409 - bytestream2_put_be24(&pb, (*src32++) >> 8);
410 - } while (--channels);
411 - bytestream2_put_be24(&pb, 0);
412 - } while (--samples);
415 - av_log(avctx, AV_LOG_ERROR, "this bitdepth not implemented!\n");
419 + /* remapping: L, R, C, LBack, RBack, LF */
420 + case AV_CH_LAYOUT_5POINT1_BACK:
421 + if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
423 + bytestream2_put_be16u(&pb, src16[0]);
424 + bytestream2_put_be16u(&pb, src16[1]);
425 + bytestream2_put_be16u(&pb, src16[2]);
426 + bytestream2_put_be16u(&pb, src16[4]);
427 + bytestream2_put_be16u(&pb, src16[5]);
428 + bytestream2_put_be16u(&pb, src16[3]);
430 + } while (--samples);
433 + bytestream2_put_be24u(&pb, src32[0] >> 8);
434 + bytestream2_put_be24u(&pb, src32[1] >> 8);
435 + bytestream2_put_be24u(&pb, src32[2] >> 8);
436 + bytestream2_put_be24u(&pb, src32[4] >> 8);
437 + bytestream2_put_be24u(&pb, src32[5] >> 8);
438 + bytestream2_put_be24u(&pb, src32[3] >> 8);
440 + } while (--samples);
443 + /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
444 + case AV_CH_LAYOUT_7POINT0:
445 + if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
447 + bytestream2_put_be16u(&pb, src16[0]);
448 + bytestream2_put_be16u(&pb, src16[1]);
449 + bytestream2_put_be16u(&pb, src16[2]);
450 + bytestream2_put_be16u(&pb, src16[5]);
451 + bytestream2_put_be16u(&pb, src16[3]);
452 + bytestream2_put_be16u(&pb, src16[4]);
453 + bytestream2_put_be16u(&pb, src16[6]);
455 + bytestream2_put_ne16(&pb, 0);
456 + } while (--samples);
459 + bytestream2_put_be24u(&pb, src32[0] >> 8);
460 + bytestream2_put_be24u(&pb, src32[1] >> 8);
461 + bytestream2_put_be24u(&pb, src32[2] >> 8);
462 + bytestream2_put_be24u(&pb, src32[5] >> 8);
463 + bytestream2_put_be24u(&pb, src32[3] >> 8);
464 + bytestream2_put_be24u(&pb, src32[4] >> 8);
465 + bytestream2_put_be24u(&pb, src32[6] >> 8);
467 + bytestream2_put_ne24(&pb, 0);
468 + } while (--samples);
471 + /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
472 + case AV_CH_LAYOUT_7POINT1:
473 + if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
475 + bytestream2_put_be16u(&pb, src16[0]);
476 + bytestream2_put_be16u(&pb, src16[1]);
477 + bytestream2_put_be16u(&pb, src16[2]);
478 + bytestream2_put_be16u(&pb, src16[6]);
479 + bytestream2_put_be16u(&pb, src16[4]);
480 + bytestream2_put_be16u(&pb, src16[5]);
481 + bytestream2_put_be16u(&pb, src16[7]);
482 + bytestream2_put_be16u(&pb, src16[3]);
484 + } while (--samples);
487 + bytestream2_put_be24u(&pb, src32[0]);
488 + bytestream2_put_be24u(&pb, src32[1]);
489 + bytestream2_put_be24u(&pb, src32[2]);
490 + bytestream2_put_be24u(&pb, src32[6]);
491 + bytestream2_put_be24u(&pb, src32[4]);
492 + bytestream2_put_be24u(&pb, src32[5]);
493 + bytestream2_put_be24u(&pb, src32[7]);
494 + bytestream2_put_be24u(&pb, src32[3]);
496 + } while (--samples);
500 + return AVERROR_BUG;
503 - avpkt->pts = frame->pts;
504 - avpkt->size = pkt_size;
505 + avpkt->pts = frame->pts;
506 avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
512 -AVCodec ff_pcm_bluray_encoder = {
513 - .name = "pcm_bluray",
514 - .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|24-bit big-endian for bluray media"),
515 - .type = AVMEDIA_TYPE_AUDIO,
516 - .id = AV_CODEC_ID_PCM_BLURAY,
517 - .priv_data_size = sizeof(PCMBDContext),
518 - .init = pcm_bd_encode_init,
519 - .encode2 = pcm_bd_encode_frame,
520 - .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
521 - .supported_samplerates = (const int[]) { 48000, 96000, 192000, 0},
522 - .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
523 - AV_CH_LAYOUT_STEREO,
524 - AV_CH_LAYOUT_5POINT1,
525 - AV_CH_LAYOUT_5POINT1_BACK,
526 - AV_CH_LAYOUT_7POINT1,
528 - .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
530 - AV_SAMPLE_FMT_NONE },
531 - .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
532 +const AVCodec ff_pcm_bluray_encoder = {
533 + .name = "pcm_bluray",
534 + .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
535 + .type = AVMEDIA_TYPE_AUDIO,
536 + .id = AV_CODEC_ID_PCM_BLURAY,
537 + .priv_data_size = sizeof(BlurayPCMEncContext),
538 + .init = pcm_bluray_encode_init,
539 + .encode2 = pcm_bluray_encode_frame,
540 + .supported_samplerates = (const int[]) { 48000, 96000, 192000, 0 },
541 +//#define FF_API_OLD_CHANNEL_LAYOUT 1
543 + .channel_layouts = (const uint64_t[]) {
545 + AV_CH_LAYOUT_STEREO,
546 + AV_CH_LAYOUT_SURROUND,
548 + AV_CH_LAYOUT_4POINT0,
550 + AV_CH_LAYOUT_5POINT0,
551 + AV_CH_LAYOUT_5POINT1_BACK,
552 + AV_CH_LAYOUT_7POINT0,
553 + AV_CH_LAYOUT_7POINT1,
557 + .p.ch_layouts = (const AVChannelLayout[]) {
558 + AV_CHANNEL_LAYOUT_MONO,
559 + AV_CHANNEL_LAYOUT_STEREO,
560 + AV_CHANNEL_LAYOUT_SURROUND,
561 + AV_CHANNEL_LAYOUT_2_1,
562 + AV_CHANNEL_LAYOUT_4POINT0,
563 + AV_CHANNEL_LAYOUT_2_2,
564 + AV_CHANNEL_LAYOUT_5POINT0,
565 + AV_CHANNEL_LAYOUT_5POINT1,
566 + AV_CHANNEL_LAYOUT_7POINT0,
567 + AV_CHANNEL_LAYOUT_7POINT1,
570 + .sample_fmts = (const enum AVSampleFormat[]) {
571 + AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE },
572 + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
573 + .capabilities = AV_CODEC_CAP_DR1,