changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext...
[libav.git] / libavformat / flvenc.c
CommitLineData
d4f5d74a
GM
1/*
2 * FLV encoder.
3 * Copyright (c) 2003 The FFmpeg Project.
4 *
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.
9 *
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.
14 *
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
18 */
19#include "avformat.h"
20
068f2a22
MN
21#undef NDEBUG
22#include <assert.h>
23
d4f5d74a
GM
24typedef struct FLVContext {
25 int hasAudio;
26 int hasVideo;
11a8e425 27 int reserved;
d4f5d74a
GM
28} FLVContext;
29
11a8e425
MN
30static void put_be24(ByteIOContext *pb, int value)
31{
32 put_byte(pb, (value>>16) & 0xFF );
33 put_byte(pb, (value>> 8) & 0xFF );
34 put_byte(pb, (value>> 0) & 0xFF );
35}
36
37cdf93d 37static int get_audio_flags(AVCodecContext *enc){
2f5132e7 38 int flags = (enc->bits_per_sample == 16) ? 0x2 : 0x0;
37cdf93d
MN
39
40 switch (enc->sample_rate) {
41 case 44100:
42 flags |= 0x0C;
43 break;
44 case 22050:
45 flags |= 0x08;
46 break;
47 case 11025:
48 flags |= 0x04;
49 break;
50 case 8000: //nellymoser only
51 case 5512: //not mp3
52 flags |= 0x00;
53 break;
54 default:
55 return -1;
56 }
57
58 if (enc->channels > 1) {
59 flags |= 0x01;
60 }
61
62 switch(enc->codec_id){
63 case CODEC_ID_MP3:
923bd441 64 flags |= 0x20 | 0x2;
37cdf93d 65 break;
923bd441
AB
66 case CODEC_ID_PCM_S8:
67 break;
68 case CODEC_ID_PCM_S16BE:
69 flags |= 0x60 | 0x2;
70 break;
71 case CODEC_ID_PCM_S16LE:
72 flags |= 0x2;
73 break;
37cdf93d
MN
74 case 0:
75 flags |= enc->codec_tag<<4;
76 break;
77 default:
78 return -1;
79 }
80
81 return flags;
82}
83
d4f5d74a
GM
84static int flv_write_header(AVFormatContext *s)
85{
86 ByteIOContext *pb = &s->pb;
87 FLVContext *flv = s->priv_data;
11a8e425 88 int i;
d4f5d74a 89
d4f5d74a
GM
90 flv->hasAudio = 0;
91 flv->hasVideo = 0;
92
d4f5d74a
GM
93 put_tag(pb,"FLV");
94 put_byte(pb,1);
95 put_byte(pb,0); // delayed write
96 put_be32(pb,9);
97 put_be32(pb,0);
11a8e425
MN
98
99 for(i=0; i<s->nb_streams; i++){
01f4895c 100 AVCodecContext *enc = s->streams[i]->codec;
9ee91c2f 101 av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
11a8e425
MN
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
108 flv->reserved=5;
109 }
37cdf93d
MN
110 if(enc->codec_type == CODEC_TYPE_AUDIO && get_audio_flags(enc)<0)
111 return -1;
11a8e425 112 }
d4f5d74a
GM
113
114 return 0;
115}
116
d4f5d74a
GM
117static int flv_write_trailer(AVFormatContext *s)
118{
14b32253
MN
119 int64_t file_size;
120 int flags = 0;
121
d4f5d74a
GM
122 ByteIOContext *pb = &s->pb;
123 FLVContext *flv = s->priv_data;
124
14b32253 125 file_size = url_ftell(pb);
d4f5d74a
GM
126 flags |= flv->hasAudio ? 4 : 0;
127 flags |= flv->hasVideo ? 1 : 0;
128 url_fseek(pb, 4, SEEK_SET);
129 put_byte(pb,flags);
130 url_fseek(pb, file_size, SEEK_SET);
131 return 0;
132}
133
e928649b 134static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
d4f5d74a
GM
135{
136 ByteIOContext *pb = &s->pb;
01f4895c 137 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
d4f5d74a 138 FLVContext *flv = s->priv_data;
e928649b 139 int size= pkt->size;
75293f05 140 int flags;
d4f5d74a 141
068f2a22
MN
142// av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
143
d4f5d74a 144 if (enc->codec_type == CODEC_TYPE_VIDEO) {
75293f05
MN
145 put_byte(pb, 9);
146 flags = 2; // choose h263
147 flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
d4f5d74a 148 flv->hasVideo = 1;
75293f05
MN
149 } else {
150 assert(enc->codec_type == CODEC_TYPE_AUDIO);
37cdf93d 151 flags = get_audio_flags(enc);
068f2a22 152
068f2a22 153 assert(size);
068f2a22 154
75293f05 155 put_byte(pb, 8);
068f2a22
MN
156
157 // We got audio! Make sure we set this to the global flags on closure
158 flv->hasAudio = 1;
75293f05
MN
159 }
160
161 put_be24(pb,size+1); // include flags
162 put_be24(pb,pkt->pts);
163 put_be32(pb,flv->reserved);
164 put_byte(pb,flags);
165 put_buffer(pb, pkt->data, size);
923bd441
AB
166 put_be32(pb,size+1+11); // previous tag size
167
d4f5d74a
GM
168 put_flush_packet(pb);
169 return 0;
170}
171
172static AVOutputFormat flv_oformat = {
173 "flv",
174 "flv format",
e817a73d 175 "video/x-flv",
d4f5d74a
GM
176 "flv",
177 sizeof(FLVContext),
178#ifdef CONFIG_MP3LAME
80783dc2 179 CODEC_ID_MP3,
d4f5d74a
GM
180#else // CONFIG_MP3LAME
181 CODEC_ID_NONE,
182#endif // CONFIG_MP3LAME
183 CODEC_ID_FLV1,
184 flv_write_header,
185 flv_write_packet,
186 flv_write_trailer,
187};
188
189int flvenc_init(void)
190{
191 av_register_output_format(&flv_oformat);
192 return 0;
193}