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 void put_be24(ByteIOContext *pb, int value)
32 put_byte(pb, (value>>16) & 0xFF );
33 put_byte(pb, (value>> 8) & 0xFF );
34 put_byte(pb, (value>> 0) & 0xFF );
37 static int get_audio_flags(AVCodecContext *enc){
40 switch (enc->sample_rate) {
50 case 8000: //nellymoser only
58 if (enc->channels > 1) {
62 switch(enc->codec_id){
67 flags |= enc->codec_tag<<4;
76 static int flv_write_header(AVFormatContext *s)
78 ByteIOContext *pb = &s->pb;
79 FLVContext *flv = s->priv_data;
87 put_byte(pb,0); // delayed write
91 for(i=0; i<s->nb_streams; i++){
92 AVCodecContext *enc = &s->streams[i]->codec;
93 av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
94 if(enc->codec_tag == 5){
95 put_byte(pb,8); // message type
96 put_be24(pb,0); // include flags
97 put_be24(pb,0); // time stamp
98 put_be32(pb,0); // reserved
99 put_be32(pb,11); // size
102 if(enc->codec_type == CODEC_TYPE_AUDIO && get_audio_flags(enc)<0)
109 static int flv_write_trailer(AVFormatContext *s)
114 ByteIOContext *pb = &s->pb;
115 FLVContext *flv = s->priv_data;
117 file_size = url_ftell(pb);
118 flags |= flv->hasAudio ? 4 : 0;
119 flags |= flv->hasVideo ? 1 : 0;
120 url_fseek(pb, 4, SEEK_SET);
122 url_fseek(pb, file_size, SEEK_SET);
126 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
128 ByteIOContext *pb = &s->pb;
129 AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
130 FLVContext *flv = s->priv_data;
134 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
136 if (enc->codec_type == CODEC_TYPE_VIDEO) {
138 flags = 2; // choose h263
139 flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
142 assert(enc->codec_type == CODEC_TYPE_AUDIO);
143 flags = get_audio_flags(enc);
149 // We got audio! Make sure we set this to the global flags on closure
153 put_be24(pb,size+1); // include flags
154 put_be24(pb,pkt->pts);
155 put_be32(pb,flv->reserved);
157 put_buffer(pb, pkt->data, size);
158 put_be32(pb,size+1+11); // reserved
160 put_flush_packet(pb);
164 static AVOutputFormat flv_oformat = {
167 "video/x-flashvideo",
170 #ifdef CONFIG_MP3LAME
172 #else // CONFIG_MP3LAME
174 #endif // CONFIG_MP3LAME
181 int flvenc_init(void)
183 av_register_output_format(&flv_oformat);