• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/seek-test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2003 Fabrice Bellard
00003  * Copyright (c) 2007 Michael Niedermayer
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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     double tsval;
00052     if (ts == AV_NOPTS_VALUE) {
00053         strcpy(buffer, " NOPTS   ");
00054         return;
00055     }
00056     tsval = ts * av_q2d(base);
00057     snprintf(buffer, 60, "%9f", tsval);
00058 }
00059 
00060 int main(int argc, char **argv)
00061 {
00062     const char *filename;
00063     AVFormatContext *ic = NULL;
00064     int i, ret, stream_id;
00065     int64_t timestamp;
00066     AVDictionary *format_opts = NULL;
00067 
00068     av_dict_set(&format_opts, "channels", "1", 0);
00069     av_dict_set(&format_opts, "sample_rate", "22050", 0);
00070 
00071     /* initialize libavcodec, and register all codecs and formats */
00072     av_register_all();
00073 
00074     if (argc != 2) {
00075         printf("usage: %s input_file\n"
00076                "\n", argv[0]);
00077         return 1;
00078     }
00079 
00080     filename = argv[1];
00081 
00082     ret = avformat_open_input(&ic, filename, NULL, &format_opts);
00083     av_dict_free(&format_opts);
00084     if (ret < 0) {
00085         fprintf(stderr, "cannot open %s\n", filename);
00086         return 1;
00087     }
00088 
00089     ret = avformat_find_stream_info(ic, NULL);
00090     if (ret < 0) {
00091         fprintf(stderr, "%s: could not find codec parameters\n", filename);
00092         return 1;
00093     }
00094 
00095     for(i=0; ; i++){
00096         AVPacket pkt;
00097         AVStream *av_uninit(st);
00098         char ts_buf[60];
00099 
00100         memset(&pkt, 0, sizeof(pkt));
00101         if(ret>=0){
00102             ret= av_read_frame(ic, &pkt);
00103             if(ret>=0){
00104                 char dts_buf[60];
00105                 st= ic->streams[pkt.stream_index];
00106                 ts_str(dts_buf, pkt.dts, st->time_base);
00107                 ts_str(ts_buf,  pkt.pts, st->time_base);
00108                 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);
00109                 av_free_packet(&pkt);
00110             } else
00111                 printf("ret:%s", ret_str(ret)); // necessary to avoid trailing whitespace
00112             printf("\n");
00113         }
00114 
00115         if(i>25) break;
00116 
00117         stream_id= (i>>1)%(ic->nb_streams+1) - 1;
00118         timestamp= (i*19362894167LL) % (4*AV_TIME_BASE) - AV_TIME_BASE;
00119         if(stream_id>=0){
00120             st= ic->streams[stream_id];
00121             timestamp= av_rescale_q(timestamp, AV_TIME_BASE_Q, st->time_base);
00122         }
00123         //FIXME fully test the new seek API
00124         if(i&1) ret = avformat_seek_file(ic, stream_id, INT64_MIN, timestamp, timestamp, 0);
00125         else    ret = avformat_seek_file(ic, stream_id, timestamp, timestamp, INT64_MAX, 0);
00126         ts_str(ts_buf, timestamp, stream_id < 0 ? AV_TIME_BASE_Q : st->time_base);
00127         printf("ret:%-10s st:%2d flags:%d  ts:%s\n", ret_str(ret), stream_id, i&1, ts_buf);
00128     }
00129 
00130     avformat_close_input(&ic);
00131 
00132     return 0;
00133 }
Generated on Thu Jan 24 2013 17:08:56 for Libav by doxygen 1.7.1