00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdint.h>
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <string.h>
00026
00027 #include "libavutil/common.h"
00028 #include "libavutil/mathematics.h"
00029 #include "libavformat/avformat.h"
00030
00031 #undef printf
00032 #undef fprintf
00033
00034 static char buffer[20];
00035
00036 static const char *ret_str(int v)
00037 {
00038 switch (v) {
00039 case AVERROR_EOF: return "-EOF";
00040 case AVERROR(EIO): return "-EIO";
00041 case AVERROR(ENOMEM): return "-ENOMEM";
00042 case AVERROR(EINVAL): return "-EINVAL";
00043 default:
00044 snprintf(buffer, sizeof(buffer), "%2d", v);
00045 return buffer;
00046 }
00047 }
00048
00049 static void ts_str(char buffer[60], int64_t ts, AVRational base)
00050 {
00051 if (ts == AV_NOPTS_VALUE) {
00052 strcpy(buffer, " NOPTS ");
00053 return;
00054 }
00055 ts= av_rescale_q(ts, base, (AVRational){1, 1000000});
00056 snprintf(buffer, 60, "%c%"PRId64".%06"PRId64"", ts<0 ? '-' : ' ', FFABS(ts)/1000000, FFABS(ts)%1000000);
00057 }
00058
00059 int main(int argc, char **argv)
00060 {
00061 const char *filename;
00062 AVFormatContext *ic = NULL;
00063 int i, ret, stream_id;
00064 int j;
00065 int64_t timestamp;
00066 AVDictionary *format_opts = NULL;
00067 int64_t seekfirst = AV_NOPTS_VALUE;
00068 int firstback=0;
00069 int frame_count = 1;
00070
00071 for(i=2; i<argc; i+=2){
00072 if (!strcmp(argv[i], "-seekforw")){
00073 seekfirst = atoi(argv[i+1]);
00074 } else if(!strcmp(argv[i], "-seekback")){
00075 seekfirst = atoi(argv[i+1]);
00076 firstback = 1;
00077 } else if(!strcmp(argv[i], "-frames")){
00078 frame_count = atoi(argv[i+1]);
00079 } else {
00080 argc = 1;
00081 }
00082 }
00083
00084 av_dict_set(&format_opts, "channels", "1", 0);
00085 av_dict_set(&format_opts, "sample_rate", "22050", 0);
00086
00087
00088 av_register_all();
00089
00090 if (argc < 2) {
00091 printf("usage: %s input_file\n"
00092 "\n", argv[0]);
00093 return 1;
00094 }
00095
00096 filename = argv[1];
00097
00098 ret = avformat_open_input(&ic, filename, NULL, &format_opts);
00099 av_dict_free(&format_opts);
00100 if (ret < 0) {
00101 fprintf(stderr, "cannot open %s\n", filename);
00102 return 1;
00103 }
00104
00105 ret = avformat_find_stream_info(ic, NULL);
00106 if (ret < 0) {
00107 fprintf(stderr, "%s: could not find codec parameters\n", filename);
00108 return 1;
00109 }
00110
00111 if(seekfirst != AV_NOPTS_VALUE){
00112 if(firstback) avformat_seek_file(ic, -1, INT64_MIN, seekfirst, seekfirst, 0);
00113 else avformat_seek_file(ic, -1, seekfirst, seekfirst, INT64_MAX, 0);
00114 }
00115 for(i=0; ; i++){
00116 AVPacket pkt = { 0 };
00117 AVStream *av_uninit(st);
00118 char ts_buf[60];
00119
00120 if(ret>=0){
00121 for(j=0; j<frame_count; j++) {
00122 ret= av_read_frame(ic, &pkt);
00123 if(ret>=0){
00124 char dts_buf[60];
00125 st= ic->streams[pkt.stream_index];
00126 ts_str(dts_buf, pkt.dts, st->time_base);
00127 ts_str(ts_buf, pkt.pts, st->time_base);
00128 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);
00129 av_free_packet(&pkt);
00130 } else
00131 printf("ret:%s", ret_str(ret));
00132 printf("\n");
00133 }
00134 }
00135
00136 if(i>25) break;
00137
00138 stream_id= (i>>1)%(ic->nb_streams+1) - 1;
00139 timestamp= (i*19362894167LL) % (4*AV_TIME_BASE) - AV_TIME_BASE;
00140 if(stream_id>=0){
00141 st= ic->streams[stream_id];
00142 timestamp= av_rescale_q(timestamp, AV_TIME_BASE_Q, st->time_base);
00143 }
00144
00145 if(i&1) ret = avformat_seek_file(ic, stream_id, INT64_MIN, timestamp, timestamp, 0);
00146 else ret = avformat_seek_file(ic, stream_id, timestamp, timestamp, INT64_MAX, 0);
00147 ts_str(ts_buf, timestamp, stream_id < 0 ? AV_TIME_BASE_Q : st->time_base);
00148 printf("ret:%-10s st:%2d flags:%d ts:%s\n", ret_str(ret), stream_id, i&1, ts_buf);
00149 }
00150
00151 avformat_close_input(&ic);
00152
00153 return 0;
00154 }