graph2dot: Use the fallback getopt implementation if needed
[libav.git] / tools / pktdumper.c
CommitLineData
15ea54d5
DB
1/*
2 * Copyright (c) 2005 Francois Revol
3 *
2912e87a 4 * This file is part of Libav.
15ea54d5 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
15ea54d5
DB
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
15ea54d5
DB
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
2912e87a 17 * License along with Libav; if not, write to the Free Software
15ea54d5
DB
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
212ec5fa 21#include "config.h"
0fa04b7f
FR
22#include <limits.h>
23#include <fcntl.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
212ec5fa 27#if HAVE_UNISTD_H
0fa04b7f 28#include <unistd.h>
212ec5fa
MS
29#endif
30#if HAVE_IO_H
31#include <io.h>
32#endif
4e81b5f5 33
896bb0d7 34#include "libavutil/time.h"
3ef61825 35#include "libavformat/avformat.h"
0fa04b7f 36
4e81b5f5 37#define PKTFILESUFF "_%08" PRId64 "_%02d_%010" PRId64 "_%06d_%c.bin"
0fa04b7f
FR
38
39static int usage(int ret)
40{
15b34f63
FR
41 fprintf(stderr, "dump (up to maxpkts) AVPackets as they are demuxed by libavformat.\n");
42 fprintf(stderr, "each packet is dumped in its own file named like `basename file.ext`_$PKTNUM_$STREAMINDEX_$STAMP_$SIZE_$FLAGS.bin\n");
2d216336
FR
43 fprintf(stderr, "pktdumper [-nw] file [maxpkts]\n");
44 fprintf(stderr, "-n\twrite No file at all, only demux.\n");
45 fprintf(stderr, "-w\tWait at end of processing instead of quitting.\n");
15b34f63 46 return ret;
0fa04b7f
FR
47}
48
49int main(int argc, char **argv)
50{
15b34f63
FR
51 char fntemplate[PATH_MAX];
52 char pktfilename[PATH_MAX];
d830264a 53 AVFormatContext *fctx = NULL;
15b34f63 54 AVPacket pkt;
4e81b5f5 55 int64_t pktnum = 0;
15b34f63 56 int64_t maxpkts = 0;
4e81b5f5
DB
57 int donotquit = 0;
58 int nowrite = 0;
15b34f63 59 int err;
115329f1 60
2d216336
FR
61 if ((argc > 1) && !strncmp(argv[1], "-", 1)) {
62 if (strchr(argv[1], 'w'))
755bfeab 63 donotquit = 1;
2d216336
FR
64 if (strchr(argv[1], 'n'))
65 nowrite = 1;
66 argv++;
67 argc--;
68 }
15b34f63
FR
69 if (argc < 2)
70 return usage(1);
71 if (argc > 2)
72 maxpkts = atoi(argv[2]);
4e81b5f5 73 strncpy(fntemplate, argv[1], PATH_MAX - 1);
15b34f63 74 if (strrchr(argv[1], '/'))
4e81b5f5 75 strncpy(fntemplate, strrchr(argv[1], '/') + 1, PATH_MAX - 1);
15b34f63
FR
76 if (strrchr(fntemplate, '.'))
77 *strrchr(fntemplate, '.') = '\0';
78 if (strchr(fntemplate, '%')) {
79 fprintf(stderr, "can't use filenames containing '%%'\n");
80 return usage(1);
81 }
4e81b5f5 82 if (strlen(fntemplate) + sizeof(PKTFILESUFF) >= PATH_MAX - 1) {
15b34f63
FR
83 fprintf(stderr, "filename too long\n");
84 return usage(1);
85 }
86 strcat(fntemplate, PKTFILESUFF);
87 printf("FNTEMPLATE: '%s'\n", fntemplate);
115329f1 88
15b34f63
FR
89 // register all file formats
90 av_register_all();
115329f1 91
d830264a 92 err = avformat_open_input(&fctx, argv[1], NULL, NULL);
15b34f63 93 if (err < 0) {
d830264a 94 fprintf(stderr, "cannot open input: error %d\n", err);
15b34f63
FR
95 return 1;
96 }
115329f1 97
c960e67a 98 err = avformat_find_stream_info(fctx, NULL);
15b34f63 99 if (err < 0) {
c960e67a 100 fprintf(stderr, "avformat_find_stream_info: error %d\n", err);
15b34f63
FR
101 return 1;
102 }
115329f1 103
15b34f63 104 av_init_packet(&pkt);
115329f1 105
15b34f63
FR
106 while ((err = av_read_frame(fctx, &pkt)) >= 0) {
107 int fd;
4e81b5f5
DB
108 snprintf(pktfilename, PATH_MAX - 1, fntemplate, pktnum,
109 pkt.stream_index, pkt.pts, pkt.size,
110 (pkt.flags & AV_PKT_FLAG_KEY) ? 'K' : '_');
111 printf(PKTFILESUFF "\n", pktnum, pkt.stream_index, pkt.pts, pkt.size,
112 (pkt.flags & AV_PKT_FLAG_KEY) ? 'K' : '_');
15b34f63 113 //printf("open(\"%s\")\n", pktfilename);
2d216336 114 if (!nowrite) {
4e81b5f5 115 fd = open(pktfilename, O_WRONLY | O_CREAT, 0644);
d39facc7
DB
116 err = write(fd, pkt.data, pkt.size);
117 if (err < 0) {
118 fprintf(stderr, "write: error %d\n", err);
119 return 1;
120 }
2d216336
FR
121 close(fd);
122 }
92147b6b 123 av_free_packet(&pkt);
15b34f63
FR
124 pktnum++;
125 if (maxpkts && (pktnum >= maxpkts))
126 break;
127 }
115329f1 128
cd3716b9 129 avformat_close_input(&fctx);
92147b6b 130
755bfeab 131 while (donotquit)
896bb0d7 132 av_usleep(60 * 1000000);
115329f1 133
15b34f63 134 return 0;
0fa04b7f 135}