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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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:
69 flags |= enc->codec_tag<<4;
72 av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
79 static int flv_write_header(AVFormatContext *s)
81 ByteIOContext *pb = &s->pb;
82 FLVContext *flv = s->priv_data;
90 put_byte(pb,0); // delayed write
94 for(i=0; i<s->nb_streams; i++){
95 AVCodecContext *enc = s->streams[i]->codec;
96 av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
97 if(enc->codec_tag == 5){
98 put_byte(pb,8); // message type
99 put_be24(pb,0); // include flags
100 put_be24(pb,0); // time stamp
101 put_be32(pb,0); // reserved
102 put_be32(pb,11); // size
105 if(enc->codec_type == CODEC_TYPE_AUDIO && get_audio_flags(enc)<0)
112 static int flv_write_trailer(AVFormatContext *s)
117 ByteIOContext *pb = &s->pb;
118 FLVContext *flv = s->priv_data;
120 file_size = url_ftell(pb);
121 flags |= flv->hasAudio ? 4 : 0;
122 flags |= flv->hasVideo ? 1 : 0;
123 url_fseek(pb, 4, SEEK_SET);
125 url_fseek(pb, file_size, SEEK_SET);
129 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
131 ByteIOContext *pb = &s->pb;
132 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
133 FLVContext *flv = s->priv_data;
137 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
139 if (enc->codec_type == CODEC_TYPE_VIDEO) {
141 flags = 2; // choose h263
142 flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
145 assert(enc->codec_type == CODEC_TYPE_AUDIO);
146 flags = get_audio_flags(enc);
152 // We got audio! Make sure we set this to the global flags on closure
156 put_be24(pb,size+1); // include flags
157 put_be24(pb,pkt->pts);
158 put_be32(pb,flv->reserved);
160 put_buffer(pb, pkt->data, size);
161 put_be32(pb,size+1+11); // previous tag size
163 put_flush_packet(pb);
167 static AVOutputFormat flv_oformat = {
173 #ifdef CONFIG_MP3LAME
175 #else // CONFIG_MP3LAME
177 #endif // CONFIG_MP3LAME
184 int flvenc_init(void)
186 av_register_output_format(&flv_oformat);