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
){
38 int flags
= (enc
->bits_per_sample
== 16) ?
0x2 : 0x0;
40 switch (enc
->sample_rate
) {
50 case 8000: //nellymoser only
58 if (enc
->channels
> 1) {
62 switch(enc
->codec_id
){
68 case CODEC_ID_PCM_S16BE
:
71 case CODEC_ID_PCM_S16LE
:
75 flags
|= enc
->codec_tag
<<4;
84 static int flv_write_header(AVFormatContext
*s
)
86 ByteIOContext
*pb
= &s
->pb
;
87 FLVContext
*flv
= s
->priv_data
;
95 put_byte(pb
,0); // delayed write
99 for(i
=0; i
<s
->nb_streams
; i
++){
100 AVCodecContext
*enc
= s
->streams
[i
]->codec
;
101 av_set_pts_info(s
->streams
[i
], 24, 1, 1000); /* 24 bit pts in ms */
102 if(enc
->codec_tag
== 5){
103 put_byte(pb
,8); // message type
104 put_be24(pb
,0); // include flags
105 put_be24(pb
,0); // time stamp
106 put_be32(pb
,0); // reserved
107 put_be32(pb
,11); // size
110 if(enc
->codec_type
== CODEC_TYPE_AUDIO
&& get_audio_flags(enc
)<0)
117 static int flv_write_trailer(AVFormatContext
*s
)
122 ByteIOContext
*pb
= &s
->pb
;
123 FLVContext
*flv
= s
->priv_data
;
125 file_size
= url_ftell(pb
);
126 flags
|= flv
->hasAudio ?
4 : 0;
127 flags
|= flv
->hasVideo ?
1 : 0;
128 url_fseek(pb
, 4, SEEK_SET
);
130 url_fseek(pb
, file_size
, SEEK_SET
);
134 static int flv_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
136 ByteIOContext
*pb
= &s
->pb
;
137 AVCodecContext
*enc
= s
->streams
[pkt
->stream_index
]->codec
;
138 FLVContext
*flv
= s
->priv_data
;
142 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
144 if (enc
->codec_type
== CODEC_TYPE_VIDEO
) {
146 flags
= 2; // choose h263
147 flags
|= pkt
->flags
& PKT_FLAG_KEY ?
0x10 : 0x20; // add keyframe indicator
150 assert(enc
->codec_type
== CODEC_TYPE_AUDIO
);
151 flags
= get_audio_flags(enc
);
157 // We got audio! Make sure we set this to the global flags on closure
161 put_be24(pb
,size
+1); // include flags
162 put_be24(pb
,pkt
->pts
);
163 put_be32(pb
,flv
->reserved
);
165 put_buffer(pb
, pkt
->data
, size
);
166 put_be32(pb
,size
+1+11); // previous tag size
168 put_flush_packet(pb
);
172 static AVOutputFormat flv_oformat
= {
178 #ifdef CONFIG_MP3LAME
180 #else // CONFIG_MP3LAME
182 #endif // CONFIG_MP3LAME
189 int flvenc_init(void)
191 av_register_output_format(&flv_oformat
);