3 * Copyright (c) 2003 The FFmpeg Project.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 typedef struct FLVContext {
30 static int get_audio_flags(AVCodecContext *enc){
31 int flags = (enc->bits_per_sample == 16) ? 0x2 : 0x0;
33 switch (enc->sample_rate) {
43 case 8000: //nellymoser only
48 av_log(enc, AV_LOG_ERROR, "flv doesnt support that sample rate, choose from (44100, 22050, 11025)\n");
52 if (enc->channels > 1) {
56 switch(enc->codec_id){
62 case CODEC_ID_PCM_S16BE:
65 case CODEC_ID_PCM_S16LE:
68 case CODEC_ID_ADPCM_SWF:
72 flags |= enc->codec_tag<<4;
75 av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
82 static int flv_write_header(AVFormatContext *s)
84 ByteIOContext *pb = &s->pb;
85 FLVContext *flv = s->priv_data;
93 put_byte(pb,0); // delayed write
97 for(i=0; i<s->nb_streams; i++){
98 AVCodecContext *enc = s->streams[i]->codec;
99 av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
100 if(enc->codec_tag == 5){
101 put_byte(pb,8); // message type
102 put_be24(pb,0); // include flags
103 put_be24(pb,0); // time stamp
104 put_be32(pb,0); // reserved
105 put_be32(pb,11); // size
108 if(enc->codec_type == CODEC_TYPE_AUDIO && get_audio_flags(enc)<0)
115 static int flv_write_trailer(AVFormatContext *s)
120 ByteIOContext *pb = &s->pb;
121 FLVContext *flv = s->priv_data;
123 file_size = url_ftell(pb);
124 flags |= flv->hasAudio ? 4 : 0;
125 flags |= flv->hasVideo ? 1 : 0;
126 url_fseek(pb, 4, SEEK_SET);
128 url_fseek(pb, file_size, SEEK_SET);
132 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
134 ByteIOContext *pb = &s->pb;
135 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
136 FLVContext *flv = s->priv_data;
140 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
142 if (enc->codec_type == CODEC_TYPE_VIDEO) {
144 flags = 2; // choose h263
145 flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
148 assert(enc->codec_type == CODEC_TYPE_AUDIO);
149 flags = get_audio_flags(enc);
155 // We got audio! Make sure we set this to the global flags on closure
159 put_be24(pb,size+1); // include flags
160 put_be24(pb,pkt->pts);
161 put_be32(pb,flv->reserved);
163 put_buffer(pb, pkt->data, size);
164 put_be32(pb,size+1+11); // previous tag size
166 put_flush_packet(pb);
170 static AVOutputFormat flv_oformat = {
176 #ifdef CONFIG_MP3LAME
178 #else // CONFIG_MP3LAME
180 #endif // CONFIG_MP3LAME
187 int flvenc_init(void)
189 av_register_output_format(&flv_oformat);