FFmpeg
Loading...
Searching...
No Matches
pcm-dvdenc.c
Go to the documentation of this file.
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/avassert.h"
24#include "avcodec.h"
25#include "bytestream.h"
26#include "codec_internal.h"
27#include "encode.h"
28
29typedef 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
35
37{
38 PCMDVDContext *s = avctx->priv_data;
39 int quant, freq, frame_size;
40
41 switch (avctx->sample_rate) {
42 case 48000:
43 freq = 0;
44 break;
45 case 96000:
46 freq = 1;
47 break;
48 default:
49 av_unreachable("Already checked via CODEC_SAMPLERATES");
50 }
51
52 switch (avctx->sample_fmt) {
54 avctx->bits_per_coded_sample = 16;
55 quant = 0;
56 break;
58 avctx->bits_per_coded_sample = 24;
59 quant = 2;
60 break;
61 default:
62 av_unreachable("Already checked via CODEC_SAMPLEFMTS");
63 }
64
65 avctx->bits_per_coded_sample = 16 + quant * 4;
66 avctx->block_align = avctx->ch_layout.nb_channels * avctx->bits_per_coded_sample / 8;
67 avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate;
68 if (avctx->bit_rate > 9800000) {
69 av_log(avctx, AV_LOG_ERROR, "Too big bitrate: reduce sample rate, bitdepth or channels.\n");
70 return AVERROR(EINVAL);
71 }
72
73 if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
74 s->samples_per_block = 1;
75 s->block_size = avctx->ch_layout.nb_channels * 2;
76 frame_size = 2008 / s->block_size;
77 } else {
78 switch (avctx->ch_layout.nb_channels) {
79 case 1:
80 case 2:
81 case 4:
82 /* one group has all the samples needed */
83 s->block_size = 4 * avctx->bits_per_coded_sample / 8;
84 s->samples_per_block = 4 / avctx->ch_layout.nb_channels;
85 s->groups_per_block = 1;
86 break;
87 case 8:
88 /* two groups have all the samples needed */
89 s->block_size = 8 * avctx->bits_per_coded_sample / 8;
90 s->samples_per_block = 1;
91 s->groups_per_block = 2;
92 break;
93 default:
94 /* need avctx->ch_layout.nb_channels groups */
95 s->block_size = 4 * avctx->ch_layout.nb_channels *
96 avctx->bits_per_coded_sample / 8;
97 s->samples_per_block = 4;
98 s->groups_per_block = avctx->ch_layout.nb_channels;
99 break;
100 }
101
102 frame_size = FFALIGN(2008 / s->block_size, s->samples_per_block);
103 }
104
105 s->header[0] = 0x0c;
106 s->header[1] = (quant << 6) | (freq << 4) | (avctx->ch_layout.nb_channels - 1);
107 s->header[2] = 0x80;
108
109 avctx->frame_size = frame_size;
110
111 return 0;
112}
113
115 const AVFrame *frame, int *got_packet_ptr)
116{
117 PCMDVDContext *s = avctx->priv_data;
118 int samples = frame->nb_samples * avctx->ch_layout.nb_channels;
119 int64_t pkt_size = (int64_t)(frame->nb_samples / s->samples_per_block) * s->block_size + 3;
120 int blocks = (pkt_size - 3) / s->block_size;
121 const int16_t *src16;
122 const int32_t *src32;
124 int ret;
125
126 if ((ret = ff_get_encode_buffer(avctx, avpkt, pkt_size, 0)) < 0)
127 return ret;
128
129 memcpy(avpkt->data, s->header, 3);
130
131 src16 = (const int16_t *)frame->data[0];
132 src32 = (const int32_t *)frame->data[0];
133
134 bytestream2_init_writer(&pb, avpkt->data + 3, avpkt->size - 3);
135
136 switch (avctx->sample_fmt) {
138 do {
139 bytestream2_put_be16(&pb, *src16++);
140 } while (--samples);
141 break;
143 if (avctx->ch_layout.nb_channels == 1) {
144 do {
145 for (int i = 2; i; i--) {
146 bytestream2_put_be16(&pb, src32[0] >> 16);
147 bytestream2_put_be16(&pb, src32[1] >> 16);
148 bytestream2_put_byte(&pb, (uint8_t)((*src32++) >> 8));
149 bytestream2_put_byte(&pb, (uint8_t)((*src32++) >> 8));
150 }
151 } while (--blocks);
152 } else {
153 do {
154 for (int i = s->groups_per_block; i; i--) {
155 bytestream2_put_be16(&pb, src32[0] >> 16);
156 bytestream2_put_be16(&pb, src32[1] >> 16);
157 bytestream2_put_be16(&pb, src32[2] >> 16);
158 bytestream2_put_be16(&pb, src32[3] >> 16);
159 bytestream2_put_byte(&pb, (uint8_t)((*src32++) >> 8));
160 bytestream2_put_byte(&pb, (uint8_t)((*src32++) >> 8));
161 bytestream2_put_byte(&pb, (uint8_t)((*src32++) >> 8));
162 bytestream2_put_byte(&pb, (uint8_t)((*src32++) >> 8));
163 }
164 } while (--blocks);
165 }
166 break;
167 }
168
169 *got_packet_ptr = 1;
170
171 return 0;
172}
173
const FFCodec ff_pcm_dvd_encoder
Definition pcm-dvdenc.c:174
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
Libavcodec external API header.
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition bytestream.h:147
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define CODEC_CH_LAYOUTS(...)
#define CODEC_SAMPLERATES(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
long long int64_t
Definition coverity.c:34
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
static const uint8_t frame_size[4]
Definition g723_1.h:222
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition codec.h:84
@ AV_CODEC_ID_PCM_DVD
Definition codec_id.h:349
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_7POINT1
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition samplefmt.h:59
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define av_cold
Definition attributes.h:117
#define FFALIGN(x, a)
Definition macros.h:78
static av_cold int pcm_dvd_encode_init(AVCodecContext *avctx)
Definition pcm-dvdenc.c:36
static int pcm_dvd_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition pcm-dvdenc.c:114
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int sample_rate
samples per second
Definition avcodec.h:1040
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition avcodec.h:1075
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
int block_size
Definition pcm-dvd.c:34
int groups_per_block
Definition pcm-dvd.c:37
int samples_per_block
Definition pcm-dvd.c:36
uint8_t header[3]
Definition pcm-dvdenc.c:30
#define av_log(a,...)
static const uint8_t quant[64]
Definition vmixdec.c:71