annotate src/ffmpeg/libavformat/swf.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 * Flash Compatible Streaming Format
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3 * Copyright (c) 2000 Fabrice Bellard.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
4 * Copyright (c) 2003 Tinic Uro.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
5 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
6 * This file is part of FFmpeg.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
7 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
12 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
16 * Lesser General Public License for more details.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
17 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
21 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
22 #include "avformat.h"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
23 #include "bitstream.h"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
24 #include "riff.h" /* for CodecTag */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
25
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
26 /* should have a generic way to indicate probable size */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
27 #define DUMMY_FILE_SIZE (100 * 1024 * 1024)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
28 #define DUMMY_DURATION 600 /* in seconds */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
29
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
30 #define TAG_END 0
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
31 #define TAG_SHOWFRAME 1
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
32 #define TAG_DEFINESHAPE 2
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
33 #define TAG_FREECHARACTER 3
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
34 #define TAG_PLACEOBJECT 4
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
35 #define TAG_REMOVEOBJECT 5
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
36 #define TAG_STREAMHEAD 18
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
37 #define TAG_STREAMBLOCK 19
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
38 #define TAG_JPEG2 21
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
39 #define TAG_PLACEOBJECT2 26
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
40 #define TAG_STREAMHEAD2 45
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
41 #define TAG_VIDEOSTREAM 60
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
42 #define TAG_VIDEOFRAME 61
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
43
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
44 #define TAG_LONG 0x100
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
45
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
46 /* flags for shape definition */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
47 #define FLAG_MOVETO 0x01
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
48 #define FLAG_SETFILL0 0x02
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
49 #define FLAG_SETFILL1 0x04
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
50
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
51 #define AUDIO_FIFO_SIZE 65536
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
52
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
53 /* character id used */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
54 #define BITMAP_ID 0
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
55 #define VIDEO_ID 0
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
56 #define SHAPE_ID 1
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
57
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
58 #undef NDEBUG
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
59 #include <assert.h>
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
60
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
61 typedef struct {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
62
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
63 offset_t duration_pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
64 offset_t tag_pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
65
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
66 int samples_per_frame;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
67 int sound_samples;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
68 int video_samples;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
69 int swf_frame_number;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
70 int video_frame_number;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
71 int ms_per_frame;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
72 int ch_id;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
73 int tag;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
74
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
75 uint8_t *audio_fifo;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
76 int audio_in_pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
77 int audio_out_pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
78 int audio_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
79
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
80 int video_type;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
81 int audio_type;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
82 } SWFContext;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
83
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
84 static const CodecTag swf_codec_tags[] = {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
85 {CODEC_ID_FLV1, 0x02},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
86 {CODEC_ID_VP6F, 0x04},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
87 {0, 0},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
88 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
89
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
90 static const int sSampleRates[3][4] = {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
91 {44100, 48000, 32000, 0},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
92 {22050, 24000, 16000, 0},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
93 {11025, 12000, 8000, 0},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
94 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
95
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
96 static const int sBitRates[2][3][15] = {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
97 { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
98 { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
99 { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
100 },
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
101 { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
102 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
103 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
104 },
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
105 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
106
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
107 static const int sSamplesPerFrame[3][3] =
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
108 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
109 { 384, 1152, 1152 },
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
110 { 384, 1152, 576 },
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
111 { 384, 1152, 576 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
112 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
113
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
114 static const int sBitsPerSlot[3] = {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
115 32,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
116 8,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
117 8
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
118 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
119
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
120 static int swf_mp3_info(void *data, int *byteSize, int *samplesPerFrame, int *sampleRate, int *isMono )
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
121 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
122 uint8_t *dataTmp = (uint8_t *)data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
123 uint32_t header = ( (uint32_t)dataTmp[0] << 24 ) | ( (uint32_t)dataTmp[1] << 16 ) | ( (uint32_t)dataTmp[2] << 8 ) | (uint32_t)dataTmp[3];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
124 int layerID = 3 - ((header >> 17) & 0x03);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
125 int bitRateID = ((header >> 12) & 0x0f);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
126 int sampleRateID = ((header >> 10) & 0x03);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
127 int bitRate = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
128 int bitsPerSlot = sBitsPerSlot[layerID];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
129 int isPadded = ((header >> 9) & 0x01);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
130
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
131 if ( (( header >> 21 ) & 0x7ff) != 0x7ff ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
132 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
133 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
134
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
135 *isMono = ((header >> 6) & 0x03) == 0x03;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
136
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
137 if ( (header >> 19 ) & 0x01 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
138 *sampleRate = sSampleRates[0][sampleRateID];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
139 bitRate = sBitRates[0][layerID][bitRateID] * 1000;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
140 *samplesPerFrame = sSamplesPerFrame[0][layerID];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
141 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
142 if ( (header >> 20) & 0x01 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
143 *sampleRate = sSampleRates[1][sampleRateID];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
144 bitRate = sBitRates[1][layerID][bitRateID] * 1000;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
145 *samplesPerFrame = sSamplesPerFrame[1][layerID];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
146 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
147 *sampleRate = sSampleRates[2][sampleRateID];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
148 bitRate = sBitRates[1][layerID][bitRateID] * 1000;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
149 *samplesPerFrame = sSamplesPerFrame[2][layerID];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
150 }
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 *byteSize = ( ( ( ( *samplesPerFrame * (bitRate / bitsPerSlot) ) / *sampleRate ) + isPadded ) );
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
154
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
155 return 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
156 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
157
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
158 #ifdef CONFIG_MUXERS
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
159 static void put_swf_tag(AVFormatContext *s, int tag)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
160 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
161 SWFContext *swf = s->priv_data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
162 ByteIOContext *pb = &s->pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
163
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
164 swf->tag_pos = url_ftell(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
165 swf->tag = tag;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
166 /* reserve some room for the tag */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
167 if (tag & TAG_LONG) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
168 put_le16(pb, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
169 put_le32(pb, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
170 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
171 put_le16(pb, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
172 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
173 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
174
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
175 static void put_swf_end_tag(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
176 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
177 SWFContext *swf = s->priv_data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
178 ByteIOContext *pb = &s->pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
179 offset_t pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
180 int tag_len, tag;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
181
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
182 pos = url_ftell(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
183 tag_len = pos - swf->tag_pos - 2;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
184 tag = swf->tag;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
185 url_fseek(pb, swf->tag_pos, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
186 if (tag & TAG_LONG) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
187 tag &= ~TAG_LONG;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
188 put_le16(pb, (tag << 6) | 0x3f);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
189 put_le32(pb, tag_len - 4);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
190 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
191 assert(tag_len < 0x3f);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
192 put_le16(pb, (tag << 6) | tag_len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
193 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
194 url_fseek(pb, pos, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
195 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
196
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
197 static inline void max_nbits(int *nbits_ptr, int val)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
198 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
199 int n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
200
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
201 if (val == 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
202 return;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
203 val = abs(val);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
204 n = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
205 while (val != 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
206 n++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
207 val >>= 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
208 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
209 if (n > *nbits_ptr)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
210 *nbits_ptr = n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
211 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
212
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
213 static void put_swf_rect(ByteIOContext *pb,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
214 int xmin, int xmax, int ymin, int ymax)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
215 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
216 PutBitContext p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
217 uint8_t buf[256];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
218 int nbits, mask;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
219
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
220 init_put_bits(&p, buf, sizeof(buf));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
221
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
222 nbits = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
223 max_nbits(&nbits, xmin);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
224 max_nbits(&nbits, xmax);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
225 max_nbits(&nbits, ymin);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
226 max_nbits(&nbits, ymax);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
227 mask = (1 << nbits) - 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
228
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
229 /* rectangle info */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
230 put_bits(&p, 5, nbits);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
231 put_bits(&p, nbits, xmin & mask);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
232 put_bits(&p, nbits, xmax & mask);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
233 put_bits(&p, nbits, ymin & mask);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
234 put_bits(&p, nbits, ymax & mask);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
235
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
236 flush_put_bits(&p);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
237 put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
238 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
239
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
240 static void put_swf_line_edge(PutBitContext *pb, int dx, int dy)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
241 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
242 int nbits, mask;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
243
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
244 put_bits(pb, 1, 1); /* edge */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
245 put_bits(pb, 1, 1); /* line select */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
246 nbits = 2;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
247 max_nbits(&nbits, dx);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
248 max_nbits(&nbits, dy);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
249
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
250 mask = (1 << nbits) - 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
251 put_bits(pb, 4, nbits - 2); /* 16 bits precision */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
252 if (dx == 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
253 put_bits(pb, 1, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
254 put_bits(pb, 1, 1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
255 put_bits(pb, nbits, dy & mask);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
256 } else if (dy == 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
257 put_bits(pb, 1, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
258 put_bits(pb, 1, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
259 put_bits(pb, nbits, dx & mask);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
260 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
261 put_bits(pb, 1, 1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
262 put_bits(pb, nbits, dx & mask);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
263 put_bits(pb, nbits, dy & mask);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
264 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
265 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
266
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
267 #define FRAC_BITS 16
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
268
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
269 /* put matrix */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
270 static void put_swf_matrix(ByteIOContext *pb,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
271 int a, int b, int c, int d, int tx, int ty)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
272 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
273 PutBitContext p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
274 uint8_t buf[256];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
275 int nbits;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
276
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
277 init_put_bits(&p, buf, sizeof(buf));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
278
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
279 put_bits(&p, 1, 1); /* a, d present */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
280 nbits = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
281 max_nbits(&nbits, a);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
282 max_nbits(&nbits, d);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
283 put_bits(&p, 5, nbits); /* nb bits */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
284 put_bits(&p, nbits, a);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
285 put_bits(&p, nbits, d);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
286
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
287 put_bits(&p, 1, 1); /* b, c present */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
288 nbits = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
289 max_nbits(&nbits, c);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
290 max_nbits(&nbits, b);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
291 put_bits(&p, 5, nbits); /* nb bits */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
292 put_bits(&p, nbits, c);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
293 put_bits(&p, nbits, b);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
294
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
295 nbits = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
296 max_nbits(&nbits, tx);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
297 max_nbits(&nbits, ty);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
298 put_bits(&p, 5, nbits); /* nb bits */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
299 put_bits(&p, nbits, tx);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
300 put_bits(&p, nbits, ty);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
301
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
302 flush_put_bits(&p);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
303 put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
304 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
305
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
306 /* */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
307 static int swf_write_header(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
308 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
309 SWFContext *swf;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
310 ByteIOContext *pb = &s->pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
311 AVCodecContext *enc, *audio_enc, *video_enc;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
312 PutBitContext p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
313 uint8_t buf1[256];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
314 int i, width, height, rate, rate_base;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
315
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
316 swf = av_malloc(sizeof(SWFContext));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
317 if (!swf)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
318 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
319 s->priv_data = swf;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
320
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
321 swf->ch_id = -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
322 swf->audio_in_pos = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
323 swf->audio_out_pos = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
324 swf->audio_size = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
325 swf->audio_fifo = av_malloc(AUDIO_FIFO_SIZE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
326 swf->sound_samples = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
327 swf->video_samples = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
328 swf->swf_frame_number = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
329 swf->video_frame_number = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
330
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
331 video_enc = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
332 audio_enc = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
333 for(i=0;i<s->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
334 enc = s->streams[i]->codec;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
335 if (enc->codec_type == CODEC_TYPE_AUDIO)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
336 audio_enc = enc;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
337 else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
338 if ( enc->codec_id == CODEC_ID_VP6F ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
339 enc->codec_id == CODEC_ID_FLV1 ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
340 enc->codec_id == CODEC_ID_MJPEG ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
341 video_enc = enc;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
342 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
343 av_log(enc, AV_LOG_ERROR, "SWF only supports VP6, FLV1 and MJPEG\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
344 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
345 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
346 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
347 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
348
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
349 if (!video_enc) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
350 /* currenty, cannot work correctly if audio only */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
351 swf->video_type = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
352 width = 320;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
353 height = 200;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
354 rate = 10;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
355 rate_base= 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
356 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
357 swf->video_type = video_enc->codec_id;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
358 width = video_enc->width;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
359 height = video_enc->height;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
360 rate = video_enc->time_base.den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
361 rate_base = video_enc->time_base.num;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
362 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
363
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
364 if (!audio_enc ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
365 swf->audio_type = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
366 swf->samples_per_frame = ( 44100. * rate_base ) / rate;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
367 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
368 swf->audio_type = audio_enc->codec_id;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
369 swf->samples_per_frame = ( ( audio_enc->sample_rate ) * rate_base ) / rate;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
370 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
371
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
372 put_tag(pb, "FWS");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
373 if ( video_enc && video_enc->codec_id == CODEC_ID_VP6F ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
374 put_byte(pb, 8); /* version (version 8 and above support VP6 codec) */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
375 } else if ( video_enc && video_enc->codec_id == CODEC_ID_FLV1 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
376 put_byte(pb, 6); /* version (version 6 and above support FLV1 codec) */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
377 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
378 put_byte(pb, 4); /* version (should use 4 for mpeg audio support) */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
379 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
380 put_le32(pb, DUMMY_FILE_SIZE); /* dummy size
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
381 (will be patched if not streamed) */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
382
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
383 put_swf_rect(pb, 0, width * 20, 0, height * 20);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
384 put_le16(pb, (rate * 256) / rate_base); /* frame rate */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
385 swf->duration_pos = url_ftell(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
386 put_le16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
387
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
388 /* define a shape with the jpeg inside */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
389 if ( video_enc && (video_enc->codec_id == CODEC_ID_VP6F ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
390 video_enc->codec_id == CODEC_ID_FLV1 )) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
391 } else if ( video_enc && video_enc->codec_id == CODEC_ID_MJPEG ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
392 put_swf_tag(s, TAG_DEFINESHAPE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
393
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
394 put_le16(pb, SHAPE_ID); /* ID of shape */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
395 /* bounding rectangle */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
396 put_swf_rect(pb, 0, width, 0, height);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
397 /* style info */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
398 put_byte(pb, 1); /* one fill style */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
399 put_byte(pb, 0x41); /* clipped bitmap fill */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
400 put_le16(pb, BITMAP_ID); /* bitmap ID */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
401 /* position of the bitmap */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
402 put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
403 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
404 put_byte(pb, 0); /* no line style */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
405
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
406 /* shape drawing */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
407 init_put_bits(&p, buf1, sizeof(buf1));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
408 put_bits(&p, 4, 1); /* one fill bit */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
409 put_bits(&p, 4, 0); /* zero line bit */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
410
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
411 put_bits(&p, 1, 0); /* not an edge */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
412 put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
413 put_bits(&p, 5, 1); /* nbits */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
414 put_bits(&p, 1, 0); /* X */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
415 put_bits(&p, 1, 0); /* Y */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
416 put_bits(&p, 1, 1); /* set fill style 1 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
417
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
418 /* draw the rectangle ! */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
419 put_swf_line_edge(&p, width, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
420 put_swf_line_edge(&p, 0, height);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
421 put_swf_line_edge(&p, -width, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
422 put_swf_line_edge(&p, 0, -height);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
423
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
424 /* end of shape */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
425 put_bits(&p, 1, 0); /* not an edge */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
426 put_bits(&p, 5, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
427
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
428 flush_put_bits(&p);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
429 put_buffer(pb, buf1, pbBufPtr(&p) - p.buf);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
430
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
431 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
432 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
433
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
434 if (audio_enc && audio_enc->codec_id == CODEC_ID_MP3 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
435 int v;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
436
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
437 /* start sound */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
438 put_swf_tag(s, TAG_STREAMHEAD2);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
439
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
440 v = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
441 switch(audio_enc->sample_rate) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
442 case 11025:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
443 v |= 1 << 2;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
444 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
445 case 22050:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
446 v |= 2 << 2;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
447 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
448 case 44100:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
449 v |= 3 << 2;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
450 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
451 default:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
452 /* not supported */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
453 av_free(swf->audio_fifo);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
454 av_free(swf);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
455 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
456 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
457 v |= 0x02; /* 16 bit playback */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
458 if (audio_enc->channels == 2)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
459 v |= 0x01; /* stereo playback */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
460 put_byte(&s->pb, v);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
461 v |= 0x20; /* mp3 compressed */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
462 put_byte(&s->pb, v);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
463 put_le16(&s->pb, swf->samples_per_frame); /* avg samples per frame */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
464 put_le16(&s->pb, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
465
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
466 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
467 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
468
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
469 put_flush_packet(&s->pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
470 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
471 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
472
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
473 static int swf_write_video(AVFormatContext *s,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
474 AVCodecContext *enc, const uint8_t *buf, int size)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
475 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
476 SWFContext *swf = s->priv_data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
477 ByteIOContext *pb = &s->pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
478 int c = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
479 int outSize = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
480 int outSamples = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
481
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
482 /* Flash Player limit */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
483 if ( swf->swf_frame_number == 16000 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
484 av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
485 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
486
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
487 if ( swf->audio_type ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
488 /* Prescan audio data for this swf frame */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
489 retry_swf_audio_packet:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
490 if ( ( swf->audio_size-outSize ) >= 4 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
491 int mp3FrameSize = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
492 int mp3SampleRate = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
493 int mp3IsMono = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
494 int mp3SamplesPerFrame = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
495
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
496 /* copy out mp3 header from ring buffer */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
497 uint8_t header[4];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
498 for (c=0; c<4; c++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
499 header[c] = swf->audio_fifo[(swf->audio_in_pos+outSize+c) % AUDIO_FIFO_SIZE];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
500 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
501
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
502 if ( swf_mp3_info(header,&mp3FrameSize,&mp3SamplesPerFrame,&mp3SampleRate,&mp3IsMono) ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
503 if ( ( swf->audio_size-outSize ) >= mp3FrameSize ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
504 outSize += mp3FrameSize;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
505 outSamples += mp3SamplesPerFrame;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
506 if ( ( swf->sound_samples + outSamples + swf->samples_per_frame ) < swf->video_samples ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
507 goto retry_swf_audio_packet;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
508 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
509 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
510 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
511 /* invalid mp3 data, skip forward
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
512 we need to do this since the Flash Player
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
513 does not like custom headers */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
514 swf->audio_in_pos ++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
515 swf->audio_size --;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
516 swf->audio_in_pos %= AUDIO_FIFO_SIZE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
517 goto retry_swf_audio_packet;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
518 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
519 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
520
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
521 /* audio stream is behind video stream, bail */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
522 if ( ( swf->sound_samples + outSamples + swf->samples_per_frame ) < swf->video_samples ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
523 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
524 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
525 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
526
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
527 if ( swf->video_type == CODEC_ID_VP6F ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
528 swf->video_type == CODEC_ID_FLV1 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
529 if ( swf->video_frame_number == 0 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
530 /* create a new video object */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
531 put_swf_tag(s, TAG_VIDEOSTREAM);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
532 put_le16(pb, VIDEO_ID);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
533 put_le16(pb, 15000 ); /* hard flash player limit */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
534 put_le16(pb, enc->width);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
535 put_le16(pb, enc->height);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
536 put_byte(pb, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
537 put_byte(pb,codec_get_tag(swf_codec_tags,swf->video_type));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
538 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
539
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
540 /* place the video object for the first time */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
541 put_swf_tag(s, TAG_PLACEOBJECT2);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
542 put_byte(pb, 0x36);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
543 put_le16(pb, 1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
544 put_le16(pb, VIDEO_ID);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
545 put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
546 put_le16(pb, swf->video_frame_number );
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
547 put_byte(pb, 'v');
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
548 put_byte(pb, 'i');
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
549 put_byte(pb, 'd');
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
550 put_byte(pb, 'e');
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
551 put_byte(pb, 'o');
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
552 put_byte(pb, 0x00);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
553 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
554 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
555 /* mark the character for update */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
556 put_swf_tag(s, TAG_PLACEOBJECT2);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
557 put_byte(pb, 0x11);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
558 put_le16(pb, 1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
559 put_le16(pb, swf->video_frame_number );
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
560 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
561 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
562
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
563 /* set video frame data */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
564 put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
565 put_le16(pb, VIDEO_ID);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
566 put_le16(pb, swf->video_frame_number++ );
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
567 put_buffer(pb, buf, size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
568 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
569 } else if ( swf->video_type == CODEC_ID_MJPEG ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
570 if (swf->swf_frame_number > 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
571 /* remove the shape */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
572 put_swf_tag(s, TAG_REMOVEOBJECT);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
573 put_le16(pb, SHAPE_ID); /* shape ID */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
574 put_le16(pb, 1); /* depth */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
575 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
576
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
577 /* free the bitmap */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
578 put_swf_tag(s, TAG_FREECHARACTER);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
579 put_le16(pb, BITMAP_ID);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
580 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
581 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
582
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
583 put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
584
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
585 put_le16(pb, BITMAP_ID); /* ID of the image */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
586
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
587 /* a dummy jpeg header seems to be required */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
588 put_byte(pb, 0xff);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
589 put_byte(pb, 0xd8);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
590 put_byte(pb, 0xff);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
591 put_byte(pb, 0xd9);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
592 /* write the jpeg image */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
593 put_buffer(pb, buf, size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
594
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
595 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
596
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
597 /* draw the shape */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
598
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
599 put_swf_tag(s, TAG_PLACEOBJECT);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
600 put_le16(pb, SHAPE_ID); /* shape ID */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
601 put_le16(pb, 1); /* depth */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
602 put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
603 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
604 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
605 /* invalid codec */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
606 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
607
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
608 swf->swf_frame_number ++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
609
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
610 swf->video_samples += swf->samples_per_frame;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
611
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
612 /* streaming sound always should be placed just before showframe tags */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
613 if ( outSize > 0 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
614 put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
615 put_le16(pb, outSamples);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
616 put_le16(pb, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
617 for (c=0; c<outSize; c++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
618 put_byte(pb,swf->audio_fifo[(swf->audio_in_pos+c) % AUDIO_FIFO_SIZE]);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
619 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
620 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
621
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
622 /* update FIFO */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
623 swf->sound_samples += outSamples;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
624 swf->audio_in_pos += outSize;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
625 swf->audio_size -= outSize;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
626 swf->audio_in_pos %= AUDIO_FIFO_SIZE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
627 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
628
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
629 /* output the frame */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
630 put_swf_tag(s, TAG_SHOWFRAME);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
631 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
632
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
633 put_flush_packet(&s->pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
634
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
635 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
636 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
637
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
638 static int swf_write_audio(AVFormatContext *s,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
639 AVCodecContext *enc, const uint8_t *buf, int size)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
640 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
641 SWFContext *swf = s->priv_data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
642 int c = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
643
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
644 /* Flash Player limit */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
645 if ( swf->swf_frame_number == 16000 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
646 av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
647 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
648
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
649 if (enc->codec_id == CODEC_ID_MP3 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
650 for (c=0; c<size; c++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
651 swf->audio_fifo[(swf->audio_out_pos+c)%AUDIO_FIFO_SIZE] = buf[c];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
652 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
653 swf->audio_size += size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
654 swf->audio_out_pos += size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
655 swf->audio_out_pos %= AUDIO_FIFO_SIZE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
656 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
657
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
658 /* if audio only stream make sure we add swf frames */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
659 if ( swf->video_type == 0 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
660 swf_write_video(s, enc, 0, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
661 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
662
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
663 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
664 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
665
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
666 static int swf_write_packet(AVFormatContext *s, AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
667 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
668 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
669 if (codec->codec_type == CODEC_TYPE_AUDIO)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
670 return swf_write_audio(s, codec, pkt->data, pkt->size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
671 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
672 return swf_write_video(s, codec, pkt->data, pkt->size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
673 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
674
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
675 static int swf_write_trailer(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
676 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
677 SWFContext *swf = s->priv_data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
678 ByteIOContext *pb = &s->pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
679 AVCodecContext *enc, *video_enc;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
680 int file_size, i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
681
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
682 video_enc = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
683 for(i=0;i<s->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
684 enc = s->streams[i]->codec;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
685 if (enc->codec_type == CODEC_TYPE_VIDEO)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
686 video_enc = enc;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
687 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
688
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
689 put_swf_tag(s, TAG_END);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
690 put_swf_end_tag(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
691
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
692 put_flush_packet(&s->pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
693
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
694 /* patch file size and number of frames if not streamed */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
695 if (!url_is_streamed(&s->pb) && video_enc) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
696 file_size = url_ftell(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
697 url_fseek(pb, 4, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
698 put_le32(pb, file_size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
699 url_fseek(pb, swf->duration_pos, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
700 put_le16(pb, video_enc->frame_number);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
701 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
702
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
703 av_free(swf->audio_fifo);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
704
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
705 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
706 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
707 #endif //CONFIG_MUXERS
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
708
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
709 /*********************************************/
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
710 /* Extract FLV encoded frame and MP3 from swf
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
711 Note that the detection of the real frame
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
712 is inaccurate at this point as it can be
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
713 quite tricky to determine, you almost certainly
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
714 will get a bad audio/video sync */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
715
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
716 static int get_swf_tag(ByteIOContext *pb, int *len_ptr)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
717 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
718 int tag, len;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
719
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
720 if (url_feof(pb))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
721 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
722
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
723 tag = get_le16(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
724 len = tag & 0x3f;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
725 tag = tag >> 6;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
726 if (len == 0x3f) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
727 len = get_le32(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
728 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
729 // av_log(NULL, AV_LOG_DEBUG, "Tag: %d - Len: %d\n", tag, len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
730 *len_ptr = len;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
731 return tag;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
732 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
733
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
734
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
735 static int swf_probe(AVProbeData *p)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
736 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
737 /* check file header */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
738 if (p->buf_size <= 16)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
739 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
740 if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
741 p->buf[2] == 'S')
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
742 return AVPROBE_SCORE_MAX;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
743 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
744 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
745 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
746
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
747 static int swf_read_header(AVFormatContext *s, AVFormatParameters *ap)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
748 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
749 SWFContext *swf = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
750 ByteIOContext *pb = &s->pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
751 int nbits, len, frame_rate, tag, v;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
752 offset_t firstTagOff;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
753 AVStream *ast = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
754 AVStream *vst = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
755
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
756 swf = av_malloc(sizeof(SWFContext));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
757 if (!swf)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
758 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
759 s->priv_data = swf;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
760
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
761 tag = get_be32(pb) & 0xffffff00;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
762
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
763 if (tag == MKBETAG('C', 'W', 'S', 0))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
764 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
765 av_log(s, AV_LOG_ERROR, "Compressed SWF format not supported\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
766 return AVERROR_IO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
767 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
768 if (tag != MKBETAG('F', 'W', 'S', 0))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
769 return AVERROR_IO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
770 get_le32(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
771 /* skip rectangle size */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
772 nbits = get_byte(pb) >> 3;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
773 len = (4 * nbits - 3 + 7) / 8;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
774 url_fskip(pb, len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
775 frame_rate = get_le16(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
776 get_le16(pb); /* frame count */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
777
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
778 /* The Flash Player converts 8.8 frame rates
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
779 to milliseconds internally. Do the same to get
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
780 a correct framerate */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
781 swf->ms_per_frame = ( 1000 * 256 ) / frame_rate;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
782 swf->samples_per_frame = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
783 swf->ch_id = -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
784
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
785 firstTagOff = url_ftell(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
786 for(;;) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
787 tag = get_swf_tag(pb, &len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
788 if (tag < 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
789 if ( ast || vst ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
790 if ( vst && ast ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
791 vst->codec->time_base.den = ast->codec->sample_rate / swf->samples_per_frame;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
792 vst->codec->time_base.num = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
793 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
794 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
795 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
796 av_log(s, AV_LOG_ERROR, "No media found in SWF\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
797 return AVERROR_IO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
798 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
799 if ( tag == TAG_VIDEOSTREAM && !vst) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
800 int codec_id;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
801 swf->ch_id = get_le16(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
802 get_le16(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
803 get_le16(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
804 get_le16(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
805 get_byte(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
806 /* Check for FLV1 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
807 codec_id = codec_get_id(swf_codec_tags, get_byte(pb));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
808 if ( codec_id ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
809 vst = av_new_stream(s, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
810 av_set_pts_info(vst, 24, 1, 1000); /* 24 bit pts in ms */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
811
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
812 vst->codec->codec_type = CODEC_TYPE_VIDEO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
813 vst->codec->codec_id = codec_id;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
814 if ( swf->samples_per_frame ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
815 vst->codec->time_base.den = 1000. / swf->ms_per_frame;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
816 vst->codec->time_base.num = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
817 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
818 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
819 } else if ( ( tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2 ) && !ast) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
820 /* streaming found */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
821 get_byte(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
822 v = get_byte(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
823 swf->samples_per_frame = get_le16(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
824 if (len!=4)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
825 url_fskip(pb,len-4);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
826 /* if mp3 streaming found, OK */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
827 if ((v & 0x20) != 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
828 if ( tag == TAG_STREAMHEAD2 ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
829 get_le16(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
830 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
831 ast = av_new_stream(s, 1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
832 av_set_pts_info(ast, 24, 1, 1000); /* 24 bit pts in ms */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
833 if (!ast)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
834 return -ENOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
835
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
836 if (v & 0x01)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
837 ast->codec->channels = 2;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
838 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
839 ast->codec->channels = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
840
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
841 switch((v>> 2) & 0x03) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
842 case 1:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
843 ast->codec->sample_rate = 11025;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
844 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
845 case 2:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
846 ast->codec->sample_rate = 22050;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
847 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
848 case 3:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
849 ast->codec->sample_rate = 44100;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
850 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
851 default:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
852 av_free(ast);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
853 return AVERROR_IO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
854 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
855 ast->codec->codec_type = CODEC_TYPE_AUDIO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
856 ast->codec->codec_id = CODEC_ID_MP3;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
857 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
858 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
859 url_fskip(pb, len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
860 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
861 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
862 url_fseek(pb, firstTagOff, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
863
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
864 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
865 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
866
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
867 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
868 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
869 SWFContext *swf = s->priv_data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
870 ByteIOContext *pb = &s->pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
871 AVStream *st = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
872 int tag, len, i, frame;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
873
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
874 for(;;) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
875 tag = get_swf_tag(pb, &len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
876 if (tag < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
877 return AVERROR_IO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
878 if (tag == TAG_VIDEOFRAME) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
879 for( i=0; i<s->nb_streams; i++ ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
880 st = s->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
881 if (st->id == 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
882 if ( get_le16(pb) == swf->ch_id ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
883 frame = get_le16(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
884 av_get_packet(pb, pkt, len-4);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
885 pkt->pts = frame * swf->ms_per_frame;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
886 pkt->stream_index = st->index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
887 return pkt->size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
888 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
889 url_fskip(pb, len-2);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
890 continue;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
891 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
892 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
893 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
894 url_fskip(pb, len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
895 } else if (tag == TAG_STREAMBLOCK) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
896 for( i=0; i<s->nb_streams; i++ ) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
897 st = s->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
898 if (st->id == 1) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
899 av_get_packet(pb, pkt, len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
900 pkt->stream_index = st->index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
901 return pkt->size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
902 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
903 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
904 url_fskip(pb, len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
905 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
906 url_fskip(pb, len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
907 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
908 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
909 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
910 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
911
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
912 static int swf_read_close(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
913 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
914 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
915 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
916
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
917 #ifdef CONFIG_SWF_DEMUXER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
918 AVInputFormat swf_demuxer = {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
919 "swf",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
920 "Flash format",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
921 sizeof(SWFContext),
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
922 swf_probe,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
923 swf_read_header,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
924 swf_read_packet,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
925 swf_read_close,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
926 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
927 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
928 #ifdef CONFIG_SWF_MUXER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
929 AVOutputFormat swf_muxer = {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
930 "swf",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
931 "Flash format",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
932 "application/x-shockwave-flash",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
933 "swf",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
934 sizeof(SWFContext),
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
935 CODEC_ID_MP3,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
936 CODEC_ID_FLV1,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
937 swf_write_header,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
938 swf_write_packet,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
939 swf_write_trailer,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
940 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
941 #endif