Statistics
| Branch: | Revision:

ffmpeg / libavformat / flvenc.c @ 7be806f3

History | View | Annotate | Download (4.51 KB)

1 d4f5d74a Garrick Meeker
/*
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 068f2a22 Michael Niedermayer
#undef NDEBUG
22
#include <assert.h>
23
24 d4f5d74a Garrick Meeker
typedef struct FLVContext {
25
    int hasAudio;
26
    int hasVideo;
27 11a8e425 Michael Niedermayer
    int reserved;
28 d4f5d74a Garrick Meeker
} FLVContext;
29
30 11a8e425 Michael Niedermayer
static 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
37 37cdf93d Michael Niedermayer
static int get_audio_flags(AVCodecContext *enc){
38
    int flags = 0x02;
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:
64
        flags |= 0x20;
65
        break;
66
    case 0:
67
        flags |= enc->codec_tag<<4;
68
        break;
69
    default:
70
        return -1;
71
    }
72
    
73
    return flags;
74
}
75
76 d4f5d74a Garrick Meeker
static int flv_write_header(AVFormatContext *s)
77
{
78
    ByteIOContext *pb = &s->pb;
79
    FLVContext *flv = s->priv_data;
80 11a8e425 Michael Niedermayer
    int i;
81 d4f5d74a Garrick Meeker
82
    flv->hasAudio = 0;
83
    flv->hasVideo = 0;
84
85
    put_tag(pb,"FLV");
86
    put_byte(pb,1);
87
    put_byte(pb,0); // delayed write
88
    put_be32(pb,9);
89
    put_be32(pb,0);
90 11a8e425 Michael Niedermayer
    
91
    for(i=0; i<s->nb_streams; i++){
92
        AVCodecContext *enc = &s->streams[i]->codec;
93 9ee91c2f Michael Niedermayer
        av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
94 11a8e425 Michael Niedermayer
        if(enc->codec_tag == 5){
95
            put_byte(pb,8); // message type
96
            put_be24(pb,0); // include flags
97
            put_be24(pb,0); // time stamp
98
            put_be32(pb,0); // reserved
99
            put_be32(pb,11); // size
100
            flv->reserved=5;
101
        }
102 37cdf93d Michael Niedermayer
        if(enc->codec_type == CODEC_TYPE_AUDIO && get_audio_flags(enc)<0)
103
            return -1;
104 11a8e425 Michael Niedermayer
    }
105 d4f5d74a Garrick Meeker
106
    return 0;
107
}
108
109
static int flv_write_trailer(AVFormatContext *s)
110
{
111 14b32253 Michael Niedermayer
    int64_t file_size;
112
    int flags = 0;
113
114 d4f5d74a Garrick Meeker
    ByteIOContext *pb = &s->pb;
115
    FLVContext *flv = s->priv_data;
116
117 14b32253 Michael Niedermayer
    file_size = url_ftell(pb);
118 d4f5d74a Garrick Meeker
    flags |= flv->hasAudio ? 4 : 0;
119
    flags |= flv->hasVideo ? 1 : 0;
120
    url_fseek(pb, 4, SEEK_SET);
121
    put_byte(pb,flags);
122
    url_fseek(pb, file_size, SEEK_SET);
123
    return 0;
124
}
125
126 e928649b Michael Niedermayer
static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
127 d4f5d74a Garrick Meeker
{
128
    ByteIOContext *pb = &s->pb;
129 e928649b Michael Niedermayer
    AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
130 d4f5d74a Garrick Meeker
    FLVContext *flv = s->priv_data;
131 e928649b Michael Niedermayer
    int size= pkt->size;
132 75293f05 Michael Niedermayer
    int flags;
133 d4f5d74a Garrick Meeker
134 068f2a22 Michael Niedermayer
//    av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
135
    
136 d4f5d74a Garrick Meeker
    if (enc->codec_type == CODEC_TYPE_VIDEO) {
137 75293f05 Michael Niedermayer
        put_byte(pb, 9);
138
        flags = 2; // choose h263
139
        flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
140 d4f5d74a Garrick Meeker
        flv->hasVideo = 1;
141 75293f05 Michael Niedermayer
    } else {
142
        assert(enc->codec_type == CODEC_TYPE_AUDIO);
143 37cdf93d Michael Niedermayer
        flags = get_audio_flags(enc);
144 068f2a22 Michael Niedermayer
        
145
        assert(size);
146
147 75293f05 Michael Niedermayer
        put_byte(pb, 8);
148 068f2a22 Michael Niedermayer
149
        // We got audio! Make sure we set this to the global flags on closure
150
        flv->hasAudio = 1;
151 75293f05 Michael Niedermayer
    }
152
153
    put_be24(pb,size+1); // include flags
154
    put_be24(pb,pkt->pts);
155
    put_be32(pb,flv->reserved);
156
    put_byte(pb,flags);
157
    put_buffer(pb, pkt->data, size);
158
    put_be32(pb,size+1+11); // reserved
159
160 d4f5d74a Garrick Meeker
    put_flush_packet(pb);
161
    return 0;
162
}
163
164
static AVOutputFormat flv_oformat = {
165
    "flv",
166
    "flv format",
167
    "video/x-flashvideo",
168
    "flv",
169
    sizeof(FLVContext),
170
#ifdef CONFIG_MP3LAME
171 80783dc2 Fabrice Bellard
    CODEC_ID_MP3,
172 d4f5d74a Garrick Meeker
#else // CONFIG_MP3LAME
173
    CODEC_ID_NONE,
174
#endif // CONFIG_MP3LAME
175
    CODEC_ID_FLV1,
176
    flv_write_header,
177
    flv_write_packet,
178
    flv_write_trailer,
179
};
180
181
int flvenc_init(void)
182
{
183
    av_register_output_format(&flv_oformat);
184
    return 0;
185
}