3808
|
1 /*
|
|
2 * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
|
|
3 *
|
|
4 * This library is free software; you can redistribute it and/or
|
|
5 * modify it under the terms of the GNU Lesser General Public
|
|
6 * License as published by the Free Software Foundation; either
|
|
7 * version 2 of the License, or (at your option) any later version.
|
|
8 *
|
|
9 * This library is distributed in the hope that it will be useful,
|
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12 * Lesser General Public License for more details.
|
|
13 *
|
|
14 * You should have received a copy of the GNU Lesser General Public
|
|
15 * License along with this library; if not, write to the Free Software
|
|
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
17 */
|
|
18
|
|
19 /**
|
|
20 * @file vorbis_enc.c
|
|
21 * Native Vorbis encoder.
|
|
22 * @author Oded Shimon <ods15@ods15.dyndns.org>
|
|
23 */
|
|
24
|
|
25 #include "avcodec.h"
|
|
26
|
|
27 #undef NDEBUG
|
|
28 #include <assert.h>
|
|
29
|
|
30 #define VORBIS_FRAME_SIZE 64
|
|
31
|
|
32 #define BUFFER_SIZE (1024*64)
|
|
33
|
|
34 typedef struct {
|
|
35 uint8_t buffer[BUFFER_SIZE];
|
|
36 int buffer_index;
|
|
37 } venc_context_t;
|
|
38
|
|
39 static int vorbis_encode_init(AVCodecContext * avccontext) {
|
|
40 venc_context_t * venc = avccontext->priv_data;
|
|
41 uint8_t *p;
|
|
42 unsigned int offset, len;
|
|
43
|
|
44 avccontext->channels;
|
|
45 avccontext->sample_rate;
|
|
46 //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
|
|
47 //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
|
|
48
|
|
49 len = header.bytes + header_comm.bytes + header_code.bytes;
|
|
50 avccontext->extradata_size = 64 + len + len/255;
|
|
51
|
|
52 p = avccontext->extradata = av_mallocz(avccontext->extradata_size);
|
|
53 p[0] = 2;
|
|
54 offset = 1;
|
|
55 offset += av_xiphlacing(&p[offset], header.bytes);
|
|
56 offset += av_xiphlacing(&p[offset], header_comm.bytes);
|
|
57 memcpy(&p[offset], header.packet, header.bytes);
|
|
58 offset += header.bytes;
|
|
59 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
|
|
60 offset += header_comm.bytes;
|
|
61 memcpy(&p[offset], header_code.packet, header_code.bytes);
|
|
62 offset += header_code.bytes;
|
|
63 avccontext->extradata_size = offset;
|
|
64 avccontext->extradata = av_realloc(avccontext->extradata, avccontext->extradata_size);
|
|
65
|
|
66 avccontext->frame_size = OGGVORBIS_FRAME_SIZE;
|
|
67
|
|
68 avccontext->coded_frame = avcodec_alloc_frame();
|
|
69 avccontext->coded_frame->key_frame = 1;
|
|
70
|
|
71 return 0;
|
|
72 }
|
|
73
|
|
74
|
|
75 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
|
|
76 {
|
|
77 venc_context_t * venc = avccontext->priv_data;
|
|
78 signed short * audio = data;
|
|
79 int l, samples = data ? VORBIS_FRAME_SIZE : 0;
|
|
80
|
|
81
|
|
82
|
|
83 if (!l) return 0;
|
|
84
|
|
85 avccontext->coded_frame->pts = av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
|
|
86 memcpy(packets, compressed_frame, l);
|
|
87 return l;
|
|
88 }
|
|
89
|
|
90
|
|
91 static int vorbis_encode_close(AVCodecContext * avccontext)
|
|
92 {
|
|
93 venc_context_t * venc = avccontext->priv_data;
|
|
94
|
|
95 av_freep(&avccontext->coded_frame);
|
|
96 av_freep(&avccontext->extradata);
|
|
97
|
|
98 return 0 ;
|
|
99 }
|
|
100
|
|
101 AVCodec oggvorbis_encoder = {
|
|
102 "vorbis",
|
|
103 CODEC_TYPE_AUDIO,
|
|
104 CODEC_ID_VORBIS,
|
|
105 sizeof(venc_context_t),
|
|
106 vorbis_encode_init,
|
|
107 vorbis_encode_frame,
|
|
108 vorbis_encode_close,
|
|
109 .capabilities= CODEC_CAP_DELAY,
|
|
110 };
|