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