annotate flvenc.c @ 240:e6f99d238179 libavformat

get_bi(), be/le fix
author michaelni
date Wed, 10 Sep 2003 13:00:24 +0000
parents eb90c0a5a1ba
children 3d92f793fd67
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
164
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
1 /*
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
2 * FLV encoder.
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
3 * Copyright (c) 2003 The FFmpeg Project.
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
4 *
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
9 *
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
13 * Lesser General Public License for more details.
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
14 *
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
18 */
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
19 #include "avformat.h"
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
20
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
21 #define VIDEO_FIFO_SIZE 512
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
22
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
23 typedef struct FLVFrame {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
24 int type;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
25 int timestamp;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
26 int flags;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
27 uint8_t *data;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
28 int size;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
29 struct FLVFrame *next;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
30 } FLVFrame;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
31
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
32 typedef struct FLVContext {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
33 int hasAudio;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
34 int hasVideo;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
35 #ifdef CONFIG_MP3LAME
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
36 int audioTime;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
37 int audioInPos;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
38 int audioOutPos;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
39 int audioSize;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
40 int audioRate;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
41 int initDelay;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
42 int soundDelay;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
43 uint8_t *audioFifo;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
44 int64_t sampleCount;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
45 #endif // CONFIG_MP3LAME
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
46 int64_t frameCount;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
47 FLVFrame *frames;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
48 } FLVContext;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
49
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
50
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
51 #ifdef CONFIG_MP3LAME
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
52
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
53 #define AUDIO_FIFO_SIZE 65536
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
54
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
55 static const int sSampleRates[3][4] = {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
56 {44100, 48000, 32000, 0},
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
57 {22050, 24000, 16000, 0},
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
58 {11025, 12000, 8000, 0},
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
59 };
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
60
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
61 static const int sBitRates[2][3][15] = {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
62 { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448},
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
63 { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384},
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
64 { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
65 },
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
66 { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256},
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
67 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
68 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
69 },
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
70 };
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
71
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
72 static const int sSamplesPerFrame[3][3] =
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
73 {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
74 { 384, 1152, 1152 },
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
75 { 384, 1152, 576 },
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
76 { 384, 1152, 576 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
77 };
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
78
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
79 static const int sBitsPerSlot[3] = {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
80 32,
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
81 8,
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
82 8
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
83 };
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
84
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
85 static int mp3info(void *data, int *byteSize, int *samplesPerFrame, int *sampleRate, int *isMono )
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
86 {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
87 uint8_t *dataTmp = (uint8_t *)data;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
88 uint32_t header = ( (uint32_t)dataTmp[0] << 24 ) | ( (uint32_t)dataTmp[1] << 16 ) | ( (uint32_t)dataTmp[2] << 8 ) | (uint32_t)dataTmp[3];
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
89 int layerID = 3 - ((header >> 17) & 0x03);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
90 int bitRateID = ((header >> 12) & 0x0f);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
91 int sampleRateID = ((header >> 10) & 0x03);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
92 int bitRate = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
93 int bitsPerSlot = sBitsPerSlot[layerID];
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
94 int isPadded = ((header >> 9) & 0x01);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
95
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
96 if ( (( header >> 21 ) & 0x7ff) != 0x7ff ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
97 return 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
98 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
99
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
100 if ( !isPadded ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
101 printf("Fatal error: mp3 data is not padded!\n");
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
102 exit(0);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
103 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
104
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
105 *isMono = ((header >> 6) & 0x03) == 0x03;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
106
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
107 if ( (header >> 19 ) & 0x01 ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
108 *sampleRate = sSampleRates[0][sampleRateID];
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
109 bitRate = sBitRates[0][layerID][bitRateID] * 1000;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
110 *samplesPerFrame = sSamplesPerFrame[0][layerID];
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
111
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
112 } else {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
113 if ( (header >> 20) & 0x01 ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
114 *sampleRate = sSampleRates[1][sampleRateID];
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
115 bitRate = sBitRates[1][layerID][bitRateID] * 1000;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
116 *samplesPerFrame = sSamplesPerFrame[1][layerID];
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
117 } else {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
118 *sampleRate = sSampleRates[2][sampleRateID];
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
119 bitRate = sBitRates[1][layerID][bitRateID] * 1000;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
120 *samplesPerFrame = sSamplesPerFrame[2][layerID];
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
121 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
122 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
123
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
124 *byteSize = ( ( ( ( *samplesPerFrame * (bitRate / bitsPerSlot) ) / *sampleRate ) + isPadded ) * bitsPerSlot);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
125
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
126 return 1;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
127 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
128 #endif // CONFIG_MP3LAME
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
129
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
130 static int flv_write_header(AVFormatContext *s)
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
131 {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
132 ByteIOContext *pb = &s->pb;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
133 FLVContext *flv = s->priv_data;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
134
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
135 av_set_pts_info(s, 24, 1, 1000); /* 24 bit pts in ms */
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
136
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
137 flv->hasAudio = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
138 flv->hasVideo = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
139
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
140 #ifdef CONFIG_MP3LAME
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
141 flv->audioTime = -1;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
142 flv->audioFifo = av_malloc(AUDIO_FIFO_SIZE);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
143 flv->audioInPos = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
144 flv->audioOutPos = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
145 flv->audioSize = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
146 flv->audioRate = 44100;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
147 flv->initDelay = -1;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
148 flv->soundDelay = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
149 #endif // CONFIG_MP3LAME
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
150
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
151 flv->frames = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
152
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
153 put_tag(pb,"FLV");
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
154 put_byte(pb,1);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
155 put_byte(pb,0); // delayed write
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
156 put_be32(pb,9);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
157 put_be32(pb,0);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
158
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
159 return 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
160 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
161
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
162 static void put_be24(ByteIOContext *pb, int value)
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
163 {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
164 put_byte(pb, (value>>16) & 0xFF );
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
165 put_byte(pb, (value>> 8) & 0xFF );
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
166 put_byte(pb, (value>> 0) & 0xFF );
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
167 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
168
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
169 static void InsertSorted(FLVContext *flv, FLVFrame *frame)
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
170 {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
171 if ( !flv->frames ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
172 flv->frames = frame;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
173 } else {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
174 FLVFrame *trav = flv->frames;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
175 FLVFrame *prev = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
176 for (;trav;) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
177 if ( trav->timestamp >= frame->timestamp ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
178 frame->next = trav;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
179 if ( prev ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
180 prev->next = frame;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
181 } else {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
182 flv->frames = frame;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
183 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
184 break;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
185 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
186 prev = trav;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
187 trav = trav->next;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
188 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
189 if ( !trav ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
190 prev->next = frame;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
191 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
192 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
193 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
194
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
195 static void DumpFrame(ByteIOContext *pb, FLVFrame *frame)
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
196 {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
197 put_byte(pb,frame->type); // message type
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
198 put_be24(pb,frame->size+1); // include flags
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
199 put_be24(pb,frame->timestamp); // time stamp
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
200 put_be32(pb,0); // reserved
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
201 put_byte(pb,frame->flags);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
202 put_buffer(pb, frame->data, frame->size);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
203 put_be32(pb,frame->size+1+11); // reserved
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
204 av_free(frame->data);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
205 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
206
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
207 static void Dump(FLVContext *flv, ByteIOContext *pb, int count)
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
208 {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
209 int c=0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
210 FLVFrame *trav = flv->frames;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
211 FLVFrame *prev = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
212 for (;trav;c++) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
213 trav = trav->next;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
214 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
215 trav = flv->frames;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
216 for ( ; c >= count; c-- ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
217 DumpFrame(pb,trav);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
218 prev = trav;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
219 trav = trav->next;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
220 av_free(prev);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
221 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
222 flv->frames = trav;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
223 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
224
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
225 static int flv_write_trailer(AVFormatContext *s)
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
226 {
166
2271829b6f7e Building ffmpeg with gcc-2.95.3 encountered a problem due to C99 initialization fix by ("Steven M. Schultz" <sms at 2BSD dot COM>)
michaelni
parents: 164
diff changeset
227 int64_t file_size;
2271829b6f7e Building ffmpeg with gcc-2.95.3 encountered a problem due to C99 initialization fix by ("Steven M. Schultz" <sms at 2BSD dot COM>)
michaelni
parents: 164
diff changeset
228 int flags = 0;
2271829b6f7e Building ffmpeg with gcc-2.95.3 encountered a problem due to C99 initialization fix by ("Steven M. Schultz" <sms at 2BSD dot COM>)
michaelni
parents: 164
diff changeset
229
164
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
230 ByteIOContext *pb = &s->pb;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
231 FLVContext *flv = s->priv_data;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
232
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
233 Dump(flv,pb,1);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
234
166
2271829b6f7e Building ffmpeg with gcc-2.95.3 encountered a problem due to C99 initialization fix by ("Steven M. Schultz" <sms at 2BSD dot COM>)
michaelni
parents: 164
diff changeset
235 file_size = url_ftell(pb);
164
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
236 flags |= flv->hasAudio ? 4 : 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
237 flags |= flv->hasVideo ? 1 : 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
238 url_fseek(pb, 4, SEEK_SET);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
239 put_byte(pb,flags);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
240 url_fseek(pb, file_size, SEEK_SET);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
241 return 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
242 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
243
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
244 static int flv_write_packet(AVFormatContext *s, int stream_index,
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
245 uint8_t *buf, int size, int timestamp)
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
246 {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
247 ByteIOContext *pb = &s->pb;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
248 AVCodecContext *enc = &s->streams[stream_index]->codec;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
249 FLVContext *flv = s->priv_data;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
250
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
251 if (enc->codec_type == CODEC_TYPE_VIDEO) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
252 FLVFrame *frame = av_malloc(sizeof(FLVFrame));
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
253 frame->next = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
254 frame->type = 9;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
255 frame->flags = 2; // choose h263
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
256 frame->flags |= enc->coded_frame->key_frame ? 0x10 : 0x20; // add keyframe indicator
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
257 frame->timestamp = timestamp;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
258 //frame->timestamp = ( ( flv->frameCount * (int64_t)FRAME_RATE_BASE * (int64_t)1000 ) / (int64_t)enc->frame_rate );
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
259 //printf("%08x %f %f\n",frame->timestamp,(double)enc->frame_rate/(double)FRAME_RATE_BASE,1000*(double)FRAME_RATE_BASE/(double)enc->frame_rate);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
260 frame->size = size;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
261 frame->data = av_malloc(size);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
262 memcpy(frame->data,buf,size);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
263 flv->hasVideo = 1;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
264
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
265 InsertSorted(flv,frame);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
266
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
267 flv->frameCount ++;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
268 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
269 else if (enc->codec_type == CODEC_TYPE_AUDIO) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
270 #ifdef CONFIG_MP3LAME
232
eb90c0a5a1ba CODEC_ID_MP3LAME is obsolete
bellard
parents: 166
diff changeset
271 if (enc->codec_id == CODEC_ID_MP3 ) {
164
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
272 int c=0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
273 for (;c<size;c++) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
274 flv->audioFifo[(flv->audioOutPos+c)%AUDIO_FIFO_SIZE] = buf[c];
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
275 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
276 flv->audioSize += size;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
277 flv->audioOutPos += size;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
278 flv->audioOutPos %= AUDIO_FIFO_SIZE;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
279
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
280 if ( flv->initDelay == -1 ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
281 flv->initDelay = timestamp;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
282 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
283
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
284 if ( flv->audioTime == -1 ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
285 flv->audioTime = timestamp;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
286 // flv->audioTime = ( ( ( flv->sampleCount - enc->delay ) * 8000 ) / flv->audioRate ) - flv->initDelay - 250;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
287 // if ( flv->audioTime < 0 ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
288 // flv->audioTime = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
289 // }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
290 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
291 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
292 for ( ; flv->audioSize >= 4 ; ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
293
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
294 int mp3FrameSize = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
295 int mp3SampleRate = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
296 int mp3IsMono = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
297 int mp3SamplesPerFrame = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
298
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
299 if ( mp3info(&flv->audioFifo[flv->audioInPos],&mp3FrameSize,&mp3SamplesPerFrame,&mp3SampleRate,&mp3IsMono) ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
300 if ( flv->audioSize >= mp3FrameSize ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
301
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
302 int soundFormat = 0x22;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
303 int c=0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
304 FLVFrame *frame = av_malloc(sizeof(FLVFrame));
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
305
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
306 flv->audioRate = mp3SampleRate;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
307
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
308 switch (mp3SampleRate) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
309 case 44100:
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
310 soundFormat |= 0x0C;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
311 break;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
312 case 22050:
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
313 soundFormat |= 0x08;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
314 break;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
315 case 11025:
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
316 soundFormat |= 0x04;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
317 break;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
318 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
319
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
320 if ( !mp3IsMono ) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
321 soundFormat |= 0x01;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
322 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
323
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
324 frame->next = 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
325 frame->type = 8;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
326 frame->flags = soundFormat;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
327 frame->timestamp = flv->audioTime;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
328 frame->size = mp3FrameSize;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
329 frame->data = av_malloc(mp3FrameSize);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
330
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
331 for (;c<mp3FrameSize;c++) {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
332 frame->data[c] = flv->audioFifo[(flv->audioInPos+c)%AUDIO_FIFO_SIZE];
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
333 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
334
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
335 flv->audioInPos += mp3FrameSize;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
336 flv->audioSize -= mp3FrameSize;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
337 flv->audioInPos %= AUDIO_FIFO_SIZE;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
338 flv->sampleCount += mp3SamplesPerFrame;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
339
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
340 // Reset audio for next round
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
341 flv->audioTime = -1;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
342 // We got audio! Make sure we set this to the global flags on closure
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
343 flv->hasAudio = 1;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
344
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
345 InsertSorted(flv,frame);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
346 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
347 break;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
348 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
349 flv->audioInPos ++;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
350 flv->audioSize --;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
351 flv->audioInPos %= AUDIO_FIFO_SIZE;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
352 // no audio in here!
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
353 flv->audioTime = -1;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
354 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
355 #endif
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
356 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
357 Dump(flv,pb,128);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
358 put_flush_packet(pb);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
359 return 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
360 }
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
361
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
362 static AVOutputFormat flv_oformat = {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
363 "flv",
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
364 "flv format",
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
365 "video/x-flashvideo",
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
366 "flv",
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
367 sizeof(FLVContext),
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
368 #ifdef CONFIG_MP3LAME
232
eb90c0a5a1ba CODEC_ID_MP3LAME is obsolete
bellard
parents: 166
diff changeset
369 CODEC_ID_MP3,
164
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
370 #else // CONFIG_MP3LAME
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
371 CODEC_ID_NONE,
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
372 #endif // CONFIG_MP3LAME
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
373 CODEC_ID_FLV1,
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
374 flv_write_header,
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
375 flv_write_packet,
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
376 flv_write_trailer,
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
377 };
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
378
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
379 int flvenc_init(void)
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
380 {
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
381 av_register_output_format(&flv_oformat);
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
382 return 0;
99fbacf0f764 flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
diff changeset
383 }