Repository navigation
Expand file tree
/
Copy pathlibavmux.c
More file actions
396 lines (278 loc) · 9.9 KB
/
Copy pathlibavmux.c
File metadata and controls
396 lines (278 loc) · 9.9 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// libavmux
// adapted from
// avcodec_sample.0.4.9.cpp
// A small sample program that shows how to use libavformat and libavcodec to
// read video from a file.
//
// This version is for the 0.4.9-pre1 release of ffmpeg. This release adds the
// av_read_frame() API call, which simplifies the reading of video frames
// considerably.
//
//gcc -O3 -I/usr/local/include/ffmpeg -I/sw/include/mjpegtools -L/sw/lib -lavcodec -lavformat -lavutil -lmjpegutils libavmux.c -o libavmux
//
/*
**<h3>Elementary stream to Container MUXER/DEMUXER</h3>
**<p>This is a very simple avi/mov/mpg demuxer and muxer.</P>
**<p>The demuxer works quite well, however the muxing part still has bugs and
**has not correctly produced a working file. This is still a work in progress.
**</p>
*/
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
static void print_usage()
{
fprintf (stderr,
"usage: libavmux <filename1> [<filenameN> -o <output file>] [-F <format>] [-h]\n"
"Muxes or demuxes video files to/from elementary streams\n"
"Single input file will demux into streams\n"
"Multiple files mux into output file specified by -o\n"
"-F specifies the container format (avi, mov, asf, mpegps...)\n"
"\n"
);
}
/* libav demuxer
* expects an array of open files, one per stream
*/
int mux_files (char *outname, char **inname, int len, char *format) {
int i;
AVFormatContext **pFormatCtx;
AVOutputFormat *fmt;
AVFormatContext *oc;
AVStream *audio_st, *video_st;
AVPacket packet;
double audio_pts, video_pts;
int audio,video;
int read_bytes=0;
unsigned int audio_bytes=0;
unsigned int video_bytes=0;
if (format) {
fmt = guess_format(format, NULL, NULL);
} else {
fmt = guess_format(NULL, outname, NULL);
}
if (!fmt) {
fprintf(stderr, "Could not find suitable output format\n");
return -1;
}
oc = av_alloc_format_context();
if (!oc) {
fprintf(stderr, "couldn't allocate memory for output format\n");
return -1;
}
oc->oformat = fmt;
snprintf(oc->filename, sizeof(oc->filename), "%s", outname);
//fprintf (stderr,"list len: %d\n",len);
pFormatCtx = (AVFormatContext **) malloc (sizeof(AVFormatContext *) * len);
// TODO: Need to re order the streams so that the video is added first
// otherwise crash ensues...
for (i =1; i < len; i++) {
//fprintf (stderr,"file %d: %s\n",i,inname[i]);
if(av_open_input_file(&pFormatCtx[i], inname[i], NULL, 0, NULL)!=0)
return -1;
if(av_find_stream_info(pFormatCtx[i])<0)
return -1; // Couldn't find stream information
dump_format(pFormatCtx[i], i, inname[i], 0);
if(pFormatCtx[i]->streams[0]->codec->codec_type==CODEC_TYPE_VIDEO)
{
fprintf (stderr,"Video FR: %d/%d\n",pFormatCtx[i]->streams[0]->codec ->time_base.num , pFormatCtx[i]->streams[0]->codec ->time_base.den);
video_st = av_new_stream(oc,0);
if (!video_st) {
fprintf(stderr, "Could not alloc stream\n");
return -1;
}
video = i;
video_st->codec = pFormatCtx[i]->streams[0]->codec;
video_st->codec->time_base.num = 1; // pFormatCtx[i]->streams[0]->codec ->time_base.num;
video_st->codec->time_base.den = 25; // pFormatCtx[i]->streams[0]->codec ->time_base.den;
// video_st->codec->codec_id = pFormatCtx[i]->streams[0]->codec->codec_id;
fprintf (stderr,"Video codec FR %d/%d\n", video_st->codec->time_base.num , video_st->codec->time_base.den);
// if(!strcmp(oc->oformat->name, "mp4") || !strcmp(oc->oformat->name, "mov") || !strcmp(oc->oformat->name, "3gp"))
if(oc->oformat->flags & AVFMT_GLOBALHEADER)
video_st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
// fprintf (stderr,"video codec: %s\n",oc->streams[0]->codec->codec->name);
fprintf (stderr,"video format: %s\n",pFormatCtx[i]->iformat->name);
} else if (pFormatCtx[i]->streams[0]->codec->codec_type==CODEC_TYPE_AUDIO) {
audio_st = av_new_stream(oc,1);
// audio_st = add_audio_stream(oc,pFormatCtx[i]->streams[0]->codec);
if (!audio_st) {
fprintf(stderr, "Could not alloc stream\n");
return -1;
}
audio=i;
audio_st->codec = pFormatCtx[i]->streams[0]->codec;
// fprintf (stderr,"audio codec: %s\n",oc->streams[1]->codec->codec->name);
fprintf (stderr,"audio format: %s\n",pFormatCtx[i]->iformat->name);
}
}
// Need to set correct frame rate
if (av_set_parameters(oc, NULL) < 0) {
fprintf(stderr, "Invalid output format parameters\n");
return -1;
}
dump_format(oc, 0, outname, 1);
if (!(fmt->flags & AVFMT_NOFILE)) {
if (url_fopen(&oc->pb, outname, URL_WRONLY) < 0) {
fprintf(stderr, "Could not open '%s'\n", outname);
return -1;
}
}
fprintf (stderr,"Video file: %d Audio File: %d\n",video,audio);
av_write_header(oc);
//fprintf (stderr,"while...\n");
/* need to re-engineer this part of the muxing code */
while(read_bytes>=0) {
/* compute current audio and video time */
if (audio_st)
audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
else
audio_pts = 0.0;
if (video_st)
video_pts = (double)video_st->pts.val * video_st->codec->time_base.num / video_st->codec->time_base.den;
else
video_pts = 0.0;
/*
if ((!audio_st || audio_pts >= STREAM_DURATION) &&
(!video_st || video_pts >= STREAM_DURATION))
break;
*/
// fprintf (stderr,"audio or video...\n");
//fprintf (stderr,"PTS A: %d V: %d \n",audio_st->pts.val,video_st->pts.val);
/* write interleaved audio and video frames */
if (!video_st || (video_st && audio_st && audio_pts < video_pts)) {
// write_audio_frame(oc, audio_st);
//fprintf (stderr,"read audio...\n");
read_bytes=av_read_frame(pFormatCtx[audio], &packet);
audio_bytes += packet.size;
packet.stream_index=1;
// fprintf (stderr,"OUT A: PTS %d DTS %d\n",packet.pts,packet.dts);
// packet.pts = audio_st->pts.val;
// packet.dts = audio_st->cur_dts;
av_interleaved_write_frame(oc, &packet);
// av_write_frame(oc, &packet);
av_free_packet(&packet);
} else {
// write_video_frame(oc, video_st);
//fprintf (stderr,"read video...\n");
read_bytes=av_read_frame(pFormatCtx[video], &packet);
video_bytes+=packet.size;
packet.stream_index=0;
// fprintf (stderr,"OUT V: PTS %d DTS %d\n",packet.pts,packet.dts);
// packet.pts = video_st->pts.val;
// packet.dts = video_st->cur_dts;
//fprintf (stderr,"write video...\n");
// a
// packet.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
//av_write_frame(oc, &packet);
av_interleaved_write_frame(oc, &packet);
av_free_packet(&packet);
}
}
// post calculate the bitrate...
//fprintf (stderr,"PTS A: %g %u V: %g %u\n",audio_pts,audio_bytes,video_pts,video_bytes);
av_write_trailer(oc);
for(i = 0; i < oc->nb_streams; i++) {
av_freep(&oc->streams[i]->codec);
av_freep(&oc->streams[i]);
}
if (!(fmt->flags & AVFMT_NOFILE)) {
/* close the output file */
url_fclose(oc->pb);
}
av_free(oc);
return 0;
}
int demux_file (char *filen)
{
AVFormatContext *pFormatCtx;
int *stream_fh;
char *tname;
AVPacket packet;
int i;
// Open video file
if(av_open_input_file(&pFormatCtx, filen, NULL, 0, NULL)!=0)
return -1; // Couldn't open file
// Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0)
return -1; // Couldn't find stream information
// Dump information about file onto standard error
dump_format(pFormatCtx, 0, filen, 0);
stream_fh = (int *)malloc(pFormatCtx->nb_streams * sizeof(int));
tname = (char *) malloc(strlen(filen)+10); // 10 characters should be enough???
printf ("Number streams: %d\n",pFormatCtx->nb_streams);
for(i=0; i<pFormatCtx->nb_streams; i++) {
//fprintf (stderr,"stream: %d = %d (%s)\n",i,pFormatCtx->streams[i]->codec->codec_type ,pFormatCtx->streams[i]->codec->codec_name);
sprintf (tname,"%s.%d",filen,pFormatCtx->streams[i]->codec->codec_id); // buffer overflow might occur. Please help!
stream_fh[i] = open (tname,O_WRONLY|O_CREAT);
fchmod(stream_fh[i], 0644);
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
{
printf ("Stream %d video %d\n",i,pFormatCtx->streams[i]->codec->codec_id);
} else {
printf ("Stream %d audio %d\n",i,pFormatCtx->streams[i]->codec->codec_id);
}
}
free(tname);
// Loop until nothing read
while(av_read_frame(pFormatCtx, &packet)>=0)
{
// Is this a packet from the video stream?
write(stream_fh[packet.stream_index],packet.data,packet.size);
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}
free(stream_fh);
// Close the video file
av_close_input_file(pFormatCtx);
return 0;
}
#define MODE_DEMUX 0
#define MODE_MUX 1
int main(int argc, char *argv[])
{
int i;
int mode = MODE_DEMUX;
char *containername = NULL;
char *outname;
// Parse Args
const static char *legal_flags = "F:o:h?";
while ((i = getopt (argc, argv, legal_flags)) != -1) {
fprintf (stderr,"checking opt: %c\n",i);
switch (i) {
case 'o':
outname = (char *) malloc (strlen(optarg)+1);
strcpy(outname,optarg);
mode = MODE_MUX;
break;
case 'F':
containername = (char *) malloc (strlen(optarg)+1);
strcpy(containername,optarg);
break;
case 'h':
case '?':
default:
print_usage (argv);
return 0 ;
break;
}
}
argc -= --optind;
argv += optind;
if ((argc != 2 && mode==MODE_DEMUX) || (argc<2 && mode==MODE_MUX)) {
fprintf (stderr,"argc = %d mode = %d\n",argc,mode);
print_usage (argv);
return 0 ;
}
// Register all formats and codecs
av_register_all();
if (mode == MODE_DEMUX) {
demux_file(argv[1]);
}
if (mode == MODE_MUX) {
mux_files(outname, argv, argc, containername);
}
}