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