annotate ffmpeg/libavcodec/libshine.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Interface to libshine for mp3 encoding
yading@10 3 * Copyright (c) 2012 Paul B Mahol
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 #include <shine/layer3.h>
yading@10 23
yading@10 24 #include "libavutil/intreadwrite.h"
yading@10 25 #include "audio_frame_queue.h"
yading@10 26 #include "avcodec.h"
yading@10 27 #include "internal.h"
yading@10 28 #include "mpegaudio.h"
yading@10 29 #include "mpegaudiodecheader.h"
yading@10 30
yading@10 31 #define BUFFER_SIZE (4096 * 20)
yading@10 32
yading@10 33 typedef struct SHINEContext {
yading@10 34 shine_config_t config;
yading@10 35 shine_t shine;
yading@10 36 uint8_t buffer[BUFFER_SIZE];
yading@10 37 int buffer_index;
yading@10 38 AudioFrameQueue afq;
yading@10 39 } SHINEContext;
yading@10 40
yading@10 41 static av_cold int libshine_encode_init(AVCodecContext *avctx)
yading@10 42 {
yading@10 43 SHINEContext *s = avctx->priv_data;
yading@10 44
yading@10 45 if (avctx->channels <= 0 || avctx->channels > 2){
yading@10 46 av_log(avctx, AV_LOG_ERROR, "only mono or stereo is supported\n");
yading@10 47 return AVERROR(EINVAL);
yading@10 48 }
yading@10 49
yading@10 50 shine_set_config_mpeg_defaults(&s->config.mpeg);
yading@10 51 if (avctx->bit_rate)
yading@10 52 s->config.mpeg.bitr = avctx->bit_rate / 1000;
yading@10 53 if (shine_find_bitrate_index(s->config.mpeg.bitr) < 0) {
yading@10 54 av_log(avctx, AV_LOG_ERROR, "invalid bitrate\n");
yading@10 55 return AVERROR(EINVAL);
yading@10 56 }
yading@10 57 s->config.mpeg.mode = avctx->channels == 2 ? STEREO : MONO;
yading@10 58 s->config.wave.samplerate = avctx->sample_rate;
yading@10 59 s->config.wave.channels = avctx->channels == 2 ? PCM_STEREO : PCM_MONO;
yading@10 60 s->shine = shine_initialise(&s->config);
yading@10 61 if (!s->shine)
yading@10 62 return AVERROR(ENOMEM);
yading@10 63 avctx->frame_size = samp_per_frame;
yading@10 64 ff_af_queue_init(avctx, &s->afq);
yading@10 65 return 0;
yading@10 66 }
yading@10 67
yading@10 68 static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
yading@10 69 const AVFrame *frame, int *got_packet_ptr)
yading@10 70 {
yading@10 71 SHINEContext *s = avctx->priv_data;
yading@10 72 MPADecodeHeader hdr;
yading@10 73 unsigned char *data;
yading@10 74 long written;
yading@10 75 int ret, len;
yading@10 76
yading@10 77 if (frame)
yading@10 78 data = shine_encode_frame(s->shine, frame->data[0], &written);
yading@10 79 else
yading@10 80 data = shine_flush(s->shine, &written);
yading@10 81 if (written < 0)
yading@10 82 return -1;
yading@10 83 if (written > 0) {
yading@10 84 if (s->buffer_index + written > BUFFER_SIZE) {
yading@10 85 av_log(avctx, AV_LOG_ERROR, "internal buffer too small\n");
yading@10 86 return AVERROR_BUG;
yading@10 87 }
yading@10 88 memcpy(s->buffer + s->buffer_index, data, written);
yading@10 89 s->buffer_index += written;
yading@10 90 }
yading@10 91 if (frame) {
yading@10 92 if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
yading@10 93 return ret;
yading@10 94 }
yading@10 95
yading@10 96 if (s->buffer_index < 4 || !s->afq.frame_count)
yading@10 97 return 0;
yading@10 98 if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
yading@10 99 av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
yading@10 100 return -1;
yading@10 101 }
yading@10 102
yading@10 103 len = hdr.frame_size;
yading@10 104 if (len <= s->buffer_index) {
yading@10 105 if ((ret = ff_alloc_packet2(avctx, avpkt, len)))
yading@10 106 return ret;
yading@10 107 memcpy(avpkt->data, s->buffer, len);
yading@10 108 s->buffer_index -= len;
yading@10 109 memmove(s->buffer, s->buffer + len, s->buffer_index);
yading@10 110
yading@10 111 ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
yading@10 112 &avpkt->duration);
yading@10 113
yading@10 114 avpkt->size = len;
yading@10 115 *got_packet_ptr = 1;
yading@10 116 }
yading@10 117 return 0;
yading@10 118 }
yading@10 119
yading@10 120 static av_cold int libshine_encode_close(AVCodecContext *avctx)
yading@10 121 {
yading@10 122 SHINEContext *s = avctx->priv_data;
yading@10 123
yading@10 124 ff_af_queue_close(&s->afq);
yading@10 125 shine_close(s->shine);
yading@10 126 return 0;
yading@10 127 }
yading@10 128
yading@10 129 static const int libshine_sample_rates[] = {
yading@10 130 44100, 48000, 32000, 0
yading@10 131 };
yading@10 132
yading@10 133 AVCodec ff_libshine_encoder = {
yading@10 134 .name = "libshine",
yading@10 135 .type = AVMEDIA_TYPE_AUDIO,
yading@10 136 .id = CODEC_ID_MP3,
yading@10 137 .priv_data_size = sizeof(SHINEContext),
yading@10 138 .init = libshine_encode_init,
yading@10 139 .encode2 = libshine_encode_frame,
yading@10 140 .close = libshine_encode_close,
yading@10 141 .capabilities = CODEC_CAP_DELAY,
yading@10 142 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P,
yading@10 143 AV_SAMPLE_FMT_NONE },
yading@10 144 .supported_samplerates = libshine_sample_rates,
yading@10 145 .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
yading@10 146 AV_CH_LAYOUT_STEREO,
yading@10 147 0 },
yading@10 148 .long_name = NULL_IF_CONFIG_SMALL("libshine MP3 (MPEG audio layer 3)"),
yading@10 149 };