yading@11
|
1 /*
|
yading@11
|
2 * Copyright (c) 2003 Fabrice Bellard
|
yading@11
|
3 * Copyright (c) 2007 Michael Niedermayer
|
yading@11
|
4 *
|
yading@11
|
5 * This file is part of FFmpeg.
|
yading@11
|
6 *
|
yading@11
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
9 * License as published by the Free Software Foundation; either
|
yading@11
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
11 *
|
yading@11
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
15 * Lesser General Public License for more details.
|
yading@11
|
16 *
|
yading@11
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
20 */
|
yading@11
|
21
|
yading@11
|
22 #include <stdint.h>
|
yading@11
|
23 #include <stdlib.h>
|
yading@11
|
24 #include <stdio.h>
|
yading@11
|
25 #include <string.h>
|
yading@11
|
26
|
yading@11
|
27 #include "libavutil/common.h"
|
yading@11
|
28 #include "libavutil/mathematics.h"
|
yading@11
|
29 #include "libavformat/avformat.h"
|
yading@11
|
30
|
yading@11
|
31 static char buffer[20];
|
yading@11
|
32
|
yading@11
|
33 static const char *ret_str(int v)
|
yading@11
|
34 {
|
yading@11
|
35 switch (v) {
|
yading@11
|
36 case AVERROR_EOF: return "-EOF";
|
yading@11
|
37 case AVERROR(EIO): return "-EIO";
|
yading@11
|
38 case AVERROR(ENOMEM): return "-ENOMEM";
|
yading@11
|
39 case AVERROR(EINVAL): return "-EINVAL";
|
yading@11
|
40 default:
|
yading@11
|
41 snprintf(buffer, sizeof(buffer), "%2d", v);
|
yading@11
|
42 return buffer;
|
yading@11
|
43 }
|
yading@11
|
44 }
|
yading@11
|
45
|
yading@11
|
46 static void ts_str(char buffer[60], int64_t ts, AVRational base)
|
yading@11
|
47 {
|
yading@11
|
48 if (ts == AV_NOPTS_VALUE) {
|
yading@11
|
49 strcpy(buffer, " NOPTS ");
|
yading@11
|
50 return;
|
yading@11
|
51 }
|
yading@11
|
52 ts= av_rescale_q(ts, base, (AVRational){1, 1000000});
|
yading@11
|
53 snprintf(buffer, 60, "%c%"PRId64".%06"PRId64"", ts<0 ? '-' : ' ', FFABS(ts)/1000000, FFABS(ts)%1000000);
|
yading@11
|
54 }
|
yading@11
|
55
|
yading@11
|
56 int main(int argc, char **argv)
|
yading@11
|
57 {
|
yading@11
|
58 const char *filename;
|
yading@11
|
59 AVFormatContext *ic = NULL;
|
yading@11
|
60 int i, ret, stream_id;
|
yading@11
|
61 int j;
|
yading@11
|
62 int64_t timestamp;
|
yading@11
|
63 AVDictionary *format_opts = NULL;
|
yading@11
|
64 int64_t seekfirst = AV_NOPTS_VALUE;
|
yading@11
|
65 int firstback=0;
|
yading@11
|
66 int frame_count = 1;
|
yading@11
|
67
|
yading@11
|
68 for(i=2; i<argc; i+=2){
|
yading@11
|
69 if (!strcmp(argv[i], "-seekforw")){
|
yading@11
|
70 seekfirst = atoi(argv[i+1]);
|
yading@11
|
71 } else if(!strcmp(argv[i], "-seekback")){
|
yading@11
|
72 seekfirst = atoi(argv[i+1]);
|
yading@11
|
73 firstback = 1;
|
yading@11
|
74 } else if(!strcmp(argv[i], "-frames")){
|
yading@11
|
75 frame_count = atoi(argv[i+1]);
|
yading@11
|
76 } else {
|
yading@11
|
77 argc = 1;
|
yading@11
|
78 }
|
yading@11
|
79 }
|
yading@11
|
80
|
yading@11
|
81 av_dict_set(&format_opts, "channels", "1", 0);
|
yading@11
|
82 av_dict_set(&format_opts, "sample_rate", "22050", 0);
|
yading@11
|
83
|
yading@11
|
84 /* initialize libavcodec, and register all codecs and formats */
|
yading@11
|
85 av_register_all();
|
yading@11
|
86
|
yading@11
|
87 if (argc < 2) {
|
yading@11
|
88 printf("usage: %s input_file\n"
|
yading@11
|
89 "\n", argv[0]);
|
yading@11
|
90 return 1;
|
yading@11
|
91 }
|
yading@11
|
92
|
yading@11
|
93 filename = argv[1];
|
yading@11
|
94
|
yading@11
|
95 ret = avformat_open_input(&ic, filename, NULL, &format_opts);
|
yading@11
|
96 av_dict_free(&format_opts);
|
yading@11
|
97 if (ret < 0) {
|
yading@11
|
98 fprintf(stderr, "cannot open %s\n", filename);
|
yading@11
|
99 return 1;
|
yading@11
|
100 }
|
yading@11
|
101
|
yading@11
|
102 ret = avformat_find_stream_info(ic, NULL);
|
yading@11
|
103 if (ret < 0) {
|
yading@11
|
104 fprintf(stderr, "%s: could not find codec parameters\n", filename);
|
yading@11
|
105 return 1;
|
yading@11
|
106 }
|
yading@11
|
107
|
yading@11
|
108 if(seekfirst != AV_NOPTS_VALUE){
|
yading@11
|
109 if(firstback) avformat_seek_file(ic, -1, INT64_MIN, seekfirst, seekfirst, 0);
|
yading@11
|
110 else avformat_seek_file(ic, -1, seekfirst, seekfirst, INT64_MAX, 0);
|
yading@11
|
111 }
|
yading@11
|
112 for(i=0; ; i++){
|
yading@11
|
113 AVPacket pkt = { 0 };
|
yading@11
|
114 AVStream *av_uninit(st);
|
yading@11
|
115 char ts_buf[60];
|
yading@11
|
116
|
yading@11
|
117 if(ret>=0){
|
yading@11
|
118 for(j=0; j<frame_count; j++) {
|
yading@11
|
119 ret= av_read_frame(ic, &pkt);
|
yading@11
|
120 if(ret>=0){
|
yading@11
|
121 char dts_buf[60];
|
yading@11
|
122 st= ic->streams[pkt.stream_index];
|
yading@11
|
123 ts_str(dts_buf, pkt.dts, st->time_base);
|
yading@11
|
124 ts_str(ts_buf, pkt.pts, st->time_base);
|
yading@11
|
125 printf("ret:%-10s st:%2d flags:%d dts:%s pts:%s pos:%7" PRId64 " size:%6d", ret_str(ret), pkt.stream_index, pkt.flags, dts_buf, ts_buf, pkt.pos, pkt.size);
|
yading@11
|
126 av_free_packet(&pkt);
|
yading@11
|
127 } else
|
yading@11
|
128 printf("ret:%s", ret_str(ret)); // necessary to avoid trailing whitespace
|
yading@11
|
129 printf("\n");
|
yading@11
|
130 }
|
yading@11
|
131 }
|
yading@11
|
132
|
yading@11
|
133 if(i>25) break;
|
yading@11
|
134
|
yading@11
|
135 stream_id= (i>>1)%(ic->nb_streams+1) - 1;
|
yading@11
|
136 timestamp= (i*19362894167LL) % (4*AV_TIME_BASE) - AV_TIME_BASE;
|
yading@11
|
137 if(stream_id>=0){
|
yading@11
|
138 st= ic->streams[stream_id];
|
yading@11
|
139 timestamp= av_rescale_q(timestamp, AV_TIME_BASE_Q, st->time_base);
|
yading@11
|
140 }
|
yading@11
|
141 //FIXME fully test the new seek API
|
yading@11
|
142 if(i&1) ret = avformat_seek_file(ic, stream_id, INT64_MIN, timestamp, timestamp, 0);
|
yading@11
|
143 else ret = avformat_seek_file(ic, stream_id, timestamp, timestamp, INT64_MAX, 0);
|
yading@11
|
144 ts_str(ts_buf, timestamp, stream_id < 0 ? AV_TIME_BASE_Q : st->time_base);
|
yading@11
|
145 printf("ret:%-10s st:%2d flags:%d ts:%s\n", ret_str(ret), stream_id, i&1, ts_buf);
|
yading@11
|
146 }
|
yading@11
|
147
|
yading@11
|
148 avformat_close_input(&ic);
|
yading@11
|
149
|
yading@11
|
150 return 0;
|
yading@11
|
151 }
|