FFmpeg
Loading...
Searching...
No Matches
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
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/**
36 * Delay of the analysis filterbank, polyphase (240) and MDCT (288), as broken
37 * down in LAME's libmp3lame/encoder.h. shine adds no padding of its own.
38 */
39#define SHINE_ENCODER_DELAY 528
40
41typedef struct SHINEContext {
42 shine_config_t config;
43 shine_t shine;
48
50{
51 SHINEContext *s = avctx->priv_data;
52
53 shine_set_config_mpeg_defaults(&s->config.mpeg);
54 if (avctx->bit_rate)
55 s->config.mpeg.bitr = avctx->bit_rate / 1000;
56 s->config.mpeg.mode = avctx->ch_layout.nb_channels == 2 ? STEREO : MONO;
57 s->config.wave.samplerate = avctx->sample_rate;
58 s->config.wave.channels = avctx->ch_layout.nb_channels == 2 ? PCM_STEREO : PCM_MONO;
59 if (shine_check_config(s->config.wave.samplerate, s->config.mpeg.bitr) < 0) {
60 av_log(avctx, AV_LOG_ERROR, "invalid configuration\n");
61 return AVERROR(EINVAL);
62 }
63 s->shine = shine_initialise(&s->config);
64 if (!s->shine)
65 return AVERROR(ENOMEM);
66 avctx->frame_size = shine_samples_per_pass(s->shine);
68 ff_af_queue_init(avctx, &s->afq);
69 return 0;
70}
71
73 const AVFrame *frame, int *got_packet_ptr)
74{
75 SHINEContext *s = avctx->priv_data;
77 unsigned char *data;
78 int written;
79 int ret, len;
80
81 if (frame)
82 data = shine_encode_buffer(s->shine, (int16_t **)frame->data, &written);
83 else
84 data = shine_flush(s->shine, &written);
85 if (written < 0)
86 return -1;
87 if (written > 0) {
88 if (s->buffer_index + written > BUFFER_SIZE) {
89 av_log(avctx, AV_LOG_ERROR, "internal buffer too small\n");
90 return AVERROR_BUG;
91 }
92 memcpy(s->buffer + s->buffer_index, data, written);
93 s->buffer_index += written;
94 }
95 if (frame) {
96 if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
97 return ret;
98 }
99
100 if (s->buffer_index < 4 || !s->afq.frame_count)
101 return 0;
102 if (ff_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
103 av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
104 return -1;
105 }
106
107 len = hdr.frame_size;
108 if (len <= s->buffer_index) {
109 if ((ret = ff_get_encode_buffer(avctx, avpkt, len, 0)))
110 return ret;
111 memcpy(avpkt->data, s->buffer, len);
112 s->buffer_index -= len;
113 memmove(s->buffer, s->buffer + len, s->buffer_index);
114
115 ret = ff_af_queue_remove(&s->afq, avctx->frame_size, avpkt);
116 if (ret < 0)
117 return ret;
118
119 *got_packet_ptr = 1;
120 }
121 return 0;
122}
123
125{
126 SHINEContext *s = avctx->priv_data;
127
128 ff_af_queue_close(&s->afq);
129 shine_close(s->shine);
130 return 0;
131}
132
133static const int libshine_sample_rates[] = {
134 44100, 48000, 32000, 0
135};
136
138 .p.name = "libshine",
139 CODEC_LONG_NAME("libshine MP3 (MPEG audio layer 3)"),
140 .p.type = AVMEDIA_TYPE_AUDIO,
141 .p.id = AV_CODEC_ID_MP3,
142 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
143 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
144 .priv_data_size = sizeof(SHINEContext),
147 .close = libshine_encode_close,
151 .p.wrapper_name = "libshine",
152};
const FFCodec ff_libshine_encoder
Definition libshine.c:137
av_cold void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, AVPacket *pkt)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
Libavcodec external API header.
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define CODEC_SAMPLERATES_ARRAY(array)
#define CODEC_CH_LAYOUTS(...)
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
#define STEREO
Definition cook.c:65
#define MONO
Definition cook.c:64
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:107
int ret
Definition ffplay.c:391
#define BUFFER_SIZE
#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:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:460
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#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_S16P
signed 16 bits, planar
Definition samplefmt.h:64
#define AV_RB32(p)
#define av_cold
Definition attributes.h:117
#define SHINE_ENCODER_DELAY
Delay of the analysis filterbank, polyphase (240) and MDCT (288), as broken down in LAME's libmp3lame...
Definition libshine.c:39
static av_cold int libshine_encode_init(AVCodecContext *avctx)
Definition libshine.c:49
static const int libshine_sample_rates[]
Definition libshine.c:133
static av_cold int libshine_encode_close(AVCodecContext *avctx)
Definition libshine.c:124
#define BUFFER_SIZE
Definition libshine.c:33
static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition libshine.c:72
mpeg audio declarations for both encoder and decoder.
#define MP3_DECODER_DELAY
Delay of the layer III synthesis filterbank, in samples.
Definition mpegaudio.h:46
int ff_mpegaudio_decode_header(MPADecodeHeader2 *s, uint32_t header)
MPEG Audio header decoder.
const char data[16]
Definition mxf.c:155
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:1059
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int initial_padding
Audio only.
Definition avcodec.h:1118
int sample_rate
samples per second
Definition avcodec.h:1044
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1072
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
uint8_t * data
Definition packet.h:610
Same as MPADecodeHeader, plus the header fields that carry no decoding information.
AudioFrameQueue afq
Definition libshine.c:46
uint8_t buffer[BUFFER_SIZE]
Definition libshine.c:44
shine_t shine
Definition libshine.c:43
int buffer_index
Definition libshine.c:45
shine_config_t config
Definition libshine.c:42
#define av_log(a,...)
int len