tools: K&R reformatting cosmetics
[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
0fa04b7f
FR
21#include <limits.h>
22#include <fcntl.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
4e81b5f5 27
3ef61825 28#include "libavformat/avformat.h"
0fa04b7f 29
4e81b5f5 30#define PKTFILESUFF "_%08" PRId64 "_%02d_%010" PRId64 "_%06d_%c.bin"
0fa04b7f 31
6878026a
DB
32#undef strcat
33
0fa04b7f
FR
34static int usage(int ret)
35{
15b34f63
FR
36 fprintf(stderr, "dump (up to maxpkts) AVPackets as they are demuxed by libavformat.\n");
37 fprintf(stderr, "each packet is dumped in its own file named like `basename file.ext`_$PKTNUM_$STREAMINDEX_$STAMP_$SIZE_$FLAGS.bin\n");
2d216336
FR
38 fprintf(stderr, "pktdumper [-nw] file [maxpkts]\n");
39 fprintf(stderr, "-n\twrite No file at all, only demux.\n");
40 fprintf(stderr, "-w\tWait at end of processing instead of quitting.\n");
15b34f63 41 return ret;
0fa04b7f
FR
42}
43
44int main(int argc, char **argv)
45{
15b34f63
FR
46 char fntemplate[PATH_MAX];
47 char pktfilename[PATH_MAX];
d830264a 48 AVFormatContext *fctx = NULL;
15b34f63 49 AVPacket pkt;
4e81b5f5 50 int64_t pktnum = 0;
15b34f63 51 int64_t maxpkts = 0;
4e81b5f5
DB
52 int donotquit = 0;
53 int nowrite = 0;
15b34f63 54 int err;
115329f1 55
2d216336
FR
56 if ((argc > 1) && !strncmp(argv[1], "-", 1)) {
57 if (strchr(argv[1], 'w'))
755bfeab 58 donotquit = 1;
2d216336
FR
59 if (strchr(argv[1], 'n'))
60 nowrite = 1;
61 argv++;
62 argc--;
63 }
15b34f63
FR
64 if (argc < 2)
65 return usage(1);
66 if (argc > 2)
67 maxpkts = atoi(argv[2]);
4e81b5f5 68 strncpy(fntemplate, argv[1], PATH_MAX - 1);
15b34f63 69 if (strrchr(argv[1], '/'))
4e81b5f5 70 strncpy(fntemplate, strrchr(argv[1], '/') + 1, PATH_MAX - 1);
15b34f63
FR
71 if (strrchr(fntemplate, '.'))
72 *strrchr(fntemplate, '.') = '\0';
73 if (strchr(fntemplate, '%')) {
74 fprintf(stderr, "can't use filenames containing '%%'\n");
75 return usage(1);
76 }
4e81b5f5 77 if (strlen(fntemplate) + sizeof(PKTFILESUFF) >= PATH_MAX - 1) {
15b34f63
FR
78 fprintf(stderr, "filename too long\n");
79 return usage(1);
80 }
81 strcat(fntemplate, PKTFILESUFF);
82 printf("FNTEMPLATE: '%s'\n", fntemplate);
115329f1 83
15b34f63
FR
84 // register all file formats
85 av_register_all();
115329f1 86
d830264a 87 err = avformat_open_input(&fctx, argv[1], NULL, NULL);
15b34f63 88 if (err < 0) {
d830264a 89 fprintf(stderr, "cannot open input: error %d\n", err);
15b34f63
FR
90 return 1;
91 }
115329f1 92
c960e67a 93 err = avformat_find_stream_info(fctx, NULL);
15b34f63 94 if (err < 0) {
c960e67a 95 fprintf(stderr, "avformat_find_stream_info: error %d\n", err);
15b34f63
FR
96 return 1;
97 }
115329f1 98
15b34f63 99 av_init_packet(&pkt);
115329f1 100
15b34f63
FR
101 while ((err = av_read_frame(fctx, &pkt)) >= 0) {
102 int fd;
4e81b5f5
DB
103 snprintf(pktfilename, PATH_MAX - 1, fntemplate, pktnum,
104 pkt.stream_index, pkt.pts, pkt.size,
105 (pkt.flags & AV_PKT_FLAG_KEY) ? 'K' : '_');
106 printf(PKTFILESUFF "\n", pktnum, pkt.stream_index, pkt.pts, pkt.size,
107 (pkt.flags & AV_PKT_FLAG_KEY) ? 'K' : '_');
15b34f63 108 //printf("open(\"%s\")\n", pktfilename);
2d216336 109 if (!nowrite) {
4e81b5f5 110 fd = open(pktfilename, O_WRONLY | O_CREAT, 0644);
d39facc7
DB
111 err = write(fd, pkt.data, pkt.size);
112 if (err < 0) {
113 fprintf(stderr, "write: error %d\n", err);
114 return 1;
115 }
2d216336
FR
116 close(fd);
117 }
92147b6b 118 av_free_packet(&pkt);
15b34f63
FR
119 pktnum++;
120 if (maxpkts && (pktnum >= maxpkts))
121 break;
122 }
115329f1 123
cd3716b9 124 avformat_close_input(&fctx);
92147b6b 125
755bfeab 126 while (donotquit)
7072a6a4 127 usleep(60 * 1000000);
115329f1 128
15b34f63 129 return 0;
0fa04b7f 130}