annotate src/ffmpeg/libavformat/utils.c @ 866:d8b8b36d2709 trunk

[svn] - prevent skin list overlap when it's first populated and user quickly switchs to other tabs in the prefswin
author giacomo
date Fri, 16 Mar 2007 18:47:24 -0700
parents dec0488e1344
children d9c7b1d413c9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1 /*
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2 * Various utilities for ffmpeg system
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
4 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
5 * This file is part of FFmpeg.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
6 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
11 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
15 * Lesser General Public License for more details.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
16 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
20 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
21 #include "avformat.h"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
22 #include "allformats.h"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
23 #include "opt.h"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
24
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
25 #undef NDEBUG
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
26 #include <assert.h>
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
27
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
28 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
29 * @file libavformat/utils.c
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
30 * Various utility functions for using ffmpeg library.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
31 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
32
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
33 static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
34 static void av_frac_add(AVFrac *f, int64_t incr);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
35 static void av_frac_set(AVFrac *f, int64_t val);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
36
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
37 /** head of registered input format linked list. */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
38 AVInputFormat *first_iformat = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
39 /** head of registered output format linked list. */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
40 AVOutputFormat *first_oformat = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
41 /** head of registered image format linked list. */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
42 AVImageFormat *first_image_format = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
43
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
44 void av_register_input_format(AVInputFormat *format)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
45 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
46 AVInputFormat **p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
47 p = &first_iformat;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
48 while (*p != NULL) p = &(*p)->next;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
49 *p = format;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
50 format->next = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
51 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
52
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
53 void av_register_output_format(AVOutputFormat *format)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
54 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
55 AVOutputFormat **p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
56 p = &first_oformat;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
57 while (*p != NULL) p = &(*p)->next;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
58 *p = format;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
59 format->next = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
60 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
61
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
62 int match_ext(const char *filename, const char *extensions)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
63 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
64 const char *ext, *p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
65 char ext1[32], *q;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
66
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
67 if(!filename)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
68 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
69
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
70 ext = strrchr(filename, '.');
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
71 if (ext) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
72 ext++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
73 p = extensions;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
74 for(;;) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
75 q = ext1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
76 while (*p != '\0' && *p != ',' && q-ext1<sizeof(ext1)-1)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
77 *q++ = *p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
78 *q = '\0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
79 if (!strcasecmp(ext1, ext))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
80 return 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
81 if (*p == '\0')
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
82 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
83 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
84 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
85 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
86 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
87 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
88
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
89 AVOutputFormat *guess_format(const char *short_name, const char *filename,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
90 const char *mime_type)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
91 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
92 AVOutputFormat *fmt, *fmt_found;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
93 int score_max, score;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
94
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
95 /* specific test for image sequences */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
96 #ifdef CONFIG_IMAGE2_MUXER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
97 if (!short_name && filename &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
98 av_filename_number_test(filename) &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
99 av_guess_image2_codec(filename) != CODEC_ID_NONE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
100 return guess_format("image2", NULL, NULL);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
101 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
102 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
103 if (!short_name && filename &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
104 av_filename_number_test(filename) &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
105 guess_image_format(filename)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
106 return guess_format("image", NULL, NULL);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
107 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
108
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
109 /* find the proper file type */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
110 fmt_found = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
111 score_max = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
112 fmt = first_oformat;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
113 while (fmt != NULL) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
114 score = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
115 if (fmt->name && short_name && !strcmp(fmt->name, short_name))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
116 score += 100;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
117 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
118 score += 10;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
119 if (filename && fmt->extensions &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
120 match_ext(filename, fmt->extensions)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
121 score += 5;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
122 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
123 if (score > score_max) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
124 score_max = score;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
125 fmt_found = fmt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
126 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
127 fmt = fmt->next;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
128 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
129 return fmt_found;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
130 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
131
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
132 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
133 const char *mime_type)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
134 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
135 AVOutputFormat *fmt = guess_format(short_name, filename, mime_type);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
136
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
137 if (fmt) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
138 AVOutputFormat *stream_fmt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
139 char stream_format_name[64];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
140
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
141 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
142 stream_fmt = guess_format(stream_format_name, NULL, NULL);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
143
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
144 if (stream_fmt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
145 fmt = stream_fmt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
146 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
147
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
148 return fmt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
149 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
150
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
151 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
152 * Guesses the codec id based upon muxer and filename.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
153 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
154 enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
155 const char *filename, const char *mime_type, enum CodecType type){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
156 if(type == CODEC_TYPE_VIDEO){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
157 enum CodecID codec_id= CODEC_ID_NONE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
158
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
159 #ifdef CONFIG_IMAGE2_MUXER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
160 if(!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
161 codec_id= av_guess_image2_codec(filename);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
162 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
163 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
164 if(codec_id == CODEC_ID_NONE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
165 codec_id= fmt->video_codec;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
166 return codec_id;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
167 }else if(type == CODEC_TYPE_AUDIO)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
168 return fmt->audio_codec;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
169 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
170 return CODEC_ID_NONE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
171 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
172
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
173 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
174 * finds AVInputFormat based on input format's short name.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
175 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
176 AVInputFormat *av_find_input_format(const char *short_name)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
177 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
178 AVInputFormat *fmt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
179 for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
180 if (!strcmp(fmt->name, short_name))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
181 return fmt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
182 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
183 return NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
184 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
185
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
186 /* memory handling */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
187
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
188 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
189 * Default packet destructor.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
190 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
191 void av_destruct_packet(AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
192 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
193 av_free(pkt->data);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
194 pkt->data = NULL; pkt->size = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
195 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
196
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
197 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
198 * Allocate the payload of a packet and intialized its fields to default values.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
199 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
200 * @param pkt packet
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
201 * @param size wanted payload size
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
202 * @return 0 if OK. AVERROR_xxx otherwise.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
203 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
204 int av_new_packet(AVPacket *pkt, int size)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
205 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
206 void *data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
207 if((unsigned)size > (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
208 return AVERROR_NOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
209 data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
210 if (!data)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
211 return AVERROR_NOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
212 memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
213
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
214 av_init_packet(pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
215 pkt->data = data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
216 pkt->size = size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
217 pkt->destruct = av_destruct_packet;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
218 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
219 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
220
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
221 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
222 * Allocate and read the payload of a packet and intialized its fields to default values.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
223 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
224 * @param pkt packet
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
225 * @param size wanted payload size
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
226 * @return >0 (read size) if OK. AVERROR_xxx otherwise.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
227 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
228 int av_get_packet(ByteIOContext *s, AVPacket *pkt, int size)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
229 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
230 int ret= av_new_packet(pkt, size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
231
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
232 if(ret<0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
233 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
234
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
235 pkt->pos= url_ftell(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
236
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
237 ret= get_buffer(s, pkt->data, size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
238 if(ret<=0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
239 av_free_packet(pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
240 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
241 pkt->size= ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
242
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
243 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
244 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
245
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
246 /* This is a hack - the packet memory allocation stuff is broken. The
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
247 packet is allocated if it was not really allocated */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
248 int av_dup_packet(AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
249 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
250 if (pkt->destruct != av_destruct_packet) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
251 uint8_t *data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
252 /* we duplicate the packet and don't forget to put the padding
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
253 again */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
254 if((unsigned)pkt->size > (unsigned)pkt->size + FF_INPUT_BUFFER_PADDING_SIZE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
255 return AVERROR_NOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
256 data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
257 if (!data) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
258 return AVERROR_NOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
259 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
260 memcpy(data, pkt->data, pkt->size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
261 memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
262 pkt->data = data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
263 pkt->destruct = av_destruct_packet;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
264 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
265 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
266 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
267
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
268 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
269 * Allocate the payload of a packet and intialized its fields to default values.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
270 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
271 * @param filename possible numbered sequence string
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
272 * @return 1 if a valid numbered sequence string, 0 otherwise.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
273 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
274 int av_filename_number_test(const char *filename)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
275 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
276 char buf[1024];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
277 return filename && (av_get_frame_filename(buf, sizeof(buf), filename, 1)>=0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
278 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
279
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
280 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
281 * Guess file format.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
282 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
283 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
284 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
285 AVInputFormat *fmt1, *fmt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
286 int score, score_max;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
287
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
288 fmt = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
289 score_max = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
290 for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
291 if (!is_opened && !(fmt1->flags & AVFMT_NOFILE))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
292 continue;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
293 score = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
294 if (fmt1->read_probe) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
295 score = fmt1->read_probe(pd);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
296 } else if (fmt1->extensions) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
297 if (match_ext(pd->filename, fmt1->extensions)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
298 score = 50;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
299 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
300 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
301 if (score > score_max) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
302 score_max = score;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
303 fmt = fmt1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
304 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
305 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
306 return fmt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
307 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
308
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
309 /************************************************************/
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
310 /* input media file */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
311
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
312 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
313 * Open a media file from an IO stream. 'fmt' must be specified.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
314 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
315 static const char* format_to_name(void* ptr)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
316 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
317 AVFormatContext* fc = (AVFormatContext*) ptr;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
318 if(fc->iformat) return fc->iformat->name;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
319 else if(fc->oformat) return fc->oformat->name;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
320 else return "NULL";
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
321 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
322
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
323 #define OFFSET(x) offsetof(AVFormatContext,x)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
324 #define DEFAULT 0 //should be NAN but it doesnt work as its not a constant in glibc as required by ANSI/ISO C
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
325 //these names are too long to be readable
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
326 #define E AV_OPT_FLAG_ENCODING_PARAM
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
327 #define D AV_OPT_FLAG_DECODING_PARAM
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
328
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
329 static const AVOption options[]={
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
330 {"probesize", NULL, OFFSET(probesize), FF_OPT_TYPE_INT, 32000, 32, INT_MAX, D}, /* 32000 from mpegts.c: 1.0 second at 24Mbit/s */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
331 {"muxrate", "set mux rate", OFFSET(mux_rate), FF_OPT_TYPE_INT, DEFAULT, 0, INT_MAX, E},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
332 {"packetsize", "set packet size", OFFSET(packet_size), FF_OPT_TYPE_INT, DEFAULT, 0, INT_MAX, E},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
333 {"fflags", NULL, OFFSET(flags), FF_OPT_TYPE_FLAGS, DEFAULT, INT_MIN, INT_MAX, D, "fflags"},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
334 {"ignidx", "ignore index", 0, FF_OPT_TYPE_CONST, AVFMT_FLAG_IGNIDX, INT_MIN, INT_MAX, D, "fflags"},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
335 {"genpts", "generate pts", 0, FF_OPT_TYPE_CONST, AVFMT_FLAG_GENPTS, INT_MIN, INT_MAX, D, "fflags"},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
336 {"track", " set the track number", OFFSET(track), FF_OPT_TYPE_INT, DEFAULT, 0, INT_MAX, E},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
337 {"year", "set the year", OFFSET(year), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, E},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
338 {NULL},
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
339 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
340
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
341 #undef E
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
342 #undef D
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
343 #undef DEFAULT
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
344
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
345 static const AVClass av_format_context_class = { "AVFormatContext", format_to_name, options };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
346
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
347 #if LIBAVFORMAT_VERSION_INT >= ((51<<16)+(0<<8)+0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
348 static
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
349 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
350 void avformat_get_context_defaults(AVFormatContext *s){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
351 memset(s, 0, sizeof(AVFormatContext));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
352
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
353 s->av_class = &av_format_context_class;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
354
818
07107d476f32 [svn] - remove some more stuff
nenolod
parents: 808
diff changeset
355 /* av_opt_set_defaults(s); */
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
356 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
357
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
358 AVFormatContext *av_alloc_format_context(void)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
359 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
360 AVFormatContext *ic;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
361 ic = av_malloc(sizeof(AVFormatContext));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
362 if (!ic) return ic;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
363 avformat_get_context_defaults(ic);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
364 ic->av_class = &av_format_context_class;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
365 return ic;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
366 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
367
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
368 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
369 * Allocates all the structures needed to read an input stream.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
370 * This does not open the needed codecs for decoding the stream[s].
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
371 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
372 int av_open_input_stream(AVFormatContext **ic_ptr,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
373 ByteIOContext *pb, const char *filename,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
374 AVInputFormat *fmt, AVFormatParameters *ap)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
375 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
376 int err;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
377 AVFormatContext *ic;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
378 AVFormatParameters default_ap;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
379
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
380 if(!ap){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
381 ap=&default_ap;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
382 memset(ap, 0, sizeof(default_ap));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
383 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
384
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
385 if(!ap->prealloced_context)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
386 ic = av_alloc_format_context();
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
387 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
388 ic = *ic_ptr;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
389 if (!ic) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
390 err = AVERROR_NOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
391 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
392 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
393 ic->iformat = fmt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
394 if (pb)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
395 ic->pb = *pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
396 ic->duration = AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
397 ic->start_time = AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
398 pstrcpy(ic->filename, sizeof(ic->filename), filename);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
399
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
400 /* allocate private data */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
401 if (fmt->priv_data_size > 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
402 ic->priv_data = av_mallocz(fmt->priv_data_size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
403 if (!ic->priv_data) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
404 err = AVERROR_NOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
405 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
406 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
407 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
408 ic->priv_data = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
409 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
410
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
411 err = ic->iformat->read_header(ic, ap);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
412 if (err < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
413 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
414
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
415 if (pb)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
416 ic->data_offset = url_ftell(&ic->pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
417
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
418 *ic_ptr = ic;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
419 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
420 fail:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
421 if (ic) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
422 av_freep(&ic->priv_data);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
423 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
424 av_free(ic);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
425 *ic_ptr = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
426 return err;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
427 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
428
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
429 /** Size of probe buffer, for guessing file type from file contents. */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
430 #define PROBE_BUF_MIN 2048
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
431 #define PROBE_BUF_MAX (1<<20)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
432
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
433 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
434 * Open a media file as input. The codec are not opened. Only the file
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
435 * header (if present) is read.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
436 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
437 * @param ic_ptr the opened media file handle is put here
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
438 * @param filename filename to open.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
439 * @param fmt if non NULL, force the file format to use
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
440 * @param buf_size optional buffer size (zero if default is OK)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
441 * @param ap additionnal parameters needed when opening the file (NULL if default)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
442 * @return 0 if OK. AVERROR_xxx otherwise.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
443 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
444 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
445 AVInputFormat *fmt,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
446 int buf_size,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
447 AVFormatParameters *ap)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
448 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
449 int err, must_open_file, file_opened, probe_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
450 AVProbeData probe_data, *pd = &probe_data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
451 ByteIOContext pb1, *pb = &pb1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
452
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
453 file_opened = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
454 pd->filename = "";
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
455 if (filename)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
456 pd->filename = filename;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
457 pd->buf = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
458 pd->buf_size = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
459
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
460 if (!fmt) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
461 /* guess format if no file can be opened */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
462 fmt = av_probe_input_format(pd, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
463 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
464
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
465 /* do not open file if the format does not need it. XXX: specific
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
466 hack needed to handle RTSP/TCP */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
467 must_open_file = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
468 if (fmt && (fmt->flags & AVFMT_NOFILE)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
469 must_open_file = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
470 pb= NULL; //FIXME this or memset(pb, 0, sizeof(ByteIOContext)); otherwise its uninitalized
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
471 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
472
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
473 if (!fmt || must_open_file) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
474 /* if no file needed do not try to open one */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
475 if (url_fopen(pb, filename, URL_RDONLY) < 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
476 err = AVERROR_IO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
477 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
478 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
479 file_opened = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
480 if (buf_size > 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
481 url_setbufsize(pb, buf_size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
482 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
483
866
d8b8b36d2709 [svn] - prevent skin list overlap when it's first populated and user quickly switchs to other tabs in the prefswin
giacomo
parents: 831
diff changeset
484 for(probe_size= PROBE_BUF_MAX; probe_size<=PROBE_BUF_MAX && !fmt; probe_size<<=1){
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
485 /* read probe data */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
486 pd->buf= av_realloc(pd->buf, probe_size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
487 pd->buf_size = get_buffer(pb, pd->buf, probe_size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
488 if (url_fseek(pb, 0, SEEK_SET) == (offset_t)-EPIPE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
489 url_fclose(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
490 if (url_fopen(pb, filename, URL_RDONLY) < 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
491 file_opened = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
492 err = AVERROR_IO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
493 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
494 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
495 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
496 /* guess file format */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
497 fmt = av_probe_input_format(pd, 1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
498 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
499 av_freep(&pd->buf);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
500 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
501
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
502 /* if still no format found, error */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
503 if (!fmt) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
504 err = AVERROR_NOFMT;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
505 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
506 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
507
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
508 /* XXX: suppress this hack for redirectors */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
509 #ifdef CONFIG_NETWORK
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
510 if (fmt == &redir_demuxer) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
511 err = redir_open(ic_ptr, pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
512 url_fclose(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
513 return err;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
514 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
515 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
516
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
517 /* check filename in case of an image number is expected */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
518 if (fmt->flags & AVFMT_NEEDNUMBER) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
519 if (!av_filename_number_test(filename)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
520 err = AVERROR_NUMEXPECTED;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
521 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
522 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
523 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
524 err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
525 if (err)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
526 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
527 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
528 fail:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
529 av_freep(&pd->buf);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
530 if (file_opened)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
531 url_fclose(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
532 *ic_ptr = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
533 return err;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
534
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
535 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
536
825
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
537 int av_open_input_vfsfile(AVFormatContext **ic_ptr, const char *filename, VFSFile *fd,
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
538 AVInputFormat *fmt,
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
539 int buf_size,
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
540 AVFormatParameters *ap)
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
541 {
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
542 int err, must_open_file, file_opened, probe_size;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
543 AVProbeData probe_data, *pd = &probe_data;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
544 ByteIOContext pb1, *pb = &pb1;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
545
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
546 file_opened = 0;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
547 pd->filename = "";
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
548 if (filename)
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
549 pd->filename = filename;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
550 pd->buf = NULL;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
551 pd->buf_size = 0;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
552
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
553 if (!fmt) {
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
554 /* guess format if no file can be opened */
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
555 fmt = av_probe_input_format(pd, 0);
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
556 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
557
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
558 /* do not open file if the format does not need it. XXX: specific
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
559 hack needed to handle RTSP/TCP */
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
560 must_open_file = 1;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
561 if (fmt && (fmt->flags & AVFMT_NOFILE)) {
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
562 must_open_file = 0;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
563 pb= NULL; //FIXME this or memset(pb, 0, sizeof(ByteIOContext)); otherwise its uninitalized
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
564 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
565
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
566 if (!fmt || must_open_file) {
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
567 /* if no file needed do not try to open one */
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
568 if (url_vfdopen(pb, fd) < 0) {
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
569 err = AVERROR_IO;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
570 goto fail;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
571 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
572 file_opened = 1;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
573 if (buf_size > 0) {
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
574 url_setbufsize(pb, buf_size);
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
575 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
576
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
577 for(probe_size= PROBE_BUF_MIN; probe_size<=PROBE_BUF_MAX && !fmt; probe_size<<=1){
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
578 /* read probe data */
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
579 pd->buf= av_realloc(pd->buf, probe_size);
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
580 pd->buf_size = get_buffer(pb, pd->buf, probe_size);
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
581 if (url_fseek(pb, 0, SEEK_SET) == (offset_t)-EPIPE) {
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
582 url_fclose(pb);
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
583 if (url_fopen(pb, filename, URL_RDONLY) < 0) {
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
584 file_opened = 0;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
585 err = AVERROR_IO;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
586 goto fail;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
587 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
588 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
589 /* guess file format */
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
590 fmt = av_probe_input_format(pd, 1);
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
591 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
592 av_freep(&pd->buf);
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
593 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
594
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
595 /* if still no format found, error */
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
596 if (!fmt) {
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
597 err = AVERROR_NOFMT;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
598 goto fail;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
599 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
600
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
601 /* XXX: suppress this hack for redirectors */
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
602 #ifdef CONFIG_NETWORK
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
603 if (fmt == &redir_demuxer) {
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
604 err = redir_open(ic_ptr, pb);
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
605 url_fclose(pb);
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
606 return err;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
607 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
608 #endif
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
609
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
610 /* check filename in case of an image number is expected */
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
611 if (fmt->flags & AVFMT_NEEDNUMBER) {
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
612 if (!av_filename_number_test(filename)) {
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
613 err = AVERROR_NUMEXPECTED;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
614 goto fail;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
615 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
616 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
617 err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap);
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
618 if (err)
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
619 goto fail;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
620 return 0;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
621 fail:
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
622 av_freep(&pd->buf);
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
623 if (file_opened)
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
624 url_fclose(pb);
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
625 *ic_ptr = NULL;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
626 return err;
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
627
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
628 }
3cbdc6e19d7c [svn] - make this compile again
nenolod
parents: 818
diff changeset
629
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
630 /*******************************************************/
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
631
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
632 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
633 * Read a transport packet from a media file.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
634 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
635 * This function is absolete and should never be used.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
636 * Use av_read_frame() instead.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
637 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
638 * @param s media file handle
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
639 * @param pkt is filled
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
640 * @return 0 if OK. AVERROR_xxx if error.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
641 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
642 int av_read_packet(AVFormatContext *s, AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
643 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
644 return s->iformat->read_packet(s, pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
645 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
646
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
647 /**********************************************************/
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
648
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
649 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
650 * Get the number of samples of an audio frame. Return (-1) if error.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
651 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
652 static int get_audio_frame_size(AVCodecContext *enc, int size)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
653 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
654 int frame_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
655
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
656 if (enc->frame_size <= 1) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
657 int bits_per_sample = av_get_bits_per_sample(enc->codec_id);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
658
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
659 if (bits_per_sample) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
660 if (enc->channels == 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
661 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
662 frame_size = (size << 3) / (bits_per_sample * enc->channels);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
663 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
664 /* used for example by ADPCM codecs */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
665 if (enc->bit_rate == 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
666 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
667 frame_size = (size * 8 * enc->sample_rate) / enc->bit_rate;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
668 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
669 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
670 frame_size = enc->frame_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
671 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
672 return frame_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
673 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
674
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
675
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
676 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
677 * Return the frame duration in seconds, return 0 if not available.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
678 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
679 static void compute_frame_duration(int *pnum, int *pden, AVStream *st,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
680 AVCodecParserContext *pc, AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
681 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
682 int frame_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
683
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
684 *pnum = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
685 *pden = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
686 switch(st->codec->codec_type) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
687 case CODEC_TYPE_VIDEO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
688 if(st->time_base.num*1000LL > st->time_base.den){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
689 *pnum = st->time_base.num;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
690 *pden = st->time_base.den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
691 }else if(st->codec->time_base.num*1000LL > st->codec->time_base.den){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
692 *pnum = st->codec->time_base.num;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
693 *pden = st->codec->time_base.den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
694 if (pc && pc->repeat_pict) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
695 *pden *= 2;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
696 *pnum = (*pnum) * (2 + pc->repeat_pict);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
697 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
698 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
699 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
700 case CODEC_TYPE_AUDIO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
701 frame_size = get_audio_frame_size(st->codec, pkt->size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
702 if (frame_size < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
703 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
704 *pnum = frame_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
705 *pden = st->codec->sample_rate;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
706 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
707 default:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
708 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
709 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
710 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
711
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
712 static int is_intra_only(AVCodecContext *enc){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
713 if(enc->codec_type == CODEC_TYPE_AUDIO){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
714 return 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
715 }else if(enc->codec_type == CODEC_TYPE_VIDEO){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
716 switch(enc->codec_id){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
717 case CODEC_ID_MJPEG:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
718 case CODEC_ID_MJPEGB:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
719 case CODEC_ID_LJPEG:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
720 case CODEC_ID_RAWVIDEO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
721 case CODEC_ID_DVVIDEO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
722 case CODEC_ID_HUFFYUV:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
723 case CODEC_ID_FFVHUFF:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
724 case CODEC_ID_ASV1:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
725 case CODEC_ID_ASV2:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
726 case CODEC_ID_VCR1:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
727 return 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
728 default: break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
729 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
730 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
731 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
732 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
733
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
734 static int64_t lsb2full(int64_t lsb, int64_t last_ts, int lsb_bits){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
735 int64_t mask = lsb_bits < 64 ? (1LL<<lsb_bits)-1 : -1LL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
736 int64_t delta= last_ts - mask/2;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
737 return ((lsb - delta)&mask) + delta;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
738 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
739
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
740 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
741 AVCodecParserContext *pc, AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
742 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
743 int num, den, presentation_delayed;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
744 /* handle wrapping */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
745 if(st->cur_dts != AV_NOPTS_VALUE){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
746 if(pkt->pts != AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
747 pkt->pts= lsb2full(pkt->pts, st->cur_dts, st->pts_wrap_bits);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
748 if(pkt->dts != AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
749 pkt->dts= lsb2full(pkt->dts, st->cur_dts, st->pts_wrap_bits);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
750 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
751
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
752 if (pkt->duration == 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
753 compute_frame_duration(&num, &den, st, pc, pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
754 if (den && num) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
755 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
756 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
757 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
758
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
759 if(is_intra_only(st->codec))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
760 pkt->flags |= PKT_FLAG_KEY;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
761
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
762 /* do we have a video B frame ? */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
763 presentation_delayed = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
764 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
765 /* XXX: need has_b_frame, but cannot get it if the codec is
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
766 not initialized */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
767 if (( st->codec->codec_id == CODEC_ID_H264
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
768 || st->codec->has_b_frames) &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
769 pc && pc->pict_type != FF_B_TYPE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
770 presentation_delayed = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
771 /* this may be redundant, but it shouldnt hurt */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
772 if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
773 presentation_delayed = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
774 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
775
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
776 if(st->cur_dts == AV_NOPTS_VALUE){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
777 if(presentation_delayed) st->cur_dts = -pkt->duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
778 else st->cur_dts = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
779 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
780
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
781 // av_log(NULL, AV_LOG_DEBUG, "IN delayed:%d pts:%lld, dts:%lld cur_dts:%lld st:%d pc:%p\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts, pkt->stream_index, pc);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
782 /* interpolate PTS and DTS if they are not present */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
783 if (presentation_delayed) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
784 /* DTS = decompression time stamp */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
785 /* PTS = presentation time stamp */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
786 if (pkt->dts == AV_NOPTS_VALUE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
787 /* if we know the last pts, use it */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
788 if(st->last_IP_pts != AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
789 st->cur_dts = pkt->dts = st->last_IP_pts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
790 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
791 pkt->dts = st->cur_dts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
792 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
793 st->cur_dts = pkt->dts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
794 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
795 /* this is tricky: the dts must be incremented by the duration
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
796 of the frame we are displaying, i.e. the last I or P frame */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
797 if (st->last_IP_duration == 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
798 st->cur_dts += pkt->duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
799 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
800 st->cur_dts += st->last_IP_duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
801 st->last_IP_duration = pkt->duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
802 st->last_IP_pts= pkt->pts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
803 /* cannot compute PTS if not present (we can compute it only
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
804 by knowing the futur */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
805 } else if(pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE || pkt->duration){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
806 if(pkt->pts != AV_NOPTS_VALUE && pkt->duration){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
807 int64_t old_diff= FFABS(st->cur_dts - pkt->duration - pkt->pts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
808 int64_t new_diff= FFABS(st->cur_dts - pkt->pts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
809 if(old_diff < new_diff && old_diff < (pkt->duration>>3)){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
810 pkt->pts += pkt->duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
811 // av_log(NULL, AV_LOG_DEBUG, "id:%d old:%Ld new:%Ld dur:%d cur:%Ld size:%d\n", pkt->stream_index, old_diff, new_diff, pkt->duration, st->cur_dts, pkt->size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
812 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
813 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
814
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
815 /* presentation is not delayed : PTS and DTS are the same */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
816 if (pkt->pts == AV_NOPTS_VALUE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
817 if (pkt->dts == AV_NOPTS_VALUE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
818 pkt->pts = st->cur_dts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
819 pkt->dts = st->cur_dts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
820 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
821 else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
822 st->cur_dts = pkt->dts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
823 pkt->pts = pkt->dts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
824 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
825 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
826 st->cur_dts = pkt->pts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
827 pkt->dts = pkt->pts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
828 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
829 st->cur_dts += pkt->duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
830 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
831 // av_log(NULL, AV_LOG_DEBUG, "OUTdelayed:%d pts:%lld, dts:%lld cur_dts:%lld\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
832
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
833 /* update flags */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
834 if (pc) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
835 pkt->flags = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
836 /* key frame computation */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
837 switch(st->codec->codec_type) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
838 case CODEC_TYPE_VIDEO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
839 if (pc->pict_type == FF_I_TYPE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
840 pkt->flags |= PKT_FLAG_KEY;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
841 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
842 case CODEC_TYPE_AUDIO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
843 pkt->flags |= PKT_FLAG_KEY;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
844 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
845 default:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
846 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
847 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
848 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
849 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
850
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
851 void av_destruct_packet_nofree(AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
852 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
853 pkt->data = NULL; pkt->size = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
854 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
855
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
856 static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
857 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
858 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
859 int len, ret, i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
860
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
861 for(;;) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
862 /* select current input stream component */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
863 st = s->cur_st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
864 if (st) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
865 if (!st->need_parsing || !st->parser) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
866 /* no parsing needed: we just output the packet as is */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
867 /* raw data support */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
868 *pkt = s->cur_pkt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
869 compute_pkt_fields(s, st, NULL, pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
870 s->cur_st = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
871 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
872 } else if (s->cur_len > 0 && st->discard < AVDISCARD_ALL) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
873 len = av_parser_parse(st->parser, st->codec, &pkt->data, &pkt->size,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
874 s->cur_ptr, s->cur_len,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
875 s->cur_pkt.pts, s->cur_pkt.dts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
876 s->cur_pkt.pts = AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
877 s->cur_pkt.dts = AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
878 /* increment read pointer */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
879 s->cur_ptr += len;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
880 s->cur_len -= len;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
881
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
882 /* return packet if any */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
883 if (pkt->size) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
884 got_packet:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
885 pkt->duration = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
886 pkt->stream_index = st->index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
887 pkt->pts = st->parser->pts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
888 pkt->dts = st->parser->dts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
889 pkt->destruct = av_destruct_packet_nofree;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
890 compute_pkt_fields(s, st, st->parser, pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
891 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
892 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
893 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
894 /* free packet */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
895 av_free_packet(&s->cur_pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
896 s->cur_st = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
897 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
898 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
899 /* read next packet */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
900 ret = av_read_packet(s, &s->cur_pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
901 if (ret < 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
902 if (ret == -EAGAIN)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
903 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
904 /* return the last frames, if any */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
905 for(i = 0; i < s->nb_streams; i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
906 st = s->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
907 if (st->parser && st->need_parsing) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
908 av_parser_parse(st->parser, st->codec,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
909 &pkt->data, &pkt->size,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
910 NULL, 0,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
911 AV_NOPTS_VALUE, AV_NOPTS_VALUE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
912 if (pkt->size)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
913 goto got_packet;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
914 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
915 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
916 /* no more packets: really terminates parsing */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
917 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
918 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
919
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
920 st = s->streams[s->cur_pkt.stream_index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
921 if(st->codec->debug & FF_DEBUG_PTS)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
922 av_log(s, AV_LOG_DEBUG, "av_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
923 s->cur_pkt.stream_index,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
924 s->cur_pkt.pts,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
925 s->cur_pkt.dts,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
926 s->cur_pkt.size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
927
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
928 s->cur_st = st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
929 s->cur_ptr = s->cur_pkt.data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
930 s->cur_len = s->cur_pkt.size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
931 if (st->need_parsing && !st->parser) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
932 st->parser = av_parser_init(st->codec->codec_id);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
933 if (!st->parser) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
934 /* no parser available : just output the raw packets */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
935 st->need_parsing = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
936 }else if(st->need_parsing == 2){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
937 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
938 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
939 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
940 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
941 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
942 if(st->codec->debug & FF_DEBUG_PTS)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
943 av_log(s, AV_LOG_DEBUG, "av_read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
944 pkt->stream_index,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
945 pkt->pts,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
946 pkt->dts,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
947 pkt->size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
948
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
949 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
950 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
951
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
952 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
953 * Return the next frame of a stream.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
954 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
955 * The returned packet is valid
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
956 * until the next av_read_frame() or until av_close_input_file() and
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
957 * must be freed with av_free_packet. For video, the packet contains
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
958 * exactly one frame. For audio, it contains an integer number of
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
959 * frames if each frame has a known fixed size (e.g. PCM or ADPCM
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
960 * data). If the audio frames have a variable size (e.g. MPEG audio),
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
961 * then it contains one frame.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
962 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
963 * pkt->pts, pkt->dts and pkt->duration are always set to correct
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
964 * values in AV_TIME_BASE unit (and guessed if the format cannot
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
965 * provided them). pkt->pts can be AV_NOPTS_VALUE if the video format
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
966 * has B frames, so it is better to rely on pkt->dts if you do not
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
967 * decompress the payload.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
968 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
969 * @return 0 if OK, < 0 if error or end of file.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
970 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
971 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
972 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
973 AVPacketList *pktl;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
974 int eof=0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
975 const int genpts= s->flags & AVFMT_FLAG_GENPTS;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
976
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
977 for(;;){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
978 pktl = s->packet_buffer;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
979 if (pktl) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
980 AVPacket *next_pkt= &pktl->pkt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
981
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
982 if(genpts && next_pkt->dts != AV_NOPTS_VALUE){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
983 while(pktl && next_pkt->pts == AV_NOPTS_VALUE){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
984 if( pktl->pkt.stream_index == next_pkt->stream_index
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
985 && next_pkt->dts < pktl->pkt.dts
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
986 && pktl->pkt.pts != pktl->pkt.dts //not b frame
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
987 /*&& pktl->pkt.dts != AV_NOPTS_VALUE*/){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
988 next_pkt->pts= pktl->pkt.dts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
989 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
990 pktl= pktl->next;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
991 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
992 pktl = s->packet_buffer;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
993 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
994
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
995 if( next_pkt->pts != AV_NOPTS_VALUE
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
996 || next_pkt->dts == AV_NOPTS_VALUE
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
997 || !genpts || eof){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
998 /* read packet from packet buffer, if there is data */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
999 *pkt = *next_pkt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1000 s->packet_buffer = pktl->next;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1001 av_free(pktl);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1002 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1003 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1004 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1005 if(genpts){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1006 AVPacketList **plast_pktl= &s->packet_buffer;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1007 int ret= av_read_frame_internal(s, pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1008 if(ret<0){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1009 if(pktl && ret != -EAGAIN){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1010 eof=1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1011 continue;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1012 }else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1013 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1014 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1015
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1016 /* duplicate the packet */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1017 if (av_dup_packet(pkt) < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1018 return AVERROR_NOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1019
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1020 while(*plast_pktl) plast_pktl= &(*plast_pktl)->next; //FIXME maybe maintain pointer to the last?
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1021
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1022 pktl = av_mallocz(sizeof(AVPacketList));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1023 if (!pktl)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1024 return AVERROR_NOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1025
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1026 /* add the packet in the buffered packet list */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1027 *plast_pktl = pktl;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1028 pktl->pkt= *pkt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1029 }else{
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1030 assert(!s->packet_buffer);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1031 return av_read_frame_internal(s, pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1032 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1033 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1034 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1035
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1036 /* XXX: suppress the packet queue */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1037 static void flush_packet_queue(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1038 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1039 AVPacketList *pktl;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1040
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1041 for(;;) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1042 pktl = s->packet_buffer;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1043 if (!pktl)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1044 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1045 s->packet_buffer = pktl->next;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1046 av_free_packet(&pktl->pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1047 av_free(pktl);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1048 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1049 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1050
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1051 /*******************************************************/
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1052 /* seek support */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1053
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1054 int av_find_default_stream_index(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1055 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1056 int i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1057 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1058
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1059 if (s->nb_streams <= 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1060 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1061 for(i = 0; i < s->nb_streams; i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1062 st = s->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1063 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1064 return i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1065 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1066 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1067 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1068 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1069
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1070 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1071 * Flush the frame reader.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1072 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1073 static void av_read_frame_flush(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1074 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1075 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1076 int i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1077
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1078 flush_packet_queue(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1079
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1080 /* free previous packet */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1081 if (s->cur_st) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1082 if (s->cur_st->parser)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1083 av_free_packet(&s->cur_pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1084 s->cur_st = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1085 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1086 /* fail safe */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1087 s->cur_ptr = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1088 s->cur_len = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1089
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1090 /* for each stream, reset read state */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1091 for(i = 0; i < s->nb_streams; i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1092 st = s->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1093
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1094 if (st->parser) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1095 av_parser_close(st->parser);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1096 st->parser = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1097 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1098 st->last_IP_pts = AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1099 st->cur_dts = 0; /* we set the current DTS to an unspecified origin */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1100 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1101 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1102
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1103 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1104 * Updates cur_dts of all streams based on given timestamp and AVStream.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1105 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1106 * Stream ref_st unchanged, others set cur_dts in their native timebase
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1107 * only needed for timestamp wrapping or if (dts not set and pts!=dts)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1108 * @param timestamp new dts expressed in time_base of param ref_st
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1109 * @param ref_st reference stream giving time_base of param timestamp
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1110 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1111 void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1112 int i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1113
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1114 for(i = 0; i < s->nb_streams; i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1115 AVStream *st = s->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1116
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1117 st->cur_dts = av_rescale(timestamp,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1118 st->time_base.den * (int64_t)ref_st->time_base.num,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1119 st->time_base.num * (int64_t)ref_st->time_base.den);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1120 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1121 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1122
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1123 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1124 * Add a index entry into a sorted list updateing if it is already there.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1125 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1126 * @param timestamp timestamp in the timebase of the given stream
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1127 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1128 int av_add_index_entry(AVStream *st,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1129 int64_t pos, int64_t timestamp, int size, int distance, int flags)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1130 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1131 AVIndexEntry *entries, *ie;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1132 int index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1133
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1134 if((unsigned)st->nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1135 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1136
830
68562d99230f [svn] - more symbol cleanups.
nenolod
parents: 825
diff changeset
1137 entries = av_realloc(st->index_entries,
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1138 (st->nb_index_entries + 1) *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1139 sizeof(AVIndexEntry));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1140 if(!entries)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1141 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1142
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1143 st->index_entries= entries;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1144
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1145 index= av_index_search_timestamp(st, timestamp, AVSEEK_FLAG_ANY);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1146
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1147 if(index<0){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1148 index= st->nb_index_entries++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1149 ie= &entries[index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1150 assert(index==0 || ie[-1].timestamp < timestamp);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1151 }else{
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1152 ie= &entries[index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1153 if(ie->timestamp != timestamp){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1154 if(ie->timestamp <= timestamp)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1155 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1156 memmove(entries + index + 1, entries + index, sizeof(AVIndexEntry)*(st->nb_index_entries - index));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1157 st->nb_index_entries++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1158 }else if(ie->pos == pos && distance < ie->min_distance) //dont reduce the distance
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1159 distance= ie->min_distance;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1160 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1161
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1162 ie->pos = pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1163 ie->timestamp = timestamp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1164 ie->min_distance= distance;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1165 ie->size= size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1166 ie->flags = flags;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1167
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1168 return index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1169 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1170
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1171 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1172 * build an index for raw streams using a parser.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1173 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1174 static void av_build_index_raw(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1175 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1176 AVPacket pkt1, *pkt = &pkt1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1177 int ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1178 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1179
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1180 st = s->streams[0];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1181 av_read_frame_flush(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1182 url_fseek(&s->pb, s->data_offset, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1183
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1184 for(;;) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1185 ret = av_read_frame(s, pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1186 if (ret < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1187 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1188 if (pkt->stream_index == 0 && st->parser &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1189 (pkt->flags & PKT_FLAG_KEY)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1190 av_add_index_entry(st, st->parser->frame_offset, pkt->dts,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1191 0, 0, AVINDEX_KEYFRAME);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1192 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1193 av_free_packet(pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1194 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1195 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1196
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1197 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1198 * Returns TRUE if we deal with a raw stream.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1199 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1200 * Raw codec data and parsing needed.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1201 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1202 static int is_raw_stream(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1203 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1204 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1205
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1206 if (s->nb_streams != 1)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1207 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1208 st = s->streams[0];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1209 if (!st->need_parsing)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1210 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1211 return 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1212 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1213
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1214 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1215 * Gets the index for a specific timestamp.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1216 * @param flags if AVSEEK_FLAG_BACKWARD then the returned index will correspond to
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1217 * the timestamp which is <= the requested one, if backward is 0
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1218 * then it will be >=
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1219 * if AVSEEK_FLAG_ANY seek to any frame, only keyframes otherwise
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1220 * @return < 0 if no such timestamp could be found
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1221 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1222 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1223 int flags)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1224 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1225 AVIndexEntry *entries= st->index_entries;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1226 int nb_entries= st->nb_index_entries;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1227 int a, b, m;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1228 int64_t timestamp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1229
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1230 a = - 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1231 b = nb_entries;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1232
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1233 while (b - a > 1) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1234 m = (a + b) >> 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1235 timestamp = entries[m].timestamp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1236 if(timestamp >= wanted_timestamp)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1237 b = m;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1238 if(timestamp <= wanted_timestamp)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1239 a = m;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1240 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1241 m= (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1242
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1243 if(!(flags & AVSEEK_FLAG_ANY)){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1244 while(m>=0 && m<nb_entries && !(entries[m].flags & AVINDEX_KEYFRAME)){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1245 m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1246 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1247 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1248
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1249 if(m == nb_entries)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1250 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1251 return m;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1252 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1253
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1254 #define DEBUG_SEEK
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1255
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1256 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1257 * Does a binary search using av_index_search_timestamp() and AVCodec.read_timestamp().
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1258 * this isnt supposed to be called directly by a user application, but by demuxers
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1259 * @param target_ts target timestamp in the time base of the given stream
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1260 * @param stream_index stream number
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1261 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1262 int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1263 AVInputFormat *avif= s->iformat;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1264 int64_t pos_min, pos_max, pos, pos_limit;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1265 int64_t ts_min, ts_max, ts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1266 int64_t start_pos, filesize;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1267 int index, no_change;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1268 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1269
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1270 if (stream_index < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1271 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1272
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1273 #ifdef DEBUG_SEEK
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1274 av_log(s, AV_LOG_DEBUG, "read_seek: %d %"PRId64"\n", stream_index, target_ts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1275 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1276
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1277 ts_max=
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1278 ts_min= AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1279 pos_limit= -1; //gcc falsely says it may be uninitalized
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1280
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1281 st= s->streams[stream_index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1282 if(st->index_entries){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1283 AVIndexEntry *e;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1284
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1285 index= av_index_search_timestamp(st, target_ts, flags | AVSEEK_FLAG_BACKWARD); //FIXME whole func must be checked for non keyframe entries in index case, especially read_timestamp()
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1286 index= FFMAX(index, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1287 e= &st->index_entries[index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1288
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1289 if(e->timestamp <= target_ts || e->pos == e->min_distance){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1290 pos_min= e->pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1291 ts_min= e->timestamp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1292 #ifdef DEBUG_SEEK
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1293 av_log(s, AV_LOG_DEBUG, "using cached pos_min=0x%"PRIx64" dts_min=%"PRId64"\n",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1294 pos_min,ts_min);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1295 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1296 }else{
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1297 assert(index==0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1298 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1299
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1300 index= av_index_search_timestamp(st, target_ts, flags & ~AVSEEK_FLAG_BACKWARD);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1301 assert(index < st->nb_index_entries);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1302 if(index >= 0){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1303 e= &st->index_entries[index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1304 assert(e->timestamp >= target_ts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1305 pos_max= e->pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1306 ts_max= e->timestamp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1307 pos_limit= pos_max - e->min_distance;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1308 #ifdef DEBUG_SEEK
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1309 av_log(s, AV_LOG_DEBUG, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64" dts_max=%"PRId64"\n",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1310 pos_max,pos_limit, ts_max);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1311 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1312 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1313 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1314
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1315 if(ts_min == AV_NOPTS_VALUE){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1316 pos_min = s->data_offset;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1317 ts_min = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1318 if (ts_min == AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1319 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1320 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1321
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1322 if(ts_max == AV_NOPTS_VALUE){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1323 int step= 1024;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1324 filesize = url_fsize(&s->pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1325 pos_max = filesize - 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1326 do{
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1327 pos_max -= step;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1328 ts_max = avif->read_timestamp(s, stream_index, &pos_max, pos_max + step);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1329 step += step;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1330 }while(ts_max == AV_NOPTS_VALUE && pos_max >= step);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1331 if (ts_max == AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1332 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1333
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1334 for(;;){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1335 int64_t tmp_pos= pos_max + 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1336 int64_t tmp_ts= avif->read_timestamp(s, stream_index, &tmp_pos, INT64_MAX);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1337 if(tmp_ts == AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1338 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1339 ts_max= tmp_ts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1340 pos_max= tmp_pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1341 if(tmp_pos >= filesize)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1342 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1343 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1344 pos_limit= pos_max;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1345 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1346
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1347 if(ts_min > ts_max){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1348 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1349 }else if(ts_min == ts_max){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1350 pos_limit= pos_min;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1351 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1352
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1353 no_change=0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1354 while (pos_min < pos_limit) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1355 #ifdef DEBUG_SEEK
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1356 av_log(s, AV_LOG_DEBUG, "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%"PRId64" dts_max=%"PRId64"\n",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1357 pos_min, pos_max,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1358 ts_min, ts_max);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1359 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1360 assert(pos_limit <= pos_max);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1361
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1362 if(no_change==0){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1363 int64_t approximate_keyframe_distance= pos_max - pos_limit;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1364 // interpolate position (better than dichotomy)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1365 pos = av_rescale(target_ts - ts_min, pos_max - pos_min, ts_max - ts_min)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1366 + pos_min - approximate_keyframe_distance;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1367 }else if(no_change==1){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1368 // bisection, if interpolation failed to change min or max pos last time
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1369 pos = (pos_min + pos_limit)>>1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1370 }else{
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1371 // linear search if bisection failed, can only happen if there are very few or no keframes between min/max
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1372 pos=pos_min;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1373 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1374 if(pos <= pos_min)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1375 pos= pos_min + 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1376 else if(pos > pos_limit)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1377 pos= pos_limit;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1378 start_pos= pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1379
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1380 ts = avif->read_timestamp(s, stream_index, &pos, INT64_MAX); //may pass pos_limit instead of -1
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1381 if(pos == pos_max)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1382 no_change++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1383 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1384 no_change=0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1385 #ifdef DEBUG_SEEK
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1386 av_log(s, AV_LOG_DEBUG, "%"PRId64" %"PRId64" %"PRId64" / %"PRId64" %"PRId64" %"PRId64" target:%"PRId64" limit:%"PRId64" start:%"PRId64" noc:%d\n", pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts, pos_limit, start_pos, no_change);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1387 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1388 assert(ts != AV_NOPTS_VALUE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1389 if (target_ts <= ts) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1390 pos_limit = start_pos - 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1391 pos_max = pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1392 ts_max = ts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1393 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1394 if (target_ts >= ts) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1395 pos_min = pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1396 ts_min = ts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1397 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1398 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1399
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1400 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1401 ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1402 #ifdef DEBUG_SEEK
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1403 pos_min = pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1404 ts_min = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1405 pos_min++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1406 ts_max = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1407 av_log(s, AV_LOG_DEBUG, "pos=0x%"PRIx64" %"PRId64"<=%"PRId64"<=%"PRId64"\n",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1408 pos, ts_min, target_ts, ts_max);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1409 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1410 /* do the seek */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1411 url_fseek(&s->pb, pos, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1412
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1413 av_update_cur_dts(s, st, ts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1414
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1415 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1416 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1417
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1418 static int av_seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1419 int64_t pos_min, pos_max;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1420 #if 0
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1421 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1422
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1423 if (stream_index < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1424 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1425
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1426 st= s->streams[stream_index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1427 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1428
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1429 pos_min = s->data_offset;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1430 pos_max = url_fsize(&s->pb) - 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1431
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1432 if (pos < pos_min) pos= pos_min;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1433 else if(pos > pos_max) pos= pos_max;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1434
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1435 url_fseek(&s->pb, pos, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1436
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1437 #if 0
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1438 av_update_cur_dts(s, st, ts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1439 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1440 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1441 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1442
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1443 static int av_seek_frame_generic(AVFormatContext *s,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1444 int stream_index, int64_t timestamp, int flags)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1445 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1446 int index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1447 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1448 AVIndexEntry *ie;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1449
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1450 if (!s->index_built) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1451 if (is_raw_stream(s)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1452 av_build_index_raw(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1453 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1454 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1455 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1456 s->index_built = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1457 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1458
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1459 st = s->streams[stream_index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1460 index = av_index_search_timestamp(st, timestamp, flags);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1461 if (index < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1462 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1463
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1464 /* now we have found the index, we can seek */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1465 ie = &st->index_entries[index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1466 av_read_frame_flush(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1467 url_fseek(&s->pb, ie->pos, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1468
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1469 av_update_cur_dts(s, st, ie->timestamp);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1470
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1471 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1472 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1473
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1474 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1475 * Seek to the key frame at timestamp.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1476 * 'timestamp' in 'stream_index'.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1477 * @param stream_index If stream_index is (-1), a default
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1478 * stream is selected, and timestamp is automatically converted
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1479 * from AV_TIME_BASE units to the stream specific time_base.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1480 * @param timestamp timestamp in AVStream.time_base units
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1481 * or if there is no stream specified then in AV_TIME_BASE units
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1482 * @param flags flags which select direction and seeking mode
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1483 * @return >= 0 on success
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1484 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1485 int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1486 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1487 int ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1488 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1489
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1490 av_read_frame_flush(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1491
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1492 if(flags & AVSEEK_FLAG_BYTE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1493 return av_seek_frame_byte(s, stream_index, timestamp, flags);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1494
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1495 if(stream_index < 0){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1496 stream_index= av_find_default_stream_index(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1497 if(stream_index < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1498 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1499
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1500 st= s->streams[stream_index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1501 /* timestamp for default must be expressed in AV_TIME_BASE units */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1502 timestamp = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1503 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1504 st= s->streams[stream_index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1505
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1506 /* first, we try the format specific seek */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1507 if (s->iformat->read_seek)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1508 ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1509 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1510 ret = -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1511 if (ret >= 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1512 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1513 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1514
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1515 if(s->iformat->read_timestamp)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1516 return av_seek_frame_binary(s, stream_index, timestamp, flags);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1517 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1518 return av_seek_frame_generic(s, stream_index, timestamp, flags);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1519 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1520
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1521 /*******************************************************/
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1522
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1523 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1524 * Returns TRUE if the stream has accurate timings in any stream.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1525 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1526 * @return TRUE if the stream has accurate timings for at least one component.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1527 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1528 static int av_has_timings(AVFormatContext *ic)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1529 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1530 int i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1531 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1532
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1533 for(i = 0;i < ic->nb_streams; i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1534 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1535 if (st->start_time != AV_NOPTS_VALUE &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1536 st->duration != AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1537 return 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1538 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1539 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1540 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1541
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1542 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1543 * Estimate the stream timings from the one of each components.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1544 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1545 * Also computes the global bitrate if possible.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1546 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1547 static void av_update_stream_timings(AVFormatContext *ic)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1548 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1549 int64_t start_time, start_time1, end_time, end_time1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1550 int i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1551 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1552
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1553 start_time = MAXINT64;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1554 end_time = MININT64;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1555 for(i = 0;i < ic->nb_streams; i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1556 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1557 if (st->start_time != AV_NOPTS_VALUE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1558 start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1559 if (start_time1 < start_time)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1560 start_time = start_time1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1561 if (st->duration != AV_NOPTS_VALUE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1562 end_time1 = start_time1
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1563 + av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1564 if (end_time1 > end_time)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1565 end_time = end_time1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1566 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1567 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1568 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1569 if (start_time != MAXINT64) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1570 ic->start_time = start_time;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1571 if (end_time != MININT64) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1572 ic->duration = end_time - start_time;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1573 if (ic->file_size > 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1574 /* compute the bit rate */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1575 ic->bit_rate = (double)ic->file_size * 8.0 * AV_TIME_BASE /
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1576 (double)ic->duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1577 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1578 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1579 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1580
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1581 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1582
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1583 static void fill_all_stream_timings(AVFormatContext *ic)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1584 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1585 int i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1586 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1587
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1588 av_update_stream_timings(ic);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1589 for(i = 0;i < ic->nb_streams; i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1590 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1591 if (st->start_time == AV_NOPTS_VALUE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1592 if(ic->start_time != AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1593 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q, st->time_base);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1594 if(ic->duration != AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1595 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q, st->time_base);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1596 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1597 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1598 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1599
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1600 static void av_estimate_timings_from_bit_rate(AVFormatContext *ic)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1601 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1602 int64_t filesize, duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1603 int bit_rate, i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1604 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1605
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1606 /* if bit_rate is already set, we believe it */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1607 if (ic->bit_rate == 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1608 bit_rate = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1609 for(i=0;i<ic->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1610 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1611 bit_rate += st->codec->bit_rate;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1612 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1613 ic->bit_rate = bit_rate;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1614 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1615
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1616 /* if duration is already set, we believe it */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1617 if (ic->duration == AV_NOPTS_VALUE &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1618 ic->bit_rate != 0 &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1619 ic->file_size != 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1620 filesize = ic->file_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1621 if (filesize > 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1622 for(i = 0; i < ic->nb_streams; i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1623 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1624 duration= av_rescale(8*filesize, st->time_base.den, ic->bit_rate*(int64_t)st->time_base.num);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1625 if (st->start_time == AV_NOPTS_VALUE ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1626 st->duration == AV_NOPTS_VALUE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1627 st->start_time = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1628 st->duration = duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1629 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1630 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1631 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1632 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1633 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1634
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1635 #define DURATION_MAX_READ_SIZE 250000
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1636
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1637 /* only usable for MPEG-PS streams */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1638 static void av_estimate_timings_from_pts(AVFormatContext *ic)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1639 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1640 AVPacket pkt1, *pkt = &pkt1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1641 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1642 int read_size, i, ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1643 int64_t end_time;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1644 int64_t filesize, offset, duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1645
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1646 /* free previous packet */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1647 if (ic->cur_st && ic->cur_st->parser)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1648 av_free_packet(&ic->cur_pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1649 ic->cur_st = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1650
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1651 /* flush packet queue */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1652 flush_packet_queue(ic);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1653
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1654 for(i=0;i<ic->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1655 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1656 if (st->parser) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1657 av_parser_close(st->parser);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1658 st->parser= NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1659 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1660 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1661
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1662 /* we read the first packets to get the first PTS (not fully
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1663 accurate, but it is enough now) */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1664 url_fseek(&ic->pb, 0, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1665 read_size = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1666 for(;;) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1667 if (read_size >= DURATION_MAX_READ_SIZE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1668 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1669 /* if all info is available, we can stop */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1670 for(i = 0;i < ic->nb_streams; i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1671 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1672 if (st->start_time == AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1673 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1674 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1675 if (i == ic->nb_streams)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1676 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1677
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1678 ret = av_read_packet(ic, pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1679 if (ret != 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1680 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1681 read_size += pkt->size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1682 st = ic->streams[pkt->stream_index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1683 if (pkt->pts != AV_NOPTS_VALUE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1684 if (st->start_time == AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1685 st->start_time = pkt->pts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1686 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1687 av_free_packet(pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1688 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1689
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1690 /* estimate the end time (duration) */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1691 /* XXX: may need to support wrapping */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1692 filesize = ic->file_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1693 offset = filesize - DURATION_MAX_READ_SIZE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1694 if (offset < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1695 offset = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1696
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1697 url_fseek(&ic->pb, offset, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1698 read_size = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1699 for(;;) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1700 if (read_size >= DURATION_MAX_READ_SIZE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1701 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1702 /* if all info is available, we can stop */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1703 for(i = 0;i < ic->nb_streams; i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1704 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1705 if (st->duration == AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1706 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1707 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1708 if (i == ic->nb_streams)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1709 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1710
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1711 ret = av_read_packet(ic, pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1712 if (ret != 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1713 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1714 read_size += pkt->size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1715 st = ic->streams[pkt->stream_index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1716 if (pkt->pts != AV_NOPTS_VALUE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1717 end_time = pkt->pts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1718 duration = end_time - st->start_time;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1719 if (duration > 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1720 if (st->duration == AV_NOPTS_VALUE ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1721 st->duration < duration)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1722 st->duration = duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1723 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1724 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1725 av_free_packet(pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1726 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1727
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1728 fill_all_stream_timings(ic);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1729
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1730 url_fseek(&ic->pb, 0, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1731 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1732
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1733 static void av_estimate_timings(AVFormatContext *ic)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1734 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1735 int64_t file_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1736
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1737 /* get the file size, if possible */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1738 if (ic->iformat->flags & AVFMT_NOFILE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1739 file_size = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1740 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1741 file_size = url_fsize(&ic->pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1742 if (file_size < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1743 file_size = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1744 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1745 ic->file_size = file_size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1746
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1747 if ((!strcmp(ic->iformat->name, "mpeg") ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1748 !strcmp(ic->iformat->name, "mpegts")) &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1749 file_size && !ic->pb.is_streamed) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1750 /* get accurate estimate from the PTSes */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1751 av_estimate_timings_from_pts(ic);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1752 } else if (av_has_timings(ic)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1753 /* at least one components has timings - we use them for all
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1754 the components */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1755 fill_all_stream_timings(ic);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1756 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1757 /* less precise: use bit rate info */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1758 av_estimate_timings_from_bit_rate(ic);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1759 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1760 av_update_stream_timings(ic);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1761
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1762 #if 0
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1763 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1764 int i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1765 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1766 for(i = 0;i < ic->nb_streams; i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1767 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1768 printf("%d: start_time: %0.3f duration: %0.3f\n",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1769 i, (double)st->start_time / AV_TIME_BASE,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1770 (double)st->duration / AV_TIME_BASE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1771 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1772 printf("stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1773 (double)ic->start_time / AV_TIME_BASE,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1774 (double)ic->duration / AV_TIME_BASE,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1775 ic->bit_rate / 1000);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1776 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1777 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1778 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1779
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1780 static int has_codec_parameters(AVCodecContext *enc)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1781 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1782 int val;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1783 switch(enc->codec_type) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1784 case CODEC_TYPE_AUDIO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1785 val = enc->sample_rate;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1786 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1787 case CODEC_TYPE_VIDEO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1788 val = enc->width && enc->pix_fmt != PIX_FMT_NONE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1789 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1790 default:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1791 val = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1792 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1793 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1794 return (val != 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1795 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1796
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1797 static int try_decode_frame(AVStream *st, const uint8_t *data, int size)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1798 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1799 int16_t *samples;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1800 AVCodec *codec;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1801 int got_picture, ret=0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1802 AVFrame picture;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1803
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1804 if(!st->codec->codec){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1805 codec = avcodec_find_decoder(st->codec->codec_id);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1806 if (!codec)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1807 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1808 ret = avcodec_open(st->codec, codec);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1809 if (ret < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1810 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1811 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1812
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1813 if(!has_codec_parameters(st->codec)){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1814 switch(st->codec->codec_type) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1815 case CODEC_TYPE_VIDEO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1816 ret = avcodec_decode_video(st->codec, &picture,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1817 &got_picture, (uint8_t *)data, size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1818 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1819 case CODEC_TYPE_AUDIO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1820 samples = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1821 if (!samples)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1822 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1823 ret = avcodec_decode_audio(st->codec, samples,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1824 &got_picture, (uint8_t *)data, size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1825 av_free(samples);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1826 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1827 default:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1828 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1829 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1830 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1831 fail:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1832 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1833 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1834
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1835 /* absolute maximum size we read until we abort */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1836 #define MAX_READ_SIZE 5000000
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1837
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1838 /* maximum duration until we stop analysing the stream */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1839 #define MAX_STREAM_DURATION ((int)(AV_TIME_BASE * 3.0))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1840
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1841 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1842 * Read the beginning of a media file to get stream information. This
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1843 * is useful for file formats with no headers such as MPEG. This
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1844 * function also compute the real frame rate in case of mpeg2 repeat
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1845 * frame mode.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1846 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1847 * @param ic media file handle
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1848 * @return >=0 if OK. AVERROR_xxx if error.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1849 * @todo let user decide somehow what information is needed so we dont waste time geting stuff the user doesnt need
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1850 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1851 int av_find_stream_info(AVFormatContext *ic)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1852 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1853 int i, count, ret, read_size, j;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1854 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1855 AVPacket pkt1, *pkt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1856 AVPacketList *pktl=NULL, **ppktl;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1857 int64_t last_dts[MAX_STREAMS];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1858 int64_t duration_sum[MAX_STREAMS];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1859 int duration_count[MAX_STREAMS]={0};
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1860
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1861 for(i=0;i<ic->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1862 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1863 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1864 /* if(!st->time_base.num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1865 st->time_base= */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1866 if(!st->codec->time_base.num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1867 st->codec->time_base= st->time_base;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1868 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1869 //only for the split stuff
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1870 if (!st->parser) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1871 st->parser = av_parser_init(st->codec->codec_id);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1872 if(st->need_parsing == 2 && st->parser){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1873 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1874 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1875 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1876 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1877
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1878 for(i=0;i<MAX_STREAMS;i++){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1879 last_dts[i]= AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1880 duration_sum[i]= INT64_MAX;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1881 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1882
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1883 count = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1884 read_size = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1885 ppktl = &ic->packet_buffer;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1886 for(;;) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1887 /* check if one codec still needs to be handled */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1888 for(i=0;i<ic->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1889 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1890 if (!has_codec_parameters(st->codec))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1891 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1892 /* variable fps and no guess at the real fps */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1893 if( st->codec->time_base.den >= 101LL*st->codec->time_base.num
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1894 && duration_count[i]<20 && st->codec->codec_type == CODEC_TYPE_VIDEO)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1895 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1896 if(st->parser && st->parser->parser->split && !st->codec->extradata)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1897 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1898 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1899 if (i == ic->nb_streams) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1900 /* NOTE: if the format has no header, then we need to read
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1901 some packets to get most of the streams, so we cannot
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1902 stop here */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1903 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1904 /* if we found the info for all the codecs, we can stop */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1905 ret = count;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1906 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1907 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1908 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1909 /* we did not get all the codec info, but we read too much data */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1910 if (read_size >= MAX_READ_SIZE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1911 ret = count;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1912 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1913 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1914
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1915 /* NOTE: a new stream can be added there if no header in file
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1916 (AVFMTCTX_NOHEADER) */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1917 ret = av_read_frame_internal(ic, &pkt1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1918 if (ret < 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1919 /* EOF or error */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1920 ret = -1; /* we could not have all the codec parameters before EOF */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1921 for(i=0;i<ic->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1922 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1923 if (!has_codec_parameters(st->codec)){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1924 char buf[256];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1925 avcodec_string(buf, sizeof(buf), st->codec, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1926 av_log(ic, AV_LOG_INFO, "Could not find codec parameters (%s)\n", buf);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1927 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1928 ret = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1929 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1930 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1931 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1932 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1933
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1934 pktl = av_mallocz(sizeof(AVPacketList));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1935 if (!pktl) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1936 ret = AVERROR_NOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1937 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1938 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1939
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1940 /* add the packet in the buffered packet list */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1941 *ppktl = pktl;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1942 ppktl = &pktl->next;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1943
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1944 pkt = &pktl->pkt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1945 *pkt = pkt1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1946
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1947 /* duplicate the packet */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1948 if (av_dup_packet(pkt) < 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1949 ret = AVERROR_NOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1950 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1951 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1952
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1953 read_size += pkt->size;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1954
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1955 st = ic->streams[pkt->stream_index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1956 st->codec_info_duration += pkt->duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1957 if (pkt->duration != 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1958 st->codec_info_nb_frames++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1959
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1960 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1961 int index= pkt->stream_index;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1962 int64_t last= last_dts[index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1963 int64_t duration= pkt->dts - last;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1964
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1965 if(pkt->dts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && duration>0){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1966 if(duration*duration_count[index]*10/9 < duration_sum[index]){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1967 duration_sum[index]= duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1968 duration_count[index]=1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1969 }else{
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1970 int factor= av_rescale(2*duration, duration_count[index], duration_sum[index]);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1971 if(factor==3)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1972 duration_count[index] *= 2;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1973 factor= av_rescale(duration, duration_count[index], duration_sum[index]);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1974 duration_sum[index] += duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1975 duration_count[index]+= factor;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1976 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1977 if(st->codec_info_nb_frames == 0 && 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1978 st->codec_info_duration += duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1979 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1980 last_dts[pkt->stream_index]= pkt->dts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1981 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1982 if(st->parser && st->parser->parser->split && !st->codec->extradata){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1983 int i= st->parser->parser->split(st->codec, pkt->data, pkt->size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1984 if(i){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1985 st->codec->extradata_size= i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1986 st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1987 memcpy(st->codec->extradata, pkt->data, st->codec->extradata_size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1988 memset(st->codec->extradata + i, 0, FF_INPUT_BUFFER_PADDING_SIZE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1989 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1990 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1991
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1992 /* if still no information, we try to open the codec and to
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1993 decompress the frame. We try to avoid that in most cases as
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1994 it takes longer and uses more memory. For MPEG4, we need to
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1995 decompress for Quicktime. */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1996 if (!has_codec_parameters(st->codec) /*&&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1997 (st->codec->codec_id == CODEC_ID_FLV1 ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1998 st->codec->codec_id == CODEC_ID_H264 ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1999 st->codec->codec_id == CODEC_ID_H263 ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2000 st->codec->codec_id == CODEC_ID_H261 ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2001 st->codec->codec_id == CODEC_ID_VORBIS ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2002 st->codec->codec_id == CODEC_ID_MJPEG ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2003 st->codec->codec_id == CODEC_ID_PNG ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2004 st->codec->codec_id == CODEC_ID_PAM ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2005 st->codec->codec_id == CODEC_ID_PGM ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2006 st->codec->codec_id == CODEC_ID_PGMYUV ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2007 st->codec->codec_id == CODEC_ID_PBM ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2008 st->codec->codec_id == CODEC_ID_PPM ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2009 st->codec->codec_id == CODEC_ID_SHORTEN ||
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2010 (st->codec->codec_id == CODEC_ID_MPEG4 && !st->need_parsing))*/)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2011 try_decode_frame(st, pkt->data, pkt->size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2012
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2013 if (av_rescale_q(st->codec_info_duration, st->time_base, AV_TIME_BASE_Q) >= MAX_STREAM_DURATION) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2014 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2015 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2016 count++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2017 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2018
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2019 // close codecs which where opened in try_decode_frame()
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2020 for(i=0;i<ic->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2021 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2022 if(st->codec->codec)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2023 avcodec_close(st->codec);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2024 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2025 for(i=0;i<ic->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2026 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2027 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2028 if(duration_count[i]
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2029 && (st->codec->time_base.num*101LL <= st->codec->time_base.den || st->codec->codec_id == CODEC_ID_MPEG2VIDEO) &&
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2030 //FIXME we should not special case mpeg2, but this needs testing with non mpeg2 ...
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2031 st->time_base.num*duration_sum[i]/duration_count[i]*101LL > st->time_base.den){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2032 int64_t num, den, error, best_error;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2033
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2034 num= st->time_base.den*duration_count[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2035 den= st->time_base.num*duration_sum[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2036
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2037 best_error= INT64_MAX;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2038 for(j=1; j<60*12; j++){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2039 error= FFABS(1001*12*num - 1001*j*den);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2040 if(error < best_error){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2041 best_error= error;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2042 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, j, 12, INT_MAX);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2043 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2044 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2045 for(j=0; j<3; j++){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2046 static const int ticks[]= {24,30,60};
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2047 error= FFABS(1001*12*num - 1000*12*den * ticks[j]);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2048 if(error < best_error){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2049 best_error= error;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2050 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, ticks[j]*1000, 1001, INT_MAX);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2051 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2052 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2053 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2054
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2055 if (!st->r_frame_rate.num){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2056 if( st->codec->time_base.den * (int64_t)st->time_base.num
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2057 <= st->codec->time_base.num * (int64_t)st->time_base.den){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2058 st->r_frame_rate.num = st->codec->time_base.den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2059 st->r_frame_rate.den = st->codec->time_base.num;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2060 }else{
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2061 st->r_frame_rate.num = st->time_base.den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2062 st->r_frame_rate.den = st->time_base.num;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2063 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2064 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2065 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2066 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2067
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2068 av_estimate_timings(ic);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2069 #if 0
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2070 /* correct DTS for b frame streams with no timestamps */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2071 for(i=0;i<ic->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2072 st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2073 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2074 if(b-frames){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2075 ppktl = &ic->packet_buffer;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2076 while(ppkt1){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2077 if(ppkt1->stream_index != i)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2078 continue;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2079 if(ppkt1->pkt->dts < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2080 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2081 if(ppkt1->pkt->pts != AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2082 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2083 ppkt1->pkt->dts -= delta;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2084 ppkt1= ppkt1->next;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2085 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2086 if(ppkt1)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2087 continue;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2088 st->cur_dts -= delta;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2089 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2090 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2091 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2092 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2093 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2094 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2095
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2096 /*******************************************************/
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2097
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2098 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2099 * start playing a network based stream (e.g. RTSP stream) at the
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2100 * current position
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2101 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2102 int av_read_play(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2103 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2104 if (!s->iformat->read_play)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2105 return AVERROR_NOTSUPP;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2106 return s->iformat->read_play(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2107 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2108
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2109 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2110 * Pause a network based stream (e.g. RTSP stream).
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2111 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2112 * Use av_read_play() to resume it.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2113 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2114 int av_read_pause(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2115 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2116 if (!s->iformat->read_pause)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2117 return AVERROR_NOTSUPP;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2118 return s->iformat->read_pause(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2119 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2120
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2121 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2122 * Close a media file (but not its codecs).
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2123 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2124 * @param s media file handle
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2125 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2126 void av_close_input_file(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2127 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2128 int i, must_open_file;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2129 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2130
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2131 /* free previous packet */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2132 if (s->cur_st && s->cur_st->parser)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2133 av_free_packet(&s->cur_pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2134
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2135 if (s->iformat->read_close)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2136 s->iformat->read_close(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2137 for(i=0;i<s->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2138 /* free all data in a stream component */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2139 st = s->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2140 if (st->parser) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2141 av_parser_close(st->parser);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2142 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2143 av_free(st->index_entries);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2144 av_free(st->codec->extradata);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2145 av_free(st->codec);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2146 av_free(st);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2147 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2148 flush_packet_queue(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2149 must_open_file = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2150 if (s->iformat->flags & AVFMT_NOFILE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2151 must_open_file = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2152 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2153 if (must_open_file) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2154 url_fclose(&s->pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2155 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2156 av_freep(&s->priv_data);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2157 av_free(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2158 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2159
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2160 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2161 * Add a new stream to a media file.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2162 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2163 * Can only be called in the read_header() function. If the flag
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2164 * AVFMTCTX_NOHEADER is in the format context, then new streams
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2165 * can be added in read_packet too.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2166 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2167 * @param s media file handle
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2168 * @param id file format dependent stream id
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2169 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2170 AVStream *av_new_stream(AVFormatContext *s, int id)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2171 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2172 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2173 int i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2174
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2175 if (s->nb_streams >= MAX_STREAMS)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2176 return NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2177
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2178 st = av_mallocz(sizeof(AVStream));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2179 if (!st)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2180 return NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2181
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2182 st->codec= avcodec_alloc_context();
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2183 if (s->iformat) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2184 /* no default bitrate if decoding */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2185 st->codec->bit_rate = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2186 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2187 st->index = s->nb_streams;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2188 st->id = id;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2189 st->start_time = AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2190 st->duration = AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2191 st->cur_dts = AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2192
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2193 /* default pts settings is MPEG like */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2194 av_set_pts_info(st, 33, 1, 90000);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2195 st->last_IP_pts = AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2196 for(i=0; i<MAX_REORDER_DELAY+1; i++)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2197 st->pts_buffer[i]= AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2198
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2199 s->streams[s->nb_streams++] = st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2200 return st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2201 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2202
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2203 /************************************************************/
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2204 /* output media file */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2205
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2206 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2207 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2208 int ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2209
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2210 if (s->oformat->priv_data_size > 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2211 s->priv_data = av_mallocz(s->oformat->priv_data_size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2212 if (!s->priv_data)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2213 return AVERROR_NOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2214 } else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2215 s->priv_data = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2216
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2217 if (s->oformat->set_parameters) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2218 ret = s->oformat->set_parameters(s, ap);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2219 if (ret < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2220 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2221 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2222 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2223 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2224
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2225 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2226 * allocate the stream private data and write the stream header to an
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2227 * output media file
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2228 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2229 * @param s media file handle
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2230 * @return 0 if OK. AVERROR_xxx if error.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2231 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2232 int av_write_header(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2233 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2234 int ret, i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2235 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2236
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2237 // some sanity checks
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2238 for(i=0;i<s->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2239 st = s->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2240
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2241 switch (st->codec->codec_type) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2242 case CODEC_TYPE_AUDIO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2243 if(st->codec->sample_rate<=0){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2244 av_log(s, AV_LOG_ERROR, "sample rate not set\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2245 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2246 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2247 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2248 case CODEC_TYPE_VIDEO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2249 if(st->codec->time_base.num<=0 || st->codec->time_base.den<=0){ //FIXME audio too?
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2250 av_log(s, AV_LOG_ERROR, "time base not set\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2251 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2252 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2253 if(st->codec->width<=0 || st->codec->height<=0){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2254 av_log(s, AV_LOG_ERROR, "dimensions not set\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2255 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2256 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2257 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2258 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2259 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2260
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2261 if(s->oformat->write_header){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2262 ret = s->oformat->write_header(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2263 if (ret < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2264 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2265 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2266
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2267 /* init PTS generation */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2268 for(i=0;i<s->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2269 int64_t den = AV_NOPTS_VALUE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2270 st = s->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2271
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2272 switch (st->codec->codec_type) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2273 case CODEC_TYPE_AUDIO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2274 den = (int64_t)st->time_base.num * st->codec->sample_rate;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2275 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2276 case CODEC_TYPE_VIDEO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2277 den = (int64_t)st->time_base.num * st->codec->time_base.den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2278 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2279 default:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2280 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2281 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2282 if (den != AV_NOPTS_VALUE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2283 if (den <= 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2284 return AVERROR_INVALIDDATA;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2285 av_frac_init(&st->pts, 0, 0, den);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2286 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2287 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2288 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2289 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2290
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2291 //FIXME merge with compute_pkt_fields
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2292 static int compute_pkt_fields2(AVStream *st, AVPacket *pkt){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2293 int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2294 int num, den, frame_size, i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2295
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2296 // av_log(NULL, AV_LOG_DEBUG, "av_write_frame: pts:%lld dts:%lld cur_dts:%lld b:%d size:%d st:%d\n", pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2297
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2298 /* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2299 return -1;*/
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2300
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2301 /* duration field */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2302 if (pkt->duration == 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2303 compute_frame_duration(&num, &den, st, NULL, pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2304 if (den && num) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2305 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2306 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2307 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2308
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2309 //XXX/FIXME this is a temporary hack until all encoders output pts
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2310 if((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2311 pkt->dts=
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2312 // pkt->pts= st->cur_dts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2313 pkt->pts= st->pts.val;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2314 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2315
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2316 //calculate dts from pts
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2317 if(pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2318 st->pts_buffer[0]= pkt->pts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2319 for(i=1; i<delay+1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2320 st->pts_buffer[i]= (i-delay-1) * pkt->duration;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2321 for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2322 SWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2323
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2324 pkt->dts= st->pts_buffer[0];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2325 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2326
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2327 if(st->cur_dts && st->cur_dts != AV_NOPTS_VALUE && st->cur_dts >= pkt->dts){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2328 av_log(NULL, AV_LOG_ERROR, "error, non monotone timestamps %"PRId64" >= %"PRId64"\n", st->cur_dts, pkt->dts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2329 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2330 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2331 if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2332 av_log(NULL, AV_LOG_ERROR, "error, pts < dts\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2333 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2334 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2335
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2336 // av_log(NULL, AV_LOG_DEBUG, "av_write_frame: pts2:%lld dts2:%lld\n", pkt->pts, pkt->dts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2337 st->cur_dts= pkt->dts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2338 st->pts.val= pkt->dts;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2339
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2340 /* update pts */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2341 switch (st->codec->codec_type) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2342 case CODEC_TYPE_AUDIO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2343 frame_size = get_audio_frame_size(st->codec, pkt->size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2344
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2345 /* HACK/FIXME, we skip the initial 0-size packets as they are most likely equal to the encoder delay,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2346 but it would be better if we had the real timestamps from the encoder */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2347 if (frame_size >= 0 && (pkt->size || st->pts.num!=st->pts.den>>1 || st->pts.val)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2348 av_frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2349 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2350 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2351 case CODEC_TYPE_VIDEO:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2352 av_frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2353 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2354 default:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2355 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2356 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2357 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2358 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2359
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2360 static void truncate_ts(AVStream *st, AVPacket *pkt){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2361 int64_t pts_mask = (2LL << (st->pts_wrap_bits-1)) - 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2362
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2363 // if(pkt->dts < 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2364 // pkt->dts= 0; //this happens for low_delay=0 and b frames, FIXME, needs further invstigation about what we should do here
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2365
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2366 pkt->pts &= pts_mask;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2367 pkt->dts &= pts_mask;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2368 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2369
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2370 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2371 * Write a packet to an output media file.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2372 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2373 * The packet shall contain one audio or video frame.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2374 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2375 * @param s media file handle
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2376 * @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ...
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2377 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2378 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2379 int av_write_frame(AVFormatContext *s, AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2380 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2381 int ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2382
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2383 ret=compute_pkt_fields2(s->streams[pkt->stream_index], pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2384 if(ret<0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2385 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2386
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2387 truncate_ts(s->streams[pkt->stream_index], pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2388
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2389 ret= s->oformat->write_packet(s, pkt);
830
68562d99230f [svn] - more symbol cleanups.
nenolod
parents: 825
diff changeset
2390 #if 0
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2391 if(!ret)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2392 ret= url_ferror(&s->pb);
830
68562d99230f [svn] - more symbol cleanups.
nenolod
parents: 825
diff changeset
2393 #endif
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2394 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2395 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2396
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2397 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2398 * Interleave a packet per DTS in an output media file.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2399 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2400 * Packets with pkt->destruct == av_destruct_packet will be freed inside this function,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2401 * so they cannot be used after it, note calling av_free_packet() on them is still safe.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2402 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2403 * @param s media file handle
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2404 * @param out the interleaved packet will be output here
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2405 * @param in the input packet
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2406 * @param flush 1 if no further packets are available as input and all
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2407 * remaining packets should be output
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2408 * @return 1 if a packet was output, 0 if no packet could be output,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2409 * < 0 if an error occured
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2410 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2411 int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2412 AVPacketList *pktl, **next_point, *this_pktl;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2413 int stream_count=0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2414 int streams[MAX_STREAMS];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2415
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2416 if(pkt){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2417 AVStream *st= s->streams[ pkt->stream_index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2418
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2419 // assert(pkt->destruct != av_destruct_packet); //FIXME
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2420
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2421 this_pktl = av_mallocz(sizeof(AVPacketList));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2422 this_pktl->pkt= *pkt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2423 if(pkt->destruct == av_destruct_packet)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2424 pkt->destruct= NULL; // non shared -> must keep original from being freed
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2425 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2426 av_dup_packet(&this_pktl->pkt); //shared -> must dup
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2427
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2428 next_point = &s->packet_buffer;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2429 while(*next_point){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2430 AVStream *st2= s->streams[ (*next_point)->pkt.stream_index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2431 int64_t left= st2->time_base.num * (int64_t)st ->time_base.den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2432 int64_t right= st ->time_base.num * (int64_t)st2->time_base.den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2433 if((*next_point)->pkt.dts * left > pkt->dts * right) //FIXME this can overflow
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2434 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2435 next_point= &(*next_point)->next;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2436 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2437 this_pktl->next= *next_point;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2438 *next_point= this_pktl;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2439 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2440
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2441 memset(streams, 0, sizeof(streams));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2442 pktl= s->packet_buffer;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2443 while(pktl){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2444 //av_log(s, AV_LOG_DEBUG, "show st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2445 if(streams[ pktl->pkt.stream_index ] == 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2446 stream_count++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2447 streams[ pktl->pkt.stream_index ]++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2448 pktl= pktl->next;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2449 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2450
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2451 if(s->nb_streams == stream_count || (flush && stream_count)){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2452 pktl= s->packet_buffer;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2453 *out= pktl->pkt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2454
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2455 s->packet_buffer= pktl->next;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2456 av_freep(&pktl);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2457 return 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2458 }else{
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2459 av_init_packet(out);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2460 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2461 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2462 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2463
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2464 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2465 * Interleaves a AVPacket correctly so it can be muxed.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2466 * @param out the interleaved packet will be output here
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2467 * @param in the input packet
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2468 * @param flush 1 if no further packets are available as input and all
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2469 * remaining packets should be output
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2470 * @return 1 if a packet was output, 0 if no packet could be output,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2471 * < 0 if an error occured
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2472 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2473 static int av_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2474 if(s->oformat->interleave_packet)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2475 return s->oformat->interleave_packet(s, out, in, flush);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2476 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2477 return av_interleave_packet_per_dts(s, out, in, flush);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2478 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2479
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2480 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2481 * Writes a packet to an output media file ensuring correct interleaving.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2482 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2483 * The packet must contain one audio or video frame.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2484 * If the packets are already correctly interleaved the application should
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2485 * call av_write_frame() instead as its slightly faster, its also important
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2486 * to keep in mind that completly non interleaved input will need huge amounts
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2487 * of memory to interleave with this, so its prefereable to interleave at the
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2488 * demuxer level
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2489 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2490 * @param s media file handle
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2491 * @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ...
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2492 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2493 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2494 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2495 AVStream *st= s->streams[ pkt->stream_index];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2496
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2497 //FIXME/XXX/HACK drop zero sized packets
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2498 if(st->codec->codec_type == CODEC_TYPE_AUDIO && pkt->size==0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2499 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2500
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2501 //av_log(NULL, AV_LOG_DEBUG, "av_interleaved_write_frame %d %Ld %Ld\n", pkt->size, pkt->dts, pkt->pts);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2502 if(compute_pkt_fields2(st, pkt) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2503 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2504
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2505 if(pkt->dts == AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2506 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2507
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2508 for(;;){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2509 AVPacket opkt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2510 int ret= av_interleave_packet(s, &opkt, pkt, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2511 if(ret<=0) //FIXME cleanup needed for ret<0 ?
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2512 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2513
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2514 truncate_ts(s->streams[opkt.stream_index], &opkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2515 ret= s->oformat->write_packet(s, &opkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2516
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2517 av_free_packet(&opkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2518 pkt= NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2519
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2520 if(ret<0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2521 return ret;
830
68562d99230f [svn] - more symbol cleanups.
nenolod
parents: 825
diff changeset
2522 #if 0
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2523 if(url_ferror(&s->pb))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2524 return url_ferror(&s->pb);
830
68562d99230f [svn] - more symbol cleanups.
nenolod
parents: 825
diff changeset
2525 #endif
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2526 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2527 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2528
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2529 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2530 * @brief Write the stream trailer to an output media file and
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2531 * free the file private data.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2532 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2533 * @param s media file handle
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2534 * @return 0 if OK. AVERROR_xxx if error.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2535 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2536 int av_write_trailer(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2537 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2538 int ret, i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2539
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2540 for(;;){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2541 AVPacket pkt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2542 ret= av_interleave_packet(s, &pkt, NULL, 1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2543 if(ret<0) //FIXME cleanup needed for ret<0 ?
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2544 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2545 if(!ret)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2546 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2547
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2548 truncate_ts(s->streams[pkt.stream_index], &pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2549 ret= s->oformat->write_packet(s, &pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2550
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2551 av_free_packet(&pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2552
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2553 if(ret<0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2554 goto fail;
830
68562d99230f [svn] - more symbol cleanups.
nenolod
parents: 825
diff changeset
2555 #if 0
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2556 if(url_ferror(&s->pb))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2557 goto fail;
830
68562d99230f [svn] - more symbol cleanups.
nenolod
parents: 825
diff changeset
2558 #endif
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2559 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2560
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2561 if(s->oformat->write_trailer)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2562 ret = s->oformat->write_trailer(s);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2563 fail:
830
68562d99230f [svn] - more symbol cleanups.
nenolod
parents: 825
diff changeset
2564 #if 0
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2565 if(ret == 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2566 ret=url_ferror(&s->pb);
830
68562d99230f [svn] - more symbol cleanups.
nenolod
parents: 825
diff changeset
2567 #endif
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2568 for(i=0;i<s->nb_streams;i++)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2569 av_freep(&s->streams[i]->priv_data);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2570 av_freep(&s->priv_data);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2571 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2572 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2573
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2574 /* "user interface" functions */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2575
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2576 void dump_format(AVFormatContext *ic,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2577 int index,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2578 const char *url,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2579 int is_output)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2580 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2581 int i, flags;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2582 char buf[256];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2583
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2584 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2585 is_output ? "Output" : "Input",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2586 index,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2587 is_output ? ic->oformat->name : ic->iformat->name,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2588 is_output ? "to" : "from", url);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2589 if (!is_output) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2590 av_log(NULL, AV_LOG_INFO, " Duration: ");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2591 if (ic->duration != AV_NOPTS_VALUE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2592 int hours, mins, secs, us;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2593 secs = ic->duration / AV_TIME_BASE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2594 us = ic->duration % AV_TIME_BASE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2595 mins = secs / 60;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2596 secs %= 60;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2597 hours = mins / 60;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2598 mins %= 60;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2599 av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%01d", hours, mins, secs,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2600 (10 * us) / AV_TIME_BASE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2601 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2602 av_log(NULL, AV_LOG_INFO, "N/A");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2603 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2604 if (ic->start_time != AV_NOPTS_VALUE) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2605 int secs, us;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2606 av_log(NULL, AV_LOG_INFO, ", start: ");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2607 secs = ic->start_time / AV_TIME_BASE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2608 us = ic->start_time % AV_TIME_BASE;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2609 av_log(NULL, AV_LOG_INFO, "%d.%06d",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2610 secs, (int)av_rescale(us, 1000000, AV_TIME_BASE));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2611 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2612 av_log(NULL, AV_LOG_INFO, ", bitrate: ");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2613 if (ic->bit_rate) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2614 av_log(NULL, AV_LOG_INFO,"%d kb/s", ic->bit_rate / 1000);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2615 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2616 av_log(NULL, AV_LOG_INFO, "N/A");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2617 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2618 av_log(NULL, AV_LOG_INFO, "\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2619 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2620 for(i=0;i<ic->nb_streams;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2621 AVStream *st = ic->streams[i];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2622 int g= ff_gcd(st->time_base.num, st->time_base.den);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2623 avcodec_string(buf, sizeof(buf), st->codec, is_output);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2624 av_log(NULL, AV_LOG_INFO, " Stream #%d.%d", index, i);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2625 /* the pid is an important information, so we display it */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2626 /* XXX: add a generic system */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2627 if (is_output)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2628 flags = ic->oformat->flags;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2629 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2630 flags = ic->iformat->flags;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2631 if (flags & AVFMT_SHOW_IDS) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2632 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2633 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2634 if (strlen(st->language) > 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2635 av_log(NULL, AV_LOG_INFO, "(%s)", st->language);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2636 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2637 av_log(NULL, AV_LOG_DEBUG, ", %d/%d", st->time_base.num/g, st->time_base.den/g);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2638 av_log(NULL, AV_LOG_INFO, ": %s", buf);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2639 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2640 if(st->r_frame_rate.den && st->r_frame_rate.num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2641 av_log(NULL, AV_LOG_INFO, ", %5.2f fps(r)", av_q2d(st->r_frame_rate));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2642 /* else if(st->time_base.den && st->time_base.num)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2643 av_log(NULL, AV_LOG_INFO, ", %5.2f fps(m)", 1/av_q2d(st->time_base));*/
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2644 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2645 av_log(NULL, AV_LOG_INFO, ", %5.2f fps(c)", 1/av_q2d(st->codec->time_base));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2646 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2647 av_log(NULL, AV_LOG_INFO, "\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2648 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2649 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2650
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2651 typedef struct {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2652 const char *abv;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2653 int width, height;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2654 int frame_rate, frame_rate_base;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2655 } AbvEntry;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2656
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2657 static AbvEntry frame_abvs[] = {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2658 { "ntsc", 720, 480, 30000, 1001 },
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2659 { "pal", 720, 576, 25, 1 },
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2660 { "qntsc", 352, 240, 30000, 1001 }, /* VCD compliant ntsc */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2661 { "qpal", 352, 288, 25, 1 }, /* VCD compliant pal */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2662 { "sntsc", 640, 480, 30000, 1001 }, /* square pixel ntsc */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2663 { "spal", 768, 576, 25, 1 }, /* square pixel pal */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2664 { "film", 352, 240, 24, 1 },
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2665 { "ntsc-film", 352, 240, 24000, 1001 },
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2666 { "sqcif", 128, 96, 0, 0 },
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2667 { "qcif", 176, 144, 0, 0 },
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2668 { "cif", 352, 288, 0, 0 },
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2669 { "4cif", 704, 576, 0, 0 },
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2670 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2671
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2672 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2673 * parses width and height out of string str.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2674 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2675 int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2676 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2677 int i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2678 int n = sizeof(frame_abvs) / sizeof(AbvEntry);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2679 const char *p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2680 int frame_width = 0, frame_height = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2681
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2682 for(i=0;i<n;i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2683 if (!strcmp(frame_abvs[i].abv, str)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2684 frame_width = frame_abvs[i].width;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2685 frame_height = frame_abvs[i].height;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2686 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2687 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2688 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2689 if (i == n) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2690 p = str;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2691 frame_width = strtol(p, (char **)&p, 10);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2692 if (*p)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2693 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2694 frame_height = strtol(p, (char **)&p, 10);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2695 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2696 if (frame_width <= 0 || frame_height <= 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2697 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2698 *width_ptr = frame_width;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2699 *height_ptr = frame_height;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2700 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2701 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2702
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2703 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2704 * Converts frame rate from string to a fraction.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2705 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2706 * First we try to get an exact integer or fractional frame rate.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2707 * If this fails we convert the frame rate to a double and return
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2708 * an approximate fraction using the DEFAULT_FRAME_RATE_BASE.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2709 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2710 int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2711 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2712 int i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2713 char* cp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2714
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2715 /* First, we check our abbreviation table */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2716 for (i = 0; i < sizeof(frame_abvs)/sizeof(*frame_abvs); ++i)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2717 if (!strcmp(frame_abvs[i].abv, arg)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2718 *frame_rate = frame_abvs[i].frame_rate;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2719 *frame_rate_base = frame_abvs[i].frame_rate_base;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2720 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2721 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2722
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2723 /* Then, we try to parse it as fraction */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2724 cp = strchr(arg, '/');
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2725 if (!cp)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2726 cp = strchr(arg, ':');
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2727 if (cp) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2728 char* cpp;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2729 *frame_rate = strtol(arg, &cpp, 10);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2730 if (cpp != arg || cpp == cp)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2731 *frame_rate_base = strtol(cp+1, &cpp, 10);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2732 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2733 *frame_rate = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2734 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2735 else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2736 /* Finally we give up and parse it as double */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2737 AVRational time_base = av_d2q(strtod(arg, 0), DEFAULT_FRAME_RATE_BASE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2738 *frame_rate_base = time_base.den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2739 *frame_rate = time_base.num;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2740 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2741 if (!*frame_rate || !*frame_rate_base)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2742 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2743 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2744 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2745 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2746
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2747 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2748 * Converts date string to number of seconds since Jan 1st, 1970.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2749 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2750 * @code
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2751 * Syntax:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2752 * - If not a duration:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2753 * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]}
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2754 * Time is localtime unless Z is suffixed to the end. In this case GMT
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2755 * Return the date in micro seconds since 1970
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2756 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2757 * - If a duration:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2758 * HH[:MM[:SS[.m...]]]
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2759 * S+[.m...]
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2760 * @endcode
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2761 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2762 #ifndef CONFIG_WINCE
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2763 int64_t parse_date(const char *datestr, int duration)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2764 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2765 const char *p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2766 int64_t t;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2767 struct tm dt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2768 int i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2769 static const char *date_fmt[] = {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2770 "%Y-%m-%d",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2771 "%Y%m%d",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2772 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2773 static const char *time_fmt[] = {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2774 "%H:%M:%S",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2775 "%H%M%S",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2776 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2777 const char *q;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2778 int is_utc, len;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2779 char lastch;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2780 int negative = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2781
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2782 #undef time
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2783 time_t now = time(0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2784
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2785 len = strlen(datestr);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2786 if (len > 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2787 lastch = datestr[len - 1];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2788 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2789 lastch = '\0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2790 is_utc = (lastch == 'z' || lastch == 'Z');
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2791
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2792 memset(&dt, 0, sizeof(dt));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2793
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2794 p = datestr;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2795 q = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2796 if (!duration) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2797 for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2798 q = small_strptime(p, date_fmt[i], &dt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2799 if (q) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2800 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2801 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2802 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2803
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2804 if (!q) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2805 if (is_utc) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2806 dt = *gmtime(&now);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2807 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2808 dt = *localtime(&now);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2809 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2810 dt.tm_hour = dt.tm_min = dt.tm_sec = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2811 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2812 p = q;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2813 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2814
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2815 if (*p == 'T' || *p == 't' || *p == ' ')
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2816 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2817
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2818 for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2819 q = small_strptime(p, time_fmt[i], &dt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2820 if (q) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2821 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2822 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2823 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2824 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2825 if (p[0] == '-') {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2826 negative = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2827 ++p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2828 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2829 q = small_strptime(p, time_fmt[0], &dt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2830 if (!q) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2831 dt.tm_sec = strtol(p, (char **)&q, 10);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2832 dt.tm_min = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2833 dt.tm_hour = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2834 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2835 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2836
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2837 /* Now we have all the fields that we can get */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2838 if (!q) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2839 if (duration)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2840 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2841 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2842 return now * int64_t_C(1000000);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2843 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2844
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2845 if (duration) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2846 t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2847 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2848 dt.tm_isdst = -1; /* unknown */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2849 if (is_utc) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2850 t = mktimegm(&dt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2851 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2852 t = mktime(&dt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2853 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2854 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2855
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2856 t *= 1000000;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2857
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2858 if (*q == '.') {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2859 int val, n;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2860 q++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2861 for (val = 0, n = 100000; n >= 1; n /= 10, q++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2862 if (!isdigit(*q))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2863 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2864 val += n * (*q - '0');
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2865 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2866 t += val;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2867 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2868 return negative ? -t : t;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2869 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2870 #endif /* CONFIG_WINCE */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2871
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2872 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2873 * Attempts to find a specific tag in a URL.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2874 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2875 * syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2876 * Return 1 if found.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2877 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2878 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2879 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2880 const char *p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2881 char tag[128], *q;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2882
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2883 p = info;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2884 if (*p == '?')
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2885 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2886 for(;;) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2887 q = tag;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2888 while (*p != '\0' && *p != '=' && *p != '&') {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2889 if ((q - tag) < sizeof(tag) - 1)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2890 *q++ = *p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2891 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2892 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2893 *q = '\0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2894 q = arg;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2895 if (*p == '=') {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2896 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2897 while (*p != '&' && *p != '\0') {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2898 if ((q - arg) < arg_size - 1) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2899 if (*p == '+')
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2900 *q++ = ' ';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2901 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2902 *q++ = *p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2903 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2904 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2905 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2906 *q = '\0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2907 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2908 if (!strcmp(tag, tag1))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2909 return 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2910 if (*p != '&')
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2911 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2912 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2913 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2914 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2915 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2916
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2917 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2918 * Returns in 'buf' the path with '%d' replaced by number.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2919
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2920 * Also handles the '%0nd' format where 'n' is the total number
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2921 * of digits and '%%'.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2922 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2923 * @param buf destination buffer
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2924 * @param buf_size destination buffer size
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2925 * @param path numbered sequence string
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2926 * @number frame number
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2927 * @return 0 if OK, -1 if format error.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2928 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2929 int av_get_frame_filename(char *buf, int buf_size,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2930 const char *path, int number)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2931 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2932 const char *p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2933 char *q, buf1[20], c;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2934 int nd, len, percentd_found;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2935
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2936 q = buf;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2937 p = path;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2938 percentd_found = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2939 for(;;) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2940 c = *p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2941 if (c == '\0')
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2942 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2943 if (c == '%') {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2944 do {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2945 nd = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2946 while (isdigit(*p)) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2947 nd = nd * 10 + *p++ - '0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2948 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2949 c = *p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2950 } while (isdigit(c));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2951
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2952 switch(c) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2953 case '%':
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2954 goto addchar;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2955 case 'd':
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2956 if (percentd_found)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2957 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2958 percentd_found = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2959 snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2960 len = strlen(buf1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2961 if ((q - buf + len) > buf_size - 1)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2962 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2963 memcpy(q, buf1, len);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2964 q += len;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2965 break;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2966 default:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2967 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2968 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2969 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2970 addchar:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2971 if ((q - buf) < buf_size - 1)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2972 *q++ = c;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2973 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2974 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2975 if (!percentd_found)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2976 goto fail;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2977 *q = '\0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2978 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2979 fail:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2980 *q = '\0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2981 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2982 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2983
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2984 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2985 * Print nice hexa dump of a buffer
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2986 * @param f stream for output
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2987 * @param buf buffer
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2988 * @param size buffer size
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2989 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2990 void av_hex_dump(FILE *f, uint8_t *buf, int size)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2991 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2992 int len, i, j, c;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2993
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2994 for(i=0;i<size;i+=16) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2995 len = size - i;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2996 if (len > 16)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2997 len = 16;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2998 fprintf(f, "%08x ", i);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2999 for(j=0;j<16;j++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3000 if (j < len)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3001 fprintf(f, " %02x", buf[i+j]);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3002 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3003 fprintf(f, " ");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3004 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3005 fprintf(f, " ");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3006 for(j=0;j<len;j++) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3007 c = buf[i+j];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3008 if (c < ' ' || c > '~')
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3009 c = '.';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3010 fprintf(f, "%c", c);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3011 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3012 fprintf(f, "\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3013 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3014 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3015
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3016 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3017 * Print on 'f' a nice dump of a packet
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3018 * @param f stream for output
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3019 * @param pkt packet to dump
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3020 * @param dump_payload true if the payload must be displayed too
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3021 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3022 //FIXME needs to know the time_base
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3023 void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3024 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3025 fprintf(f, "stream #%d:\n", pkt->stream_index);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3026 fprintf(f, " keyframe=%d\n", ((pkt->flags & PKT_FLAG_KEY) != 0));
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3027 fprintf(f, " duration=%0.3f\n", (double)pkt->duration / AV_TIME_BASE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3028 /* DTS is _always_ valid after av_read_frame() */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3029 fprintf(f, " dts=");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3030 if (pkt->dts == AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3031 fprintf(f, "N/A");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3032 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3033 fprintf(f, "%0.3f", (double)pkt->dts / AV_TIME_BASE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3034 /* PTS may be not known if B frames are present */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3035 fprintf(f, " pts=");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3036 if (pkt->pts == AV_NOPTS_VALUE)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3037 fprintf(f, "N/A");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3038 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3039 fprintf(f, "%0.3f", (double)pkt->pts / AV_TIME_BASE);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3040 fprintf(f, "\n");
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3041 fprintf(f, " size=%d\n", pkt->size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3042 if (dump_payload)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3043 av_hex_dump(f, pkt->data, pkt->size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3044 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3045
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3046 void url_split(char *proto, int proto_size,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3047 char *authorization, int authorization_size,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3048 char *hostname, int hostname_size,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3049 int *port_ptr,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3050 char *path, int path_size,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3051 const char *url)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3052 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3053 const char *p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3054 char *q;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3055 int port;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3056
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3057 port = -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3058
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3059 p = url;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3060 q = proto;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3061 while (*p != ':' && *p != '\0') {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3062 if ((q - proto) < proto_size - 1)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3063 *q++ = *p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3064 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3065 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3066 if (proto_size > 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3067 *q = '\0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3068 if (authorization_size > 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3069 authorization[0] = '\0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3070 if (*p == '\0') {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3071 if (proto_size > 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3072 proto[0] = '\0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3073 if (hostname_size > 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3074 hostname[0] = '\0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3075 p = url;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3076 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3077 char *at,*slash; // PETR: position of '@' character and '/' character
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3078
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3079 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3080 if (*p == '/')
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3081 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3082 if (*p == '/')
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3083 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3084 at = strchr(p,'@'); // PETR: get the position of '@'
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3085 slash = strchr(p,'/'); // PETR: get position of '/' - end of hostname
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3086 if (at && slash && at > slash) at = NULL; // PETR: not interested in '@' behind '/'
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3087
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3088 q = at ? authorization : hostname; // PETR: if '@' exists starting with auth.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3089
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3090 while ((at || *p != ':') && *p != '/' && *p != '?' && *p != '\0') { // PETR:
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3091 if (*p == '@') { // PETR: passed '@'
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3092 if (authorization_size > 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3093 *q = '\0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3094 q = hostname;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3095 at = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3096 } else if (!at) { // PETR: hostname
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3097 if ((q - hostname) < hostname_size - 1)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3098 *q++ = *p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3099 } else {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3100 if ((q - authorization) < authorization_size - 1)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3101 *q++ = *p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3102 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3103 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3104 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3105 if (hostname_size > 0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3106 *q = '\0';
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3107 if (*p == ':') {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3108 p++;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3109 port = strtoul(p, (char **)&p, 10);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3110 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3111 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3112 if (port_ptr)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3113 *port_ptr = port;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3114 pstrcpy(path, path_size, p);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3115 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3116
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3117 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3118 * Set the pts for a given stream.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3119 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3120 * @param s stream
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3121 * @param pts_wrap_bits number of bits effectively used by the pts
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3122 * (used for wrap control, 33 is the value for MPEG)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3123 * @param pts_num numerator to convert to seconds (MPEG: 1)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3124 * @param pts_den denominator to convert to seconds (MPEG: 90000)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3125 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3126 void av_set_pts_info(AVStream *s, int pts_wrap_bits,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3127 int pts_num, int pts_den)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3128 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3129 s->pts_wrap_bits = pts_wrap_bits;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3130 s->time_base.num = pts_num;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3131 s->time_base.den = pts_den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3132 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3133
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3134 /* fraction handling */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3135
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3136 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3137 * f = val + (num / den) + 0.5.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3138 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3139 * 'num' is normalized so that it is such as 0 <= num < den.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3140 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3141 * @param f fractional number
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3142 * @param val integer value
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3143 * @param num must be >= 0
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3144 * @param den must be >= 1
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3145 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3146 static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3147 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3148 num += (den >> 1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3149 if (num >= den) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3150 val += num / den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3151 num = num % den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3152 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3153 f->val = val;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3154 f->num = num;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3155 f->den = den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3156 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3157
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3158 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3159 * Set f to (val + 0.5).
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3160 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3161 static void av_frac_set(AVFrac *f, int64_t val)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3162 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3163 f->val = val;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3164 f->num = f->den >> 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3165 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3166
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3167 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3168 * Fractionnal addition to f: f = f + (incr / f->den).
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3169 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3170 * @param f fractional number
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3171 * @param incr increment, can be positive or negative
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3172 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3173 static void av_frac_add(AVFrac *f, int64_t incr)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3174 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3175 int64_t num, den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3176
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3177 num = f->num + incr;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3178 den = f->den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3179 if (num < 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3180 f->val += num / den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3181 num = num % den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3182 if (num < 0) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3183 num += den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3184 f->val--;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3185 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3186 } else if (num >= den) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3187 f->val += num / den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3188 num = num % den;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3189 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3190 f->num = num;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3191 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3192
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3193 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3194 * register a new image format
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3195 * @param img_fmt Image format descriptor
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3196 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3197 void av_register_image_format(AVImageFormat *img_fmt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3198 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3199 AVImageFormat **p;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3200
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3201 p = &first_image_format;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3202 while (*p != NULL) p = &(*p)->next;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3203 *p = img_fmt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3204 img_fmt->next = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3205 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3206
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3207 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3208 * Guesses image format based on data in the image.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3209 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3210 AVImageFormat *av_probe_image_format(AVProbeData *pd)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3211 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3212 AVImageFormat *fmt1, *fmt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3213 int score, score_max;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3214
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3215 fmt = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3216 score_max = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3217 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3218 if (fmt1->img_probe) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3219 score = fmt1->img_probe(pd);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3220 if (score > score_max) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3221 score_max = score;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3222 fmt = fmt1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3223 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3224 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3225 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3226 return fmt;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3227 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3228
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3229 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3230 * Guesses image format based on file name extensions.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3231 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3232 AVImageFormat *guess_image_format(const char *filename)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3233 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3234 AVImageFormat *fmt1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3235
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3236 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3237 if (fmt1->extensions && match_ext(filename, fmt1->extensions))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3238 return fmt1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3239 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3240 return NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3241 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3242
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3243 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3244 * Read an image from a stream.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3245 * @param gb byte stream containing the image
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3246 * @param fmt image format, NULL if probing is required
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3247 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3248 int av_read_image(ByteIOContext *pb, const char *filename,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3249 AVImageFormat *fmt,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3250 int (*alloc_cb)(void *, AVImageInfo *info), void *opaque)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3251 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3252 uint8_t buf[PROBE_BUF_MIN];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3253 AVProbeData probe_data, *pd = &probe_data;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3254 offset_t pos;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3255 int ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3256
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3257 if (!fmt) {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3258 pd->filename = filename;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3259 pd->buf = buf;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3260 pos = url_ftell(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3261 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_MIN);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3262 url_fseek(pb, pos, SEEK_SET);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3263 fmt = av_probe_image_format(pd);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3264 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3265 if (!fmt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3266 return AVERROR_NOFMT;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3267 ret = fmt->img_read(pb, alloc_cb, opaque);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3268 return ret;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3269 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3270
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3271 /**
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3272 * Write an image to a stream.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3273 * @param pb byte stream for the image output
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3274 * @param fmt image format
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3275 * @param img image data and informations
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3276 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3277 int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3278 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3279 return fmt->img_write(pb, img);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3280 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3281