FFmpeg
libshine.c
Go to the documentation of this file.
1 /*
2  * Interface to libshine for mp3 encoding
3  * Copyright (c) 2012 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 <shine/layer3.h>
23 
25 #include "libavutil/intreadwrite.h"
26 #include "audio_frame_queue.h"
27 #include "avcodec.h"
28 #include "codec_internal.h"
29 #include "encode.h"
30 #include "mpegaudio.h"
31 #include "mpegaudiodecheader.h"
32 
33 #define BUFFER_SIZE (4096 * 20)
34 
35 typedef struct SHINEContext {
36  shine_config_t config;
37  shine_t shine;
38  uint8_t buffer[BUFFER_SIZE];
41 } SHINEContext;
42 
44 {
45  SHINEContext *s = avctx->priv_data;
46 
47  shine_set_config_mpeg_defaults(&s->config.mpeg);
48  if (avctx->bit_rate)
49  s->config.mpeg.bitr = avctx->bit_rate / 1000;
50  s->config.mpeg.mode = avctx->ch_layout.nb_channels == 2 ? STEREO : MONO;
51  s->config.wave.samplerate = avctx->sample_rate;
52  s->config.wave.channels = avctx->ch_layout.nb_channels == 2 ? PCM_STEREO : PCM_MONO;
53  if (shine_check_config(s->config.wave.samplerate, s->config.mpeg.bitr) < 0) {
54  av_log(avctx, AV_LOG_ERROR, "invalid configuration\n");
55  return AVERROR(EINVAL);
56  }
57  s->shine = shine_initialise(&s->config);
58  if (!s->shine)
59  return AVERROR(ENOMEM);
60  avctx->frame_size = shine_samples_per_pass(s->shine);
61  ff_af_queue_init(avctx, &s->afq);
62  return 0;
63 }
64 
65 static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
66  const AVFrame *frame, int *got_packet_ptr)
67 {
68  SHINEContext *s = avctx->priv_data;
69  MPADecodeHeader hdr;
70  unsigned char *data;
71  int written;
72  int ret, len;
73 
74  if (frame)
75  data = shine_encode_buffer(s->shine, (int16_t **)frame->data, &written);
76  else
77  data = shine_flush(s->shine, &written);
78  if (written < 0)
79  return -1;
80  if (written > 0) {
81  if (s->buffer_index + written > BUFFER_SIZE) {
82  av_log(avctx, AV_LOG_ERROR, "internal buffer too small\n");
83  return AVERROR_BUG;
84  }
85  memcpy(s->buffer + s->buffer_index, data, written);
86  s->buffer_index += written;
87  }
88  if (frame) {
89  if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
90  return ret;
91  }
92 
93  if (s->buffer_index < 4 || !s->afq.frame_count)
94  return 0;
95  if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
96  av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
97  return -1;
98  }
99 
100  len = hdr.frame_size;
101  if (len <= s->buffer_index) {
102  if ((ret = ff_get_encode_buffer(avctx, avpkt, len, 0)))
103  return ret;
104  memcpy(avpkt->data, s->buffer, len);
105  s->buffer_index -= len;
106  memmove(s->buffer, s->buffer + len, s->buffer_index);
107 
108  ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
109  &avpkt->duration);
110 
111  *got_packet_ptr = 1;
112  }
113  return 0;
114 }
115 
117 {
118  SHINEContext *s = avctx->priv_data;
119 
120  ff_af_queue_close(&s->afq);
121  shine_close(s->shine);
122  return 0;
123 }
124 
125 static const int libshine_sample_rates[] = {
126  44100, 48000, 32000, 0
127 };
128 
130  .p.name = "libshine",
131  CODEC_LONG_NAME("libshine MP3 (MPEG audio layer 3)"),
132  .p.type = AVMEDIA_TYPE_AUDIO,
133  .p.id = AV_CODEC_ID_MP3,
134  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
135  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
136  .priv_data_size = sizeof(SHINEContext),
139  .close = libshine_encode_close,
140  .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P,
142  .p.supported_samplerates = libshine_sample_rates,
144  .p.ch_layouts = (const AVChannelLayout[]) { AV_CHANNEL_LAYOUT_MONO,
146  { 0 },
147  },
148  .p.wrapper_name = "libshine",
149 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1062
libshine_encode_init
static av_cold int libshine_encode_init(AVCodecContext *avctx)
Definition: libshine.c:43
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
ff_af_queue_remove
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
Definition: audio_frame_queue.c:75
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:369
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1034
ff_af_queue_close
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
Definition: audio_frame_queue.c:36
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:210
ff_af_queue_init
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
Definition: audio_frame_queue.c:28
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
mpegaudiodecheader.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
libshine_encode_close
static av_cold int libshine_encode_close(AVCodecContext *avctx)
Definition: libshine.c:116
encode.h
data
const char data[16]
Definition: mxf.c:146
MPADecodeHeader
Definition: mpegaudiodecheader.h:47
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:34
BUFFER_SIZE
#define BUFFER_SIZE
Definition: libshine.c:33
FFCodec
Definition: codec_internal.h:127
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
STEREO
#define STEREO
Definition: cook.c:64
SHINEContext::buffer
uint8_t buffer[BUFFER_SIZE]
Definition: libshine.c:38
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2054
audio_frame_queue.h
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:439
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:315
ff_af_queue_add
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
Definition: audio_frame_queue.c:44
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:211
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
avpriv_mpegaudio_decode_header
int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
Definition: mpegaudiodecheader.c:34
av_cold
#define av_cold
Definition: attributes.h:90
SHINEContext
Definition: libshine.c:35
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
libshine_sample_rates
static const int libshine_sample_rates[]
Definition: libshine.c:125
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
CODEC_OLD_CHANNEL_LAYOUTS
#define CODEC_OLD_CHANNEL_LAYOUTS(...)
Definition: codec_internal.h:302
AudioFrameQueue
Definition: audio_frame_queue.h:32
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
ff_libshine_encoder
const FFCodec ff_libshine_encoder
Definition: libshine.c:129
SHINEContext::shine
shine_t shine
Definition: libshine.c:37
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:476
SHINEContext::afq
AudioFrameQueue afq
Definition: libshine.c:40
SHINEContext::config
shine_config_t config
Definition: libshine.c:36
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
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:301
codec_internal.h
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
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
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
len
int len
Definition: vorbis_enc_data.h:426
mpegaudio.h
avcodec.h
MONO
#define MONO
Definition: cook.c:63
ret
ret
Definition: filter_design.txt:187
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
libshine_encode_frame
static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libshine.c:65
AVCodecContext
main external API structure.
Definition: avcodec.h:426
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
SHINEContext::buffer_index
int buffer_index
Definition: libshine.c:39
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:368
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:453
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27