comparison flvenc.c @ 3338:7370e3ec8bda libavformat

simplify, remove useless vars
author bcoudurier
date Sat, 24 May 2008 00:03:00 +0000
parents 83a8b43761bc
children a25db3f275d2
comparison
equal deleted inserted replaced
3337:ad5817f1efdb 3338:7370e3ec8bda
42 {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET}, 42 {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
43 {CODEC_ID_NONE, 0} 43 {CODEC_ID_NONE, 0}
44 }; 44 };
45 45
46 typedef struct FLVContext { 46 typedef struct FLVContext {
47 int hasAudio;
48 int hasVideo;
49 int reserved; 47 int reserved;
50 offset_t duration_offset; 48 offset_t duration_offset;
51 offset_t filesize_offset; 49 offset_t filesize_offset;
52 int64_t duration; 50 int64_t duration;
53 } FLVContext; 51 } FLVContext;
130 128
131 static int flv_write_header(AVFormatContext *s) 129 static int flv_write_header(AVFormatContext *s)
132 { 130 {
133 ByteIOContext *pb = s->pb; 131 ByteIOContext *pb = s->pb;
134 FLVContext *flv = s->priv_data; 132 FLVContext *flv = s->priv_data;
135 int i, width, height, samplerate, samplesize, channels, audiocodecid, videocodecid; 133 AVCodecContext *audio_enc = NULL, *video_enc = NULL;
134 int i;
136 double framerate = 0.0; 135 double framerate = 0.0;
137 int metadata_size_pos, data_size; 136 int metadata_size_pos, data_size;
138
139 flv->hasAudio = 0;
140 flv->hasVideo = 0;
141 137
142 for(i=0; i<s->nb_streams; i++){ 138 for(i=0; i<s->nb_streams; i++){
143 AVCodecContext *enc = s->streams[i]->codec; 139 AVCodecContext *enc = s->streams[i]->codec;
144 if (enc->codec_type == CODEC_TYPE_VIDEO) { 140 if (enc->codec_type == CODEC_TYPE_VIDEO) {
145 width = enc->width;
146 height = enc->height;
147 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) { 141 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
148 framerate = av_q2d(s->streams[i]->r_frame_rate); 142 framerate = av_q2d(s->streams[i]->r_frame_rate);
149 } else { 143 } else {
150 framerate = 1/av_q2d(s->streams[i]->codec->time_base); 144 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
151 } 145 }
152 flv->hasVideo=1; 146 video_enc = enc;
153 147 if(enc->codec_tag == 0) {
154 videocodecid = enc->codec_tag;
155 if(videocodecid == 0) {
156 av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n"); 148 av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
157 return -1; 149 return -1;
158 } 150 }
159 } else { 151 } else {
160 flv->hasAudio=1; 152 audio_enc = enc;
161 samplerate = enc->sample_rate;
162 channels = enc->channels;
163
164 audiocodecid = enc->codec_tag;
165 samplesize = (enc->codec_id == CODEC_ID_PCM_S8) ? 8 : 16;
166
167 if(get_audio_flags(enc)<0) 153 if(get_audio_flags(enc)<0)
168 return -1; 154 return -1;
169 } 155 }
170 av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */ 156 av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
171 } 157 }
172 put_tag(pb,"FLV"); 158 put_tag(pb,"FLV");
173 put_byte(pb,1); 159 put_byte(pb,1);
174 put_byte(pb, FLV_HEADER_FLAG_HASAUDIO * flv->hasAudio 160 put_byte(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
175 + FLV_HEADER_FLAG_HASVIDEO * flv->hasVideo); 161 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
176 put_be32(pb,9); 162 put_be32(pb,9);
177 put_be32(pb,0); 163 put_be32(pb,0);
178 164
179 for(i=0; i<s->nb_streams; i++){ 165 for(i=0; i<s->nb_streams; i++){
180 if(s->streams[i]->codec->codec_tag == 5){ 166 if(s->streams[i]->codec->codec_tag == 5){
200 put_byte(pb, AMF_DATA_TYPE_STRING); 186 put_byte(pb, AMF_DATA_TYPE_STRING);
201 put_amf_string(pb, "onMetaData"); // 12 bytes 187 put_amf_string(pb, "onMetaData"); // 12 bytes
202 188
203 /* mixed array (hash) with size and string/type/data tuples */ 189 /* mixed array (hash) with size and string/type/data tuples */
204 put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY); 190 put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
205 put_be32(pb, 5*flv->hasVideo + 4*flv->hasAudio + 2); // +2 for duration and file size 191 put_be32(pb, 5*!!video_enc + 4*!!audio_enc + 2); // +2 for duration and file size
206 192
207 put_amf_string(pb, "duration"); 193 put_amf_string(pb, "duration");
208 flv->duration_offset= url_ftell(pb); 194 flv->duration_offset= url_ftell(pb);
209 put_amf_double(pb, 0); // delayed write 195 put_amf_double(pb, 0); // delayed write
210 196
211 if(flv->hasVideo){ 197 if(video_enc){
212 put_amf_string(pb, "width"); 198 put_amf_string(pb, "width");
213 put_amf_double(pb, width); 199 put_amf_double(pb, video_enc->width);
214 200
215 put_amf_string(pb, "height"); 201 put_amf_string(pb, "height");
216 put_amf_double(pb, height); 202 put_amf_double(pb, video_enc->height);
217 203
218 put_amf_string(pb, "videodatarate"); 204 put_amf_string(pb, "videodatarate");
219 put_amf_double(pb, s->bit_rate / 1024.0); 205 put_amf_double(pb, s->bit_rate / 1024.0);
220 206
221 put_amf_string(pb, "framerate"); 207 put_amf_string(pb, "framerate");
222 put_amf_double(pb, framerate); 208 put_amf_double(pb, framerate);
223 209
224 put_amf_string(pb, "videocodecid"); 210 put_amf_string(pb, "videocodecid");
225 put_amf_double(pb, videocodecid); 211 put_amf_double(pb, video_enc->codec_tag);
226 } 212 }
227 213
228 if(flv->hasAudio){ 214 if(audio_enc){
229 put_amf_string(pb, "audiosamplerate"); 215 put_amf_string(pb, "audiosamplerate");
230 put_amf_double(pb, samplerate); 216 put_amf_double(pb, audio_enc->sample_rate);
231 217
232 put_amf_string(pb, "audiosamplesize"); 218 put_amf_string(pb, "audiosamplesize");
233 put_amf_double(pb, samplesize); 219 put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_S8 ? 8 : 16);
234 220
235 put_amf_string(pb, "stereo"); 221 put_amf_string(pb, "stereo");
236 put_amf_bool(pb, (channels == 2)); 222 put_amf_bool(pb, audio_enc->channels == 2);
237 223
238 put_amf_string(pb, "audiocodecid"); 224 put_amf_string(pb, "audiocodecid");
239 put_amf_double(pb, audiocodecid); 225 put_amf_double(pb, audio_enc->codec_tag);
240 } 226 }
241 227
242 put_amf_string(pb, "filesize"); 228 put_amf_string(pb, "filesize");
243 flv->filesize_offset= url_ftell(pb); 229 flv->filesize_offset= url_ftell(pb);
244 put_amf_double(pb, 0); // delayed write 230 put_amf_double(pb, 0); // delayed write