FFmpeg
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 
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
37 
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  }
51 
52  switch (avctx->sample_fmt) {
53  case AV_SAMPLE_FMT_S16:
54  avctx->bits_per_coded_sample = 16;
55  quant = 0;
56  break;
57  case AV_SAMPLE_FMT_S32:
58  avctx->bits_per_coded_sample = 24;
59  quant = 2;
60  break;
61  }
62 
63  avctx->bits_per_coded_sample = 16 + quant * 4;
64  avctx->block_align = avctx->ch_layout.nb_channels * avctx->bits_per_coded_sample / 8;
65  avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate;
66  if (avctx->bit_rate > 9800000) {
67  av_log(avctx, AV_LOG_ERROR, "Too big bitrate: reduce sample rate, bitdepth or channels.\n");
68  return AVERROR(EINVAL);
69  }
70 
71  if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
72  s->samples_per_block = 1;
73  s->block_size = avctx->ch_layout.nb_channels * 2;
74  frame_size = 2008 / s->block_size;
75  } else {
76  switch (avctx->ch_layout.nb_channels) {
77  case 1:
78  case 2:
79  case 4:
80  /* one group has all the samples needed */
81  s->block_size = 4 * avctx->bits_per_coded_sample / 8;
82  s->samples_per_block = 4 / avctx->ch_layout.nb_channels;
83  s->groups_per_block = 1;
84  break;
85  case 8:
86  /* two groups have all the samples needed */
87  s->block_size = 8 * avctx->bits_per_coded_sample / 8;
88  s->samples_per_block = 1;
89  s->groups_per_block = 2;
90  break;
91  default:
92  /* need avctx->ch_layout.nb_channels groups */
93  s->block_size = 4 * avctx->ch_layout.nb_channels *
94  avctx->bits_per_coded_sample / 8;
95  s->samples_per_block = 4;
96  s->groups_per_block = avctx->ch_layout.nb_channels;
97  break;
98  }
99 
100  frame_size = FFALIGN(2008 / s->block_size, s->samples_per_block);
101  }
102 
103  s->header[0] = 0x0c;
104  s->header[1] = (quant << 6) | (freq << 4) | (avctx->ch_layout.nb_channels - 1);
105  s->header[2] = 0x80;
106 
107  if (!avctx->frame_size)
108  avctx->frame_size = frame_size;
109 
110  return 0;
111 }
112 
113 static int pcm_dvd_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
114  const AVFrame *frame, int *got_packet_ptr)
115 {
116  PCMDVDContext *s = avctx->priv_data;
117  int samples = frame->nb_samples * avctx->ch_layout.nb_channels;
118  int64_t pkt_size = (frame->nb_samples / s->samples_per_block) * s->block_size + 3;
119  int blocks = (pkt_size - 3) / s->block_size;
120  const int16_t *src16;
121  const int32_t *src32;
122  PutByteContext pb;
123  int ret;
124 
125  if ((ret = ff_get_encode_buffer(avctx, avpkt, pkt_size, 0)) < 0)
126  return ret;
127 
128  memcpy(avpkt->data, s->header, 3);
129 
130  src16 = (const int16_t *)frame->data[0];
131  src32 = (const int32_t *)frame->data[0];
132 
133  bytestream2_init_writer(&pb, avpkt->data + 3, avpkt->size - 3);
134 
135  switch (avctx->sample_fmt) {
136  case AV_SAMPLE_FMT_S16:
137  do {
138  bytestream2_put_be16(&pb, *src16++);
139  } while (--samples);
140  break;
141  case AV_SAMPLE_FMT_S32:
142  if (avctx->ch_layout.nb_channels == 1) {
143  do {
144  for (int i = 2; i; i--) {
145  bytestream2_put_be16(&pb, src32[0] >> 16);
146  bytestream2_put_be16(&pb, src32[1] >> 16);
147  bytestream2_put_byte(&pb, (*src32++) >> 24);
148  bytestream2_put_byte(&pb, (*src32++) >> 24);
149  }
150  } while (--blocks);
151  } else {
152  do {
153  for (int i = s->groups_per_block; i; i--) {
154  bytestream2_put_be16(&pb, src32[0] >> 16);
155  bytestream2_put_be16(&pb, src32[1] >> 16);
156  bytestream2_put_be16(&pb, src32[2] >> 16);
157  bytestream2_put_be16(&pb, src32[3] >> 16);
158  bytestream2_put_byte(&pb, (*src32++) >> 24);
159  bytestream2_put_byte(&pb, (*src32++) >> 24);
160  bytestream2_put_byte(&pb, (*src32++) >> 24);
161  bytestream2_put_byte(&pb, (*src32++) >> 24);
162  }
163  } while (--blocks);
164  }
165  break;
166  }
167 
168  avpkt->pts = frame->pts;
169  avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
170  *got_packet_ptr = 1;
171 
172  return 0;
173 }
174 
176  .p.name = "pcm_dvd",
177  .p.long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for DVD media"),
178  .p.type = AVMEDIA_TYPE_AUDIO,
179  .p.id = AV_CODEC_ID_PCM_DVD,
181  .priv_data_size = sizeof(PCMDVDContext),
184  .p.supported_samplerates = (const int[]) { 48000, 96000, 0},
185 #if FF_API_OLD_CHANNEL_LAYOUT
186  .p.channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
190  0 },
191 #endif
192  .p.ch_layouts = (const AVChannelLayout[]) { AV_CHANNEL_LAYOUT_MONO,
196  { 0 } },
197  .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
200  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
201 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1026
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:998
AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_5POINT1
Definition: channel_layout.h:364
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:204
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
pcm_dvd_encode_init
static av_cold int pcm_dvd_encode_init(AVCodecContext *avctx)
Definition: pcm-dvdenc.c:38
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
encode.h
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:353
FFCodec
Definition: codec_internal.h:112
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:354
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
ff_pcm_dvd_encoder
const FFCodec ff_pcm_dvd_encoder
Definition: pcm-dvdenc.c:175
init
static int init
Definition: av_tx.c:47
PCMDVDContext::block_size
int block_size
Definition: pcm-dvd.c:34
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2057
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:263
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:205
quant
static int quant(float coef, const float Q, const float rounding)
Quantize one coefficient.
Definition: aacenc_utils.h:59
ff_samples_to_time_base
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition: internal.h:173
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
PCMDVDContext::groups_per_block
int groups_per_block
Definition: pcm-dvd.c:37
bytestream2_init_writer
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:147
s
#define s(width, name)
Definition: cbs_vp9.c:256
PCMDVDContext
Definition: pcm-dvd.c:32
frame_size
int frame_size
Definition: mxfenc.c:2201
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
PCMDVDContext::extra_samples
uint8_t * extra_samples
Definition: pcm-dvdenc.c:34
AV_CODEC_ID_PCM_DVD
@ AV_CODEC_ID_PCM_DVD
Definition: codec_id.h:335
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:439
AV_CH_LAYOUT_5POINT1
#define AV_CH_LAYOUT_5POINT1
Definition: channel_layout.h:215
AV_CHANNEL_LAYOUT_7POINT1
#define AV_CHANNEL_LAYOUT_7POINT1
Definition: channel_layout.h:375
PCMDVDContext::extra_sample_count
int extra_sample_count
Definition: pcm-dvd.c:38
PutByteContext
Definition: bytestream.h:37
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:116
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:290
codec_internal.h
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1014
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
PCMDVDContext::header
uint8_t header[3]
Definition: pcm-dvdenc.c:30
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1441
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AV_CH_LAYOUT_7POINT1
#define AV_CH_LAYOUT_7POINT1
Definition: channel_layout.h:226
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: avcodec.h:1043
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:389
channel_layout.h
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:79
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
pcm_dvd_encode_frame
static int pcm_dvd_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: pcm-dvdenc.c:113
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
PCMDVDContext::samples_per_block
int samples_per_block
Definition: pcm-dvd.c:36
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:87
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:59