annotate src/ffmpeg/libavformat/flvenc.c @ 828:c294c4700937 trunk

[svn] - more stuff to kill
author nenolod
date Mon, 12 Mar 2007 15:05:25 -0700
parents e8776388b02a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1 /*
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2 * FLV encoder.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3 * Copyright (c) 2003 The FFmpeg Project.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
4 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
5 * This file is part of FFmpeg.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
6 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
11 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
15 * Lesser General Public License for more details.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
16 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
20 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
21 #include "avformat.h"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
22
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
23 #undef NDEBUG
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
24 #include <assert.h>
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
25
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
26 typedef struct FLVContext {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
27 int hasAudio;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
28 int hasVideo;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
29 int reserved;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
30 offset_t duration_offset;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
31 offset_t filesize_offset;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
32 int64_t duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
33 } FLVContext;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
34
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
35 static int get_audio_flags(AVCodecContext *enc){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
36 int flags = (enc->bits_per_sample == 16) ? 0x2 : 0x0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
37
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
38 switch (enc->sample_rate) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
39 case 44100:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
40 flags |= 0x0C;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
41 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
42 case 22050:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
43 flags |= 0x08;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
44 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
45 case 11025:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
46 flags |= 0x04;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
47 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
48 case 8000: //nellymoser only
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
49 case 5512: //not mp3
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
50 flags |= 0x00;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
51 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
52 default:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
53 av_log(enc, AV_LOG_ERROR, "flv doesnt support that sample rate, choose from (44100, 22050, 11025)\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
54 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
55 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
56
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
57 if (enc->channels > 1) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
58 flags |= 0x01;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
59 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
60
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
61 switch(enc->codec_id){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
62 case CODEC_ID_MP3:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
63 flags |= 0x20 | 0x2;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
64 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
65 case CODEC_ID_PCM_S8:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
66 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
67 case CODEC_ID_PCM_S16BE:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
68 flags |= 0x60 | 0x2;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
69 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
70 case CODEC_ID_PCM_S16LE:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
71 flags |= 0x2;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
72 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
73 case CODEC_ID_ADPCM_SWF:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
74 flags |= 0x10;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
75 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
76 case 0:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
77 flags |= enc->codec_tag<<4;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
78 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
79 default:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
80 av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
81 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
82 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
83
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
84 return flags;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
85 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
86
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
87 #define AMF_DOUBLE 0
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
88 #define AMF_BOOLEAN 1
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
89 #define AMF_STRING 2
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
90 #define AMF_OBJECT 3
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
91 #define AMF_MIXED_ARRAY 8
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
92 #define AMF_ARRAY 10
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
93 #define AMF_DATE 11
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
94
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
95 static void put_amf_string(ByteIOContext *pb, const char *str)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
96 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
97 size_t len = strlen(str);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
98 put_be16(pb, len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
99 put_buffer(pb, str, len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
100 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
101
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
102 static void put_amf_double(ByteIOContext *pb, double d)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
103 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
104 put_byte(pb, AMF_DOUBLE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
105 put_be64(pb, av_dbl2int(d));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
106 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
107
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
108 static int flv_write_header(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
109 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
110 ByteIOContext *pb = &s->pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
111 FLVContext *flv = s->priv_data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
112 int i, width, height, samplerate;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
113 double framerate = 0.0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
114 int metadata_size_pos, data_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
115
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
116 flv->hasAudio = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
117 flv->hasVideo = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
118
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
119 put_tag(pb,"FLV");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
120 put_byte(pb,1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
121 put_byte(pb,0); // delayed write
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
122 put_be32(pb,9);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
123 put_be32(pb,0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
124
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
125 for(i=0; i<s->nb_streams; i++){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
126 AVCodecContext *enc = s->streams[i]->codec;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
127 if (enc->codec_type == CODEC_TYPE_VIDEO) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
128 width = enc->width;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
129 height = enc->height;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
130 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
131 framerate = av_q2d(s->streams[i]->r_frame_rate);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
132 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
133 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
134 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
135 flv->hasVideo=1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
136 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
137 flv->hasAudio=1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
138 samplerate = enc->sample_rate;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
139 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
140 av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
141 if(enc->codec_tag == 5){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
142 put_byte(pb,8); // message type
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
143 put_be24(pb,0); // include flags
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
144 put_be24(pb,0); // time stamp
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
145 put_be32(pb,0); // reserved
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
146 put_be32(pb,11); // size
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
147 flv->reserved=5;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
148 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
149 if(enc->codec_type == CODEC_TYPE_AUDIO && get_audio_flags(enc)<0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
150 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
151 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
152
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
153 /* write meta_tag */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
154 put_byte(pb, 18); // tag type META
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
155 metadata_size_pos= url_ftell(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
156 put_be24(pb, 0); // size of data part (sum of all parts below)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
157 put_be24(pb, 0); // time stamp
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
158 put_be32(pb, 0); // reserved
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
159
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
160 /* now data of data_size size */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
161
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
162 /* first event name as a string */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
163 put_byte(pb, AMF_STRING); // 1 byte
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
164 put_amf_string(pb, "onMetaData"); // 12 bytes
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
165
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
166 /* mixed array (hash) with size and string/type/data tuples */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
167 put_byte(pb, AMF_MIXED_ARRAY);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
168 put_be32(pb, 4*flv->hasVideo + flv->hasAudio + 2); // +2 for duration and file size
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
169
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
170 put_amf_string(pb, "duration");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
171 flv->duration_offset= url_ftell(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
172 put_amf_double(pb, 0); // delayed write
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
173
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
174 if(flv->hasVideo){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
175 put_amf_string(pb, "width");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
176 put_amf_double(pb, width);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
177
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
178 put_amf_string(pb, "height");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
179 put_amf_double(pb, height);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
180
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
181 put_amf_string(pb, "videodatarate");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
182 put_amf_double(pb, s->bit_rate / 1024.0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
183
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
184 put_amf_string(pb, "framerate");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
185 put_amf_double(pb, framerate);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
186 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
187
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
188 if(flv->hasAudio){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
189 put_amf_string(pb, "audiosamplerate");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
190 put_amf_double(pb, samplerate);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
191 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
192
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
193 put_amf_string(pb, "filesize");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
194 flv->filesize_offset= url_ftell(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
195 put_amf_double(pb, 0); // delayed write
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
196
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
197 put_amf_string(pb, "");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
198 put_byte(pb, 9); // end marker 1 byte
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
199
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
200 /* write total size of tag */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
201 data_size= url_ftell(pb) - metadata_size_pos - 10;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
202 url_fseek(pb, metadata_size_pos, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
203 put_be24(pb, data_size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
204 url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
205 put_be32(pb, data_size + 11);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
206
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
207 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
208 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
209
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
210 static int flv_write_trailer(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
211 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
212 int64_t file_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
213 int flags = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
214
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
215 ByteIOContext *pb = &s->pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
216 FLVContext *flv = s->priv_data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
217
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
218 file_size = url_ftell(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
219 flags |= flv->hasAudio ? 4 : 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
220 flags |= flv->hasVideo ? 1 : 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
221 url_fseek(pb, 4, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
222 put_byte(pb,flags);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
223
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
224 /* update informations */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
225 url_fseek(pb, flv->duration_offset, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
226 put_amf_double(pb, flv->duration / (double)1000);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
227 url_fseek(pb, flv->filesize_offset, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
228 put_amf_double(pb, file_size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
229
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
230 url_fseek(pb, file_size, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
231 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
232 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
233
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
234 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
235 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
236 ByteIOContext *pb = &s->pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
237 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
238 FLVContext *flv = s->priv_data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
239 int size= pkt->size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
240 int flags;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
241
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
242 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
243
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
244 if (enc->codec_type == CODEC_TYPE_VIDEO) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
245 put_byte(pb, 9);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
246 flags = 2; // choose h263
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
247 flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
248 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
249 assert(enc->codec_type == CODEC_TYPE_AUDIO);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
250 flags = get_audio_flags(enc);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
251
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
252 assert(size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
253
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
254 put_byte(pb, 8);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
255 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
256
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
257 put_be24(pb,size+1); // include flags
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
258 put_be24(pb,pkt->pts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
259 put_be32(pb,flv->reserved);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
260 put_byte(pb,flags);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
261 put_buffer(pb, pkt->data, size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
262 put_be32(pb,size+1+11); // previous tag size
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
263 flv->duration = pkt->pts + pkt->duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
264
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
265 put_flush_packet(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
266 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
267 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
268
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
269 AVOutputFormat flv_muxer = {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
270 "flv",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
271 "flv format",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
272 "video/x-flv",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
273 "flv",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
274 sizeof(FLVContext),
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
275 #ifdef CONFIG_MP3LAME
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
276 CODEC_ID_MP3,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
277 #else // CONFIG_MP3LAME
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
278 CODEC_ID_NONE,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
279 #endif // CONFIG_MP3LAME
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
280 CODEC_ID_FLV1,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
281 flv_write_header,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
282 flv_write_packet,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
283 flv_write_trailer,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
284 };