FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
24 #include "libavutil/intreadwrite.h"
25 #include "audio_frame_queue.h"
26 #include "avcodec.h"
27 #include "internal.h"
28 #include "mpegaudio.h"
29 #include "mpegaudiodecheader.h"
30 
31 #define BUFFER_SIZE (4096 * 20)
32 
33 typedef struct SHINEContext {
34  shine_config_t config;
35  shine_t shine;
39 } SHINEContext;
40 
42 {
43  SHINEContext *s = avctx->priv_data;
44 
45  if (avctx->channels <= 0 || avctx->channels > 2){
46  av_log(avctx, AV_LOG_ERROR, "only mono or stereo is supported\n");
47  return AVERROR(EINVAL);
48  }
49 
50  shine_set_config_mpeg_defaults(&s->config.mpeg);
51  if (avctx->bit_rate)
52  s->config.mpeg.bitr = avctx->bit_rate / 1000;
53  s->config.mpeg.mode = avctx->channels == 2 ? STEREO : MONO;
54  s->config.wave.samplerate = avctx->sample_rate;
55  s->config.wave.channels = avctx->channels == 2 ? PCM_STEREO : PCM_MONO;
56  if (shine_check_config(s->config.wave.samplerate, s->config.mpeg.bitr) < 0) {
57  av_log(avctx, AV_LOG_ERROR, "invalid configuration\n");
58  return AVERROR(EINVAL);
59  }
60  s->shine = shine_initialise(&s->config);
61  if (!s->shine)
62  return AVERROR(ENOMEM);
63  avctx->frame_size = shine_samples_per_pass(s->shine);
64  ff_af_queue_init(avctx, &s->afq);
65  return 0;
66 }
67 
68 static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
69  const AVFrame *frame, int *got_packet_ptr)
70 {
71  SHINEContext *s = avctx->priv_data;
72  MPADecodeHeader hdr;
73  unsigned char *data;
74  int written;
75  int ret, len;
76 
77  if (frame)
78  data = shine_encode_buffer(s->shine, (int16_t **)frame->data, &written);
79  else
80  data = shine_flush(s->shine, &written);
81  if (written < 0)
82  return -1;
83  if (written > 0) {
84  if (s->buffer_index + written > BUFFER_SIZE) {
85  av_log(avctx, AV_LOG_ERROR, "internal buffer too small\n");
86  return AVERROR_BUG;
87  }
88  memcpy(s->buffer + s->buffer_index, data, written);
89  s->buffer_index += written;
90  }
91  if (frame) {
92  if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
93  return ret;
94  }
95 
96  if (s->buffer_index < 4 || !s->afq.frame_count)
97  return 0;
99  av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
100  return -1;
101  }
102 
103  len = hdr.frame_size;
104  if (len <= s->buffer_index) {
105  if ((ret = ff_alloc_packet2(avctx, avpkt, len, 0)))
106  return ret;
107  memcpy(avpkt->data, s->buffer, len);
108  s->buffer_index -= len;
109  memmove(s->buffer, s->buffer + len, s->buffer_index);
110 
111  ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
112  &avpkt->duration);
113 
114  avpkt->size = len;
115  *got_packet_ptr = 1;
116  }
117  return 0;
118 }
119 
121 {
122  SHINEContext *s = avctx->priv_data;
123 
124  ff_af_queue_close(&s->afq);
125  shine_close(s->shine);
126  return 0;
127 }
128 
129 static const int libshine_sample_rates[] = {
130  44100, 48000, 32000, 0
131 };
132 
134  .name = "libshine",
135  .long_name = NULL_IF_CONFIG_SMALL("libshine MP3 (MPEG audio layer 3)"),
136  .type = AVMEDIA_TYPE_AUDIO,
137  .id = AV_CODEC_ID_MP3,
138  .priv_data_size = sizeof(SHINEContext),
140  .encode2 = libshine_encode_frame,
141  .close = libshine_encode_close,
142  .capabilities = AV_CODEC_CAP_DELAY,
143  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P,
145  .supported_samplerates = libshine_sample_rates,
146  .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
148  0 },
149 };
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
const char * s
Definition: avisynth_c.h:768
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
shine_config_t config
Definition: libshine.c:34
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1797
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1658
#define AV_CH_LAYOUT_STEREO
AVCodec.
Definition: avcodec.h:3681
#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: avcodec.h:1019
uint8_t
#define av_cold
Definition: attributes.h:82
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1675
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:87
static av_cold int libshine_encode_close(AVCodecContext *avctx)
Definition: libshine.c:120
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1657
int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:550
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
uint8_t buffer[BUFFER_SIZE]
Definition: libshine.c:36
static av_cold int libshine_encode_init(AVCodecContext *avctx)
Definition: libshine.c:41
#define BUFFER_SIZE
Definition: libshine.c:31
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2514
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AVCodec ff_libshine_encoder
Definition: libshine.c:133
int sample_rate
samples per second
Definition: avcodec.h:2494
main external API structure.
Definition: avcodec.h:1732
AudioFrameQueue afq
Definition: libshine.c:38
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
#define MONO
Definition: cook.c:60
int buffer_index
Definition: libshine.c:37
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1736
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:201
MPEG Audio header decoder.
common internal api header.
#define STEREO
Definition: cook.c:61
static const int libshine_sample_rates[]
Definition: libshine.c:129
mpeg audio declarations for both encoder and decoder.
void * priv_data
Definition: avcodec.h:1774
int len
int channels
number of audio channels
Definition: avcodec.h:2495
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libshine.c:68
signed 16 bits, planar
Definition: samplefmt.h:67
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:1634
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1650
shine_t shine
Definition: libshine.c:35
GLuint buffer
Definition: opengl_enc.c:102