1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <linux/videodev.h>
#include <linux/soundcard.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/time.h>
#include <getopt.h>
#include <string.h>
#include "mpegenc.h"
AVFormat *first_format;
/* XXX: suppress it ! */
int data_out_size;
const char *comment_string =
"+title=Test Video +author=FFMpeg +copyright=Free +comment=Generated by FFMpeg 1.0";
void register_avformat(AVFormat *format)
{
AVFormat **p;
p = &first_format;
while (*p != NULL) p = &(*p)->next;
*p = format;
format->next = NULL;
}
AVFormat *guess_format(const char *short_name, const char *filename, const char *mime_type)
{
AVFormat *fmt, *fmt_found;
int score_max, score;
const char *ext, *p;
char ext1[32], *q;
/* find the proper file type */
fmt_found = NULL;
score_max = 0;
fmt = first_format;
while (fmt != NULL) {
score = 0;
if (fmt->name && short_name && !strcmp(fmt->name, short_name))
score += 100;
if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
score += 10;
if (filename && fmt->extensions) {
ext = strrchr(filename, '.');
if (ext) {
ext++;
p = fmt->extensions;
for(;;) {
q = ext1;
while (*p != '\0' && *p != ',')
*q++ = *p++;
*q = '\0';
if (!strcasecmp(ext1, ext)) {
score += 5;
break;
}
if (*p == '\0')
break;
p++;
}
}
}
if (score > score_max) {
score_max = score;
fmt_found = fmt;
}
fmt = fmt->next;
}
return fmt_found;
}
/* return TRUE if val is a prefix of str. If it returns TRUE, ptr is
set to the next character in 'str' after the prefix */
int strstart(const char *str, const char *val, const char **ptr)
{
const char *p, *q;
p = str;
q = val;
while (*q != '\0') {
if (*p != *q)
return 0;
p++;
q++;
}
if (ptr)
*ptr = p;
return 1;
}
/* simple formats */
int raw_write_header(struct AVFormatContext *s)
{
return 0;
}
int raw_write_audio(struct AVFormatContext *s,
unsigned char *buf, int size)
{
put_buffer(&s->pb, buf, size);
put_flush_packet(&s->pb);
return 0;
}
int raw_write_video(struct AVFormatContext *s,
unsigned char *buf, int size)
{
put_buffer(&s->pb, buf, size);
put_flush_packet(&s->pb);
return 0;
}
int raw_write_trailer(struct AVFormatContext *s)
{
return 0;
}
AVFormat mp2_format = {
"mp2",
"MPEG audio layer 2",
"audio/x-mpeg",
"mp2,mp3",
CODEC_ID_MP2,
0,
raw_write_header,
raw_write_audio,
NULL,
raw_write_trailer,
};
AVFormat ac3_format = {
"ac3",
"raw ac3",
"audio/x-ac3",
"ac3",
CODEC_ID_AC3,
0,
raw_write_header,
raw_write_audio,
NULL,
raw_write_trailer,
};
AVFormat h263_format = {
"h263",
"raw h263",
"video/x-h263",
"h263",
0,
CODEC_ID_H263,
raw_write_header,
NULL,
raw_write_video,
raw_write_trailer,
};
AVFormat mpeg1video_format = {
"mpeg1video",
"MPEG1 video",
"video/mpeg",
"mpg,mpeg",
0,
CODEC_ID_MPEG1VIDEO,
raw_write_header,
NULL,
raw_write_video,
raw_write_trailer,
};
/* encoder management */
AVEncoder *first_encoder;
void register_avencoder(AVEncoder *format)
{
AVEncoder **p;
p = &first_encoder;
while (*p != NULL) p = &(*p)->next;
*p = format;
format->next = NULL;
}
int avencoder_open(AVEncodeContext *avctx, AVEncoder *codec)
{
int ret;
avctx->codec = codec;
avctx->frame_number = 0;
avctx->priv_data = malloc(codec->priv_data_size);
if (!avctx->priv_data)
return -ENOMEM;
memset(avctx->priv_data, 0, codec->priv_data_size);
ret = avctx->codec->init(avctx);
if (ret < 0) {
free(avctx->priv_data);
avctx->priv_data = NULL;
return ret;
}
return 0;
}
int avencoder_encode(AVEncodeContext *avctx, UINT8 *buf, int buf_size, void *data)
{
int ret;
ret = avctx->codec->encode(avctx, buf, buf_size, data);
avctx->frame_number++;
return ret;
}
int avencoder_close(AVEncodeContext *avctx)
{
if (avctx->codec->close)
avctx->codec->close(avctx);
free(avctx->priv_data);
avctx->priv_data = NULL;
return 0;
}
AVEncoder *avencoder_find(enum CodecID id)
{
AVEncoder *p;
p = first_encoder;
while (p) {
if (p->id == id)
return p;
p = p->next;
}
return NULL;
}
void avencoder_string(char *buf, int buf_size, AVEncodeContext *enc)
{
switch(enc->codec->type) {
case CODEC_TYPE_VIDEO:
snprintf(buf, buf_size,
"Video: %s, %dx%d, %d fps, %d kb/s",
enc->codec->name, enc->width, enc->height, enc->rate, enc->bit_rate / 1000);
break;
case CODEC_TYPE_AUDIO:
snprintf(buf, buf_size,
"Audio: %s, %d Hz, %s, %d kb/s",
enc->codec->name, enc->rate,
enc->channels == 2 ? "stereo" : "mono",
enc->bit_rate / 1000);
break;
default:
abort();
}
}
/* PutByteFormat */
int init_put_byte(PutByteContext *s,
unsigned char *buffer,
int buffer_size,
void *opaque,
void (*write_packet)(void *opaque, UINT8 *buf, int buf_size),
int (*write_seek)(void *opaque, long long offset, int whence))
{
s->buffer = buffer;
s->buf_ptr = buffer;
s->buf_end = buffer + buffer_size;
s->opaque = opaque;
s->write_packet = write_packet;
s->write_seek = write_seek;
s->pos = 0;
return 0;
}
static void flush_buffer(PutByteContext *s)
{
if (s->buf_ptr > s->buffer) {
if (s->write_packet)
s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
s->pos += s->buf_ptr - s->buffer;
}
s->buf_ptr = s->buffer;
}
void put_byte(PutByteContext *s, int b)
{
*(s->buf_ptr)++ = b;
if (s->buf_ptr >= s->buf_end)
flush_buffer(s);
}
void put_buffer(PutByteContext *s, unsigned char *buf, int size)
{
int len;
while (size > 0) {
len = (s->buf_end - s->buf_ptr);
if (len > size)
len = size;
memcpy(s->buf_ptr, buf, len);
s->buf_ptr += len;
if (s->buf_ptr >= s->buf_end)
flush_buffer(s);
buf += len;
size -= len;
}
}
void put_flush_packet(PutByteContext *s)
{
flush_buffer(s);
}
/* XXX: this seek is not correct if we go after the end of the written data */
long long put_seek(PutByteContext *s, long long offset, int whence)
{
long long offset1;
if (whence != SEEK_CUR && whence != SEEK_SET)
return -1;
if (whence == SEEK_CUR)
offset += s->pos + s->buf_ptr - s->buffer;
offset1 = offset - s->pos;
if (offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
/* can do the seek inside the buffer */
s->buf_ptr = s->buffer + offset1;
} else {
if (!s->write_seek)
return -1;
flush_buffer(s);
s->write_seek(s->opaque, offset, whence);
}
return offset;
}
long long put_pos(PutByteContext *s)
{
return put_seek(s, 0, SEEK_CUR);
}
void put_le32(PutByteContext *s, unsigned int val)
{
put_byte(s, val);
put_byte(s, val >> 8);
put_byte(s, val >> 16);
put_byte(s, val >> 24);
}
void put_le64(PutByteContext *s, unsigned long long val)
{
put_le32(s, val & 0xffffffff);
put_le32(s, val >> 32);
}
void put_le16(PutByteContext *s, unsigned int val)
{
put_byte(s, val);
put_byte(s, val >> 8);
}
void put_tag(PutByteContext *s, char *tag)
{
while (*tag) {
put_byte(s, *tag++);
}
}
|