bttv patches
[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
d4f5d74a
GM
37static int flv_write_header(AVFormatContext *s)
38{
39 ByteIOContext *pb = &s->pb;
40 FLVContext *flv = s->priv_data;
11a8e425 41 int i;
d4f5d74a 42
d4f5d74a
GM
43 flv->hasAudio = 0;
44 flv->hasVideo = 0;
45
d4f5d74a
GM
46 put_tag(pb,"FLV");
47 put_byte(pb,1);
48 put_byte(pb,0); // delayed write
49 put_be32(pb,9);
50 put_be32(pb,0);
11a8e425
MN
51
52 for(i=0; i<s->nb_streams; i++){
53 AVCodecContext *enc = &s->streams[i]->codec;
9ee91c2f 54 av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
11a8e425
MN
55 if(enc->codec_tag == 5){
56 put_byte(pb,8); // message type
57 put_be24(pb,0); // include flags
58 put_be24(pb,0); // time stamp
59 put_be32(pb,0); // reserved
60 put_be32(pb,11); // size
61 flv->reserved=5;
62 }
63 }
d4f5d74a
GM
64
65 return 0;
66}
67
d4f5d74a
GM
68static int flv_write_trailer(AVFormatContext *s)
69{
14b32253
MN
70 int64_t file_size;
71 int flags = 0;
72
d4f5d74a
GM
73 ByteIOContext *pb = &s->pb;
74 FLVContext *flv = s->priv_data;
75
14b32253 76 file_size = url_ftell(pb);
d4f5d74a
GM
77 flags |= flv->hasAudio ? 4 : 0;
78 flags |= flv->hasVideo ? 1 : 0;
79 url_fseek(pb, 4, SEEK_SET);
80 put_byte(pb,flags);
81 url_fseek(pb, file_size, SEEK_SET);
82 return 0;
83}
84
e928649b 85static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
d4f5d74a
GM
86{
87 ByteIOContext *pb = &s->pb;
e928649b 88 AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
d4f5d74a 89 FLVContext *flv = s->priv_data;
e928649b 90 int size= pkt->size;
75293f05 91 int flags;
d4f5d74a 92
068f2a22
MN
93// av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
94
d4f5d74a 95 if (enc->codec_type == CODEC_TYPE_VIDEO) {
75293f05
MN
96 put_byte(pb, 9);
97 flags = 2; // choose h263
98 flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
d4f5d74a 99 flv->hasVideo = 1;
75293f05
MN
100 } else {
101 assert(enc->codec_type == CODEC_TYPE_AUDIO);
102 flags = 0x02;
92a26775 103
068f2a22
MN
104 switch (enc->sample_rate) {
105 case 44100:
75293f05 106 flags |= 0x0C;
068f2a22
MN
107 break;
108 case 22050:
75293f05 109 flags |= 0x08;
068f2a22
MN
110 break;
111 case 11025:
75293f05 112 flags |= 0x04;
068f2a22
MN
113 break;
114 case 8000: //nellymoser only
115 case 5512: //not mp3
75293f05 116 flags |= 0x00;
068f2a22
MN
117 break;
118 default:
119 assert(0);
120 }
d4f5d74a 121
068f2a22 122 if (enc->channels > 1) {
75293f05 123 flags |= 0x01;
068f2a22
MN
124 }
125
126 switch(enc->codec_id){
127 case CODEC_ID_MP3:
75293f05 128 flags |= 0x20;
068f2a22
MN
129 break;
130 case 0:
75293f05 131 flags |= enc->codec_tag<<4;
068f2a22
MN
132 break;
133 default:
134 assert(0);
135 }
136
137 assert(size);
068f2a22 138
75293f05 139 put_byte(pb, 8);
068f2a22
MN
140
141 // We got audio! Make sure we set this to the global flags on closure
142 flv->hasAudio = 1;
75293f05
MN
143 }
144
145 put_be24(pb,size+1); // include flags
146 put_be24(pb,pkt->pts);
147 put_be32(pb,flv->reserved);
148 put_byte(pb,flags);
149 put_buffer(pb, pkt->data, size);
150 put_be32(pb,size+1+11); // reserved
151
d4f5d74a
GM
152 put_flush_packet(pb);
153 return 0;
154}
155
156static AVOutputFormat flv_oformat = {
157 "flv",
158 "flv format",
159 "video/x-flashvideo",
160 "flv",
161 sizeof(FLVContext),
162#ifdef CONFIG_MP3LAME
80783dc2 163 CODEC_ID_MP3,
d4f5d74a
GM
164#else // CONFIG_MP3LAME
165 CODEC_ID_NONE,
166#endif // CONFIG_MP3LAME
167 CODEC_ID_FLV1,
168 flv_write_header,
169 flv_write_packet,
170 flv_write_trailer,
171};
172
173int flvenc_init(void)
174{
175 av_register_output_format(&flv_oformat);
176 return 0;
177}