Mercurial > libavformat.hg
annotate utils.c @ 333:e3beadc2ed83 libavformat
make av_read_frame() return buffered frames at EOF
| author | bellard |
|---|---|
| date | Mon, 15 Dec 2003 14:45:37 +0000 |
| parents | c393ab803bcd |
| children | 7f089db11f9a |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * Various utilities for ffmpeg system | |
| 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard | |
| 4 * | |
| 5 * This library is free software; you can redistribute it and/or | |
| 6 * modify it under the terms of the GNU Lesser General Public | |
| 7 * License as published by the Free Software Foundation; either | |
| 8 * version 2 of the License, or (at your option) any later version. | |
| 9 * | |
| 10 * This library is distributed in the hope that it will be useful, | |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 * Lesser General Public License for more details. | |
| 14 * | |
| 15 * You should have received a copy of the GNU Lesser General Public | |
| 16 * License along with this library; if not, write to the Free Software | |
| 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 */ | |
| 19 #include "avformat.h" | |
| 20 | |
| 21 AVInputFormat *first_iformat; | |
| 22 AVOutputFormat *first_oformat; | |
| 20 | 23 AVImageFormat *first_image_format; |
| 0 | 24 |
| 25 void av_register_input_format(AVInputFormat *format) | |
| 26 { | |
| 27 AVInputFormat **p; | |
| 28 p = &first_iformat; | |
| 29 while (*p != NULL) p = &(*p)->next; | |
| 30 *p = format; | |
| 31 format->next = NULL; | |
| 32 } | |
| 33 | |
| 34 void av_register_output_format(AVOutputFormat *format) | |
| 35 { | |
| 36 AVOutputFormat **p; | |
| 37 p = &first_oformat; | |
| 38 while (*p != NULL) p = &(*p)->next; | |
| 39 *p = format; | |
| 40 format->next = NULL; | |
| 41 } | |
| 42 | |
| 43 int match_ext(const char *filename, const char *extensions) | |
| 44 { | |
| 45 const char *ext, *p; | |
| 46 char ext1[32], *q; | |
| 47 | |
| 48 ext = strrchr(filename, '.'); | |
| 49 if (ext) { | |
| 50 ext++; | |
| 51 p = extensions; | |
| 52 for(;;) { | |
| 53 q = ext1; | |
| 54 while (*p != '\0' && *p != ',') | |
| 55 *q++ = *p++; | |
| 56 *q = '\0'; | |
| 57 if (!strcasecmp(ext1, ext)) | |
| 58 return 1; | |
| 59 if (*p == '\0') | |
| 60 break; | |
| 61 p++; | |
| 62 } | |
| 63 } | |
| 64 return 0; | |
| 65 } | |
| 66 | |
| 67 AVOutputFormat *guess_format(const char *short_name, const char *filename, | |
| 68 const char *mime_type) | |
| 69 { | |
| 70 AVOutputFormat *fmt, *fmt_found; | |
| 71 int score_max, score; | |
| 72 | |
| 20 | 73 /* specific test for image sequences */ |
| 21 | 74 if (!short_name && filename && |
| 75 filename_number_test(filename) >= 0 && | |
| 76 guess_image_format(filename)) { | |
| 20 | 77 return guess_format("image", NULL, NULL); |
| 78 } | |
| 79 | |
| 0 | 80 /* find the proper file type */ |
| 81 fmt_found = NULL; | |
| 82 score_max = 0; | |
| 83 fmt = first_oformat; | |
| 84 while (fmt != NULL) { | |
| 85 score = 0; | |
| 86 if (fmt->name && short_name && !strcmp(fmt->name, short_name)) | |
| 87 score += 100; | |
| 88 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type)) | |
| 89 score += 10; | |
| 90 if (filename && fmt->extensions && | |
| 91 match_ext(filename, fmt->extensions)) { | |
| 92 score += 5; | |
| 93 } | |
| 94 if (score > score_max) { | |
| 95 score_max = score; | |
| 96 fmt_found = fmt; | |
| 97 } | |
| 98 fmt = fmt->next; | |
| 99 } | |
| 100 return fmt_found; | |
| 101 } | |
| 102 | |
| 103 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename, | |
| 104 const char *mime_type) | |
| 105 { | |
| 106 AVOutputFormat *fmt = guess_format(short_name, filename, mime_type); | |
| 107 | |
| 108 if (fmt) { | |
| 109 AVOutputFormat *stream_fmt; | |
| 110 char stream_format_name[64]; | |
| 111 | |
| 112 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name); | |
| 113 stream_fmt = guess_format(stream_format_name, NULL, NULL); | |
| 114 | |
| 115 if (stream_fmt) | |
| 116 fmt = stream_fmt; | |
| 117 } | |
| 118 | |
| 119 return fmt; | |
| 120 } | |
| 121 | |
| 122 AVInputFormat *av_find_input_format(const char *short_name) | |
| 123 { | |
| 124 AVInputFormat *fmt; | |
| 125 for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) { | |
| 126 if (!strcmp(fmt->name, short_name)) | |
| 127 return fmt; | |
| 128 } | |
| 129 return NULL; | |
| 130 } | |
| 131 | |
| 132 /* memory handling */ | |
| 133 | |
| 134 /** | |
|
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
135 * Default packet destructor |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
136 */ |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
137 static void av_destruct_packet(AVPacket *pkt) |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
138 { |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
139 av_free(pkt->data); |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
140 pkt->data = NULL; pkt->size = 0; |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
141 } |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
142 |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
143 /** |
| 0 | 144 * Allocate the payload of a packet and intialized its fields to default values. |
| 145 * | |
| 146 * @param pkt packet | |
| 147 * @param size wanted payload size | |
| 148 * @return 0 if OK. AVERROR_xxx otherwise. | |
| 149 */ | |
| 150 int av_new_packet(AVPacket *pkt, int size) | |
| 151 { | |
|
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
152 void *data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
153 if (!data) |
| 0 | 154 return AVERROR_NOMEM; |
|
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
155 memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
| 0 | 156 |
|
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
157 av_init_packet(pkt); |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
158 pkt->data = data; |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
159 pkt->size = size; |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
160 pkt->destruct = av_destruct_packet; |
| 0 | 161 return 0; |
| 162 } | |
| 163 | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
164 /* This is a hack - the packet memory allocation stuff is broken. The |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
165 packet is allocated if it was not really allocated */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
166 int av_dup_packet(AVPacket *pkt) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
167 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
168 if (pkt->destruct != av_destruct_packet) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
169 uint8_t *data; |
| 330 | 170 /* we duplicate the packet and don't forget to put the padding |
| 171 again */ | |
| 172 data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE); | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
173 if (!data) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
174 return AVERROR_NOMEM; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
175 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
176 memcpy(data, pkt->data, pkt->size); |
| 330 | 177 memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
178 pkt->data = data; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
179 pkt->destruct = av_destruct_packet; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
180 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
181 return 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
182 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
183 |
| 0 | 184 /* fifo handling */ |
| 185 | |
| 186 int fifo_init(FifoBuffer *f, int size) | |
| 187 { | |
| 188 f->buffer = av_malloc(size); | |
| 189 if (!f->buffer) | |
| 190 return -1; | |
| 191 f->end = f->buffer + size; | |
| 192 f->wptr = f->rptr = f->buffer; | |
| 193 return 0; | |
| 194 } | |
| 195 | |
| 196 void fifo_free(FifoBuffer *f) | |
| 197 { | |
| 198 av_free(f->buffer); | |
| 199 } | |
| 200 | |
| 65 | 201 int fifo_size(FifoBuffer *f, uint8_t *rptr) |
| 0 | 202 { |
| 203 int size; | |
| 204 | |
| 205 if (f->wptr >= rptr) { | |
| 206 size = f->wptr - rptr; | |
| 207 } else { | |
| 208 size = (f->end - rptr) + (f->wptr - f->buffer); | |
| 209 } | |
| 210 return size; | |
| 211 } | |
| 212 | |
| 213 /* get data from the fifo (return -1 if not enough data) */ | |
| 65 | 214 int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr) |
| 0 | 215 { |
| 65 | 216 uint8_t *rptr = *rptr_ptr; |
| 0 | 217 int size, len; |
| 218 | |
| 219 if (f->wptr >= rptr) { | |
| 220 size = f->wptr - rptr; | |
| 221 } else { | |
| 222 size = (f->end - rptr) + (f->wptr - f->buffer); | |
| 223 } | |
| 224 | |
| 225 if (size < buf_size) | |
| 226 return -1; | |
| 227 while (buf_size > 0) { | |
| 228 len = f->end - rptr; | |
| 229 if (len > buf_size) | |
| 230 len = buf_size; | |
| 231 memcpy(buf, rptr, len); | |
| 232 buf += len; | |
| 233 rptr += len; | |
| 234 if (rptr >= f->end) | |
| 235 rptr = f->buffer; | |
| 236 buf_size -= len; | |
| 237 } | |
| 238 *rptr_ptr = rptr; | |
| 239 return 0; | |
| 240 } | |
| 241 | |
| 65 | 242 void fifo_write(FifoBuffer *f, uint8_t *buf, int size, uint8_t **wptr_ptr) |
| 0 | 243 { |
| 244 int len; | |
| 65 | 245 uint8_t *wptr; |
| 0 | 246 wptr = *wptr_ptr; |
| 247 while (size > 0) { | |
| 248 len = f->end - wptr; | |
| 249 if (len > size) | |
| 250 len = size; | |
| 251 memcpy(wptr, buf, len); | |
| 252 wptr += len; | |
| 253 if (wptr >= f->end) | |
| 254 wptr = f->buffer; | |
| 255 buf += len; | |
| 256 size -= len; | |
| 257 } | |
| 258 *wptr_ptr = wptr; | |
| 259 } | |
| 260 | |
| 261 int filename_number_test(const char *filename) | |
| 262 { | |
| 263 char buf[1024]; | |
| 264 return get_frame_filename(buf, sizeof(buf), filename, 1); | |
| 265 } | |
| 266 | |
| 267 /* guess file format */ | |
| 268 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened) | |
| 269 { | |
| 270 AVInputFormat *fmt1, *fmt; | |
| 271 int score, score_max; | |
| 272 | |
| 273 fmt = NULL; | |
| 274 score_max = 0; | |
| 275 for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) { | |
| 276 if (!is_opened && !(fmt1->flags & AVFMT_NOFILE)) | |
| 277 continue; | |
| 278 score = 0; | |
| 279 if (fmt1->read_probe) { | |
| 280 score = fmt1->read_probe(pd); | |
| 281 } else if (fmt1->extensions) { | |
| 282 if (match_ext(pd->filename, fmt1->extensions)) { | |
| 283 score = 50; | |
| 284 } | |
| 285 } | |
| 286 if (score > score_max) { | |
| 287 score_max = score; | |
| 288 fmt = fmt1; | |
| 289 } | |
| 290 } | |
| 291 return fmt; | |
| 292 } | |
| 293 | |
| 294 /************************************************************/ | |
| 295 /* input media file */ | |
| 296 | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
297 /** |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
298 * open a media file from an IO stream. 'fmt' must be specified. |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
299 */ |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
300 int av_open_input_stream(AVFormatContext **ic_ptr, |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
301 ByteIOContext *pb, const char *filename, |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
302 AVInputFormat *fmt, AVFormatParameters *ap) |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
303 { |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
304 int err; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
305 AVFormatContext *ic; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
306 |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
307 ic = av_mallocz(sizeof(AVFormatContext)); |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
308 if (!ic) { |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
309 err = AVERROR_NOMEM; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
310 goto fail; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
311 } |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
312 ic->iformat = fmt; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
313 if (pb) |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
314 ic->pb = *pb; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
315 ic->duration = AV_NOPTS_VALUE; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
316 ic->start_time = AV_NOPTS_VALUE; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
317 pstrcpy(ic->filename, sizeof(ic->filename), filename); |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
318 |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
319 /* allocate private data */ |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
320 if (fmt->priv_data_size > 0) { |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
321 ic->priv_data = av_mallocz(fmt->priv_data_size); |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
322 if (!ic->priv_data) { |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
323 err = AVERROR_NOMEM; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
324 goto fail; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
325 } |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
326 } else { |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
327 ic->priv_data = NULL; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
328 } |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
329 |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
330 /* default pts settings is MPEG like */ |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
331 av_set_pts_info(ic, 33, 1, 90000); |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
332 ic->last_pkt_pts = AV_NOPTS_VALUE; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
333 ic->last_pkt_dts = AV_NOPTS_VALUE; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
334 ic->last_pkt_stream_pts = AV_NOPTS_VALUE; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
335 ic->last_pkt_stream_dts = AV_NOPTS_VALUE; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
336 |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
337 err = ic->iformat->read_header(ic, ap); |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
338 if (err < 0) |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
339 goto fail; |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
340 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
341 if (pb) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
342 ic->data_offset = url_ftell(&ic->pb); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
343 |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
344 *ic_ptr = ic; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
345 return 0; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
346 fail: |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
347 if (ic) { |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
348 av_freep(&ic->priv_data); |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
349 } |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
350 av_free(ic); |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
351 *ic_ptr = NULL; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
352 return err; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
353 } |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
354 |
| 0 | 355 #define PROBE_BUF_SIZE 2048 |
| 356 | |
| 357 /** | |
| 358 * Open a media file as input. The codec are not opened. Only the file | |
| 359 * header (if present) is read. | |
| 360 * | |
| 361 * @param ic_ptr the opened media file handle is put here | |
| 362 * @param filename filename to open. | |
| 363 * @param fmt if non NULL, force the file format to use | |
| 364 * @param buf_size optional buffer size (zero if default is OK) | |
| 365 * @param ap additionnal parameters needed when opening the file (NULL if default) | |
| 366 * @return 0 if OK. AVERROR_xxx otherwise. | |
| 367 */ | |
| 368 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename, | |
| 369 AVInputFormat *fmt, | |
| 370 int buf_size, | |
| 371 AVFormatParameters *ap) | |
| 372 { | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
373 int err, must_open_file, file_opened; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
374 uint8_t buf[PROBE_BUF_SIZE]; |
| 0 | 375 AVProbeData probe_data, *pd = &probe_data; |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
376 ByteIOContext pb1, *pb = &pb1; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
377 |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
378 file_opened = 0; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
379 pd->filename = ""; |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
380 if (filename) |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
381 pd->filename = filename; |
| 0 | 382 pd->buf = buf; |
| 383 pd->buf_size = 0; | |
| 384 | |
| 385 if (!fmt) { | |
| 386 /* guess format if no file can be opened */ | |
| 387 fmt = av_probe_input_format(pd, 0); | |
| 388 } | |
| 389 | |
| 172 | 390 /* do not open file if the format does not need it. XXX: specific |
| 391 hack needed to handle RTSP/TCP */ | |
| 392 must_open_file = 1; | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
393 if (fmt && (fmt->flags & AVFMT_NOFILE)) { |
| 172 | 394 must_open_file = 0; |
| 395 } | |
| 396 | |
| 397 if (!fmt || must_open_file) { | |
| 20 | 398 /* if no file needed do not try to open one */ |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
399 if (url_fopen(pb, filename, URL_RDONLY) < 0) { |
| 0 | 400 err = AVERROR_IO; |
| 401 goto fail; | |
| 402 } | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
403 file_opened = 1; |
| 0 | 404 if (buf_size > 0) { |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
405 url_setbufsize(pb, buf_size); |
| 0 | 406 } |
| 407 if (!fmt) { | |
| 408 /* read probe data */ | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
409 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE); |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
410 url_fseek(pb, 0, SEEK_SET); |
| 0 | 411 } |
| 412 } | |
| 413 | |
| 414 /* guess file format */ | |
| 415 if (!fmt) { | |
| 416 fmt = av_probe_input_format(pd, 1); | |
| 417 } | |
| 418 | |
| 419 /* if still no format found, error */ | |
| 420 if (!fmt) { | |
| 421 err = AVERROR_NOFMT; | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
422 goto fail; |
| 0 | 423 } |
| 424 | |
| 425 /* XXX: suppress this hack for redirectors */ | |
|
22
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
426 #ifdef CONFIG_NETWORK |
| 0 | 427 if (fmt == &redir_demux) { |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
428 err = redir_open(ic_ptr, pb); |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
429 url_fclose(pb); |
| 0 | 430 return err; |
| 431 } | |
|
11
932b59c66c60
mingw patch by (Bill Eldridge <bill at rfa dot org>)
michaelni
parents:
9
diff
changeset
|
432 #endif |
| 0 | 433 |
| 20 | 434 /* check filename in case of an image number is expected */ |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
435 if (fmt->flags & AVFMT_NEEDNUMBER) { |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
436 if (filename_number_test(filename) < 0) { |
| 20 | 437 err = AVERROR_NUMEXPECTED; |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
438 goto fail; |
| 20 | 439 } |
| 440 } | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
441 err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap); |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
442 if (err) |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
443 goto fail; |
| 0 | 444 return 0; |
| 445 fail: | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
446 if (file_opened) |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
447 url_fclose(pb); |
| 0 | 448 *ic_ptr = NULL; |
| 449 return err; | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
450 |
| 0 | 451 } |
| 452 | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
453 /*******************************************************/ |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
454 |
| 0 | 455 /** |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
456 * Read a transport packet from a media file. This function is |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
457 * absolete and should never be used. Use av_read_frame() instead. |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
458 * |
| 0 | 459 * @param s media file handle |
| 460 * @param pkt is filled | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
461 * @return 0 if OK. AVERROR_xxx if error. |
| 0 | 462 */ |
| 463 int av_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 464 { | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
465 return s->iformat->read_packet(s, pkt); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
466 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
467 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
468 /**********************************************************/ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
469 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
470 /* convert the packet time stamp units and handle wrapping. The |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
471 wrapping is handled by considering the next PTS/DTS as a delta to |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
472 the previous value. We handle the delta as a fraction to avoid any |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
473 rounding errors. */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
474 static inline int64_t convert_timestamp_units(AVFormatContext *s, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
475 int64_t *plast_pkt_pts, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
476 int *plast_pkt_pts_frac, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
477 int64_t *plast_pkt_stream_pts, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
478 int64_t pts) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
479 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
480 int64_t stream_pts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
481 int64_t delta_pts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
482 int shift, pts_frac; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
483 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
484 if (pts != AV_NOPTS_VALUE) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
485 stream_pts = pts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
486 if (*plast_pkt_stream_pts != AV_NOPTS_VALUE) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
487 shift = 64 - s->pts_wrap_bits; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
488 delta_pts = ((stream_pts - *plast_pkt_stream_pts) << shift) >> shift; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
489 /* XXX: overflow possible but very unlikely as it is a delta */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
490 delta_pts = delta_pts * AV_TIME_BASE * s->pts_num; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
491 pts = *plast_pkt_pts + (delta_pts / s->pts_den); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
492 pts_frac = *plast_pkt_pts_frac + (delta_pts % s->pts_den); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
493 if (pts_frac >= s->pts_den) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
494 pts_frac -= s->pts_den; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
495 pts++; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
496 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
497 } else { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
498 /* no previous pts, so no wrapping possible */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
499 pts = (int64_t)(((double)stream_pts * AV_TIME_BASE * s->pts_num) / |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
500 (double)s->pts_den); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
501 pts_frac = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
502 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
503 *plast_pkt_stream_pts = stream_pts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
504 *plast_pkt_pts = pts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
505 *plast_pkt_pts_frac = pts_frac; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
506 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
507 return pts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
508 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
509 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
510 /* get the number of samples of an audio frame. Return (-1) if error */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
511 static int get_audio_frame_size(AVCodecContext *enc, int size) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
512 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
513 int frame_size; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
514 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
515 if (enc->frame_size <= 1) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
516 /* specific hack for pcm codecs because no frame size is |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
517 provided */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
518 switch(enc->codec_id) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
519 case CODEC_ID_PCM_S16LE: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
520 case CODEC_ID_PCM_S16BE: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
521 case CODEC_ID_PCM_U16LE: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
522 case CODEC_ID_PCM_U16BE: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
523 if (enc->channels == 0) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
524 return -1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
525 frame_size = size / (2 * enc->channels); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
526 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
527 case CODEC_ID_PCM_S8: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
528 case CODEC_ID_PCM_U8: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
529 case CODEC_ID_PCM_MULAW: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
530 case CODEC_ID_PCM_ALAW: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
531 if (enc->channels == 0) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
532 return -1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
533 frame_size = size / (enc->channels); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
534 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
535 default: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
536 /* used for example by ADPCM codecs */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
537 if (enc->bit_rate == 0) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
538 return -1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
539 frame_size = (size * 8 * enc->sample_rate) / enc->bit_rate; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
540 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
541 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
542 } else { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
543 frame_size = enc->frame_size; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
544 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
545 return frame_size; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
546 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
547 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
548 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
549 /* return the frame duration in seconds, return 0 if not available */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
550 static void compute_frame_duration(int *pnum, int *pden, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
551 AVFormatContext *s, AVStream *st, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
552 AVCodecParserContext *pc, AVPacket *pkt) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
553 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
554 int frame_size; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
555 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
556 *pnum = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
557 *pden = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
558 switch(st->codec.codec_type) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
559 case CODEC_TYPE_VIDEO: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
560 *pnum = st->codec.frame_rate_base; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
561 *pden = st->codec.frame_rate; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
562 if (pc && pc->repeat_pict) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
563 *pden *= 2; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
564 *pnum = (*pnum) * (2 + pc->repeat_pict); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
565 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
566 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
567 case CODEC_TYPE_AUDIO: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
568 frame_size = get_audio_frame_size(&st->codec, pkt->size); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
569 if (frame_size < 0) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
570 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
571 *pnum = frame_size; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
572 *pden = st->codec.sample_rate; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
573 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
574 default: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
575 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
576 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
577 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
578 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
579 static void compute_pkt_fields(AVFormatContext *s, AVStream *st, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
580 AVCodecParserContext *pc, AVPacket *pkt) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
581 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
582 int num, den, presentation_delayed; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
583 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
584 if (pkt->duration == 0) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
585 compute_frame_duration(&num, &den, s, st, pc, pkt); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
586 if (den && num) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
587 pkt->duration = (num * (int64_t)AV_TIME_BASE) / den; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
588 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
589 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
590 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
591 /* do we have a video B frame ? */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
592 presentation_delayed = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
593 if (st->codec.codec_type == CODEC_TYPE_VIDEO) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
594 /* XXX: need has_b_frame, but cannot get it if the codec is |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
595 not initialized */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
596 if ((st->codec.codec_id == CODEC_ID_MPEG1VIDEO || |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
597 st->codec.codec_id == CODEC_ID_MPEG2VIDEO || |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
598 st->codec.codec_id == CODEC_ID_MPEG4 || |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
599 st->codec.codec_id == CODEC_ID_H264) && |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
600 pc && pc->pict_type != FF_B_TYPE) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
601 presentation_delayed = 1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
602 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
603 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
604 /* interpolate PTS and DTS if they are not present */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
605 if (presentation_delayed) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
606 /* DTS = decompression time stamp */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
607 /* PTS = presentation time stamp */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
608 if (pkt->dts == AV_NOPTS_VALUE) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
609 pkt->dts = st->cur_dts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
610 } else { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
611 st->cur_dts = pkt->dts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
612 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
613 /* this is tricky: the dts must be incremented by the duration |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
614 of the frame we are displaying, i.e. the last I or P frame */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
615 if (st->last_IP_duration == 0) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
616 st->cur_dts += pkt->duration; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
617 else |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
618 st->cur_dts += st->last_IP_duration; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
619 st->last_IP_duration = pkt->duration; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
620 /* cannot compute PTS if not present (we can compute it only |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
621 by knowing the futur */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
622 } else { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
623 /* presentation is not delayed : PTS and DTS are the same */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
624 if (pkt->pts == AV_NOPTS_VALUE) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
625 pkt->pts = st->cur_dts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
626 pkt->dts = st->cur_dts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
627 } else { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
628 st->cur_dts = pkt->pts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
629 pkt->dts = pkt->pts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
630 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
631 st->cur_dts += pkt->duration; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
632 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
633 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
634 /* update flags */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
635 if (pc) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
636 pkt->flags = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
637 /* key frame computation */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
638 switch(st->codec.codec_type) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
639 case CODEC_TYPE_VIDEO: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
640 if (pc->pict_type == FF_I_TYPE) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
641 pkt->flags |= PKT_FLAG_KEY; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
642 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
643 case CODEC_TYPE_AUDIO: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
644 pkt->flags |= PKT_FLAG_KEY; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
645 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
646 default: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
647 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
648 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
649 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
650 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
651 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
652 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
653 static void av_destruct_packet_nofree(AVPacket *pkt) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
654 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
655 pkt->data = NULL; pkt->size = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
656 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
657 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
658 static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
659 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
660 AVStream *st; |
| 333 | 661 int len, ret, i; |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
662 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
663 for(;;) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
664 /* select current input stream component */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
665 st = s->cur_st; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
666 if (st) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
667 if (!st->parser) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
668 /* no parsing needed: we just output the packet as is */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
669 /* raw data support */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
670 *pkt = s->cur_pkt; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
671 compute_pkt_fields(s, st, NULL, pkt); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
672 s->cur_st = NULL; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
673 return 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
674 } else if (s->cur_len > 0) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
675 /* we use the MPEG semantics: the pts and dts in a |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
676 packet are given from the first frame beginning in |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
677 it */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
678 if (!st->got_frame) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
679 st->cur_frame_pts = s->cur_pkt.pts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
680 st->cur_frame_dts = s->cur_pkt.dts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
681 s->cur_pkt.pts = AV_NOPTS_VALUE; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
682 s->cur_pkt.dts = AV_NOPTS_VALUE; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
683 st->got_frame = 1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
684 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
685 len = av_parser_parse(st->parser, &st->codec, &pkt->data, &pkt->size, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
686 s->cur_ptr, s->cur_len); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
687 /* increment read pointer */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
688 s->cur_ptr += len; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
689 s->cur_len -= len; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
690 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
691 /* return packet if any */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
692 if (pkt->size) { |
| 333 | 693 got_packet: |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
694 pkt->duration = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
695 pkt->stream_index = st->index; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
696 pkt->pts = st->cur_frame_pts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
697 pkt->dts = st->cur_frame_dts; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
698 pkt->destruct = av_destruct_packet_nofree; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
699 compute_pkt_fields(s, st, st->parser, pkt); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
700 st->got_frame = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
701 return 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
702 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
703 } else { |
| 319 | 704 /* free packet */ |
| 705 av_free_packet(&s->cur_pkt); | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
706 s->cur_st = NULL; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
707 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
708 } else { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
709 /* read next packet */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
710 ret = av_read_packet(s, &s->cur_pkt); |
| 333 | 711 if (ret < 0) { |
| 712 if (ret == -EAGAIN) | |
| 713 return ret; | |
| 714 /* return the last frames, if any */ | |
| 715 for(i = 0; i < s->nb_streams; i++) { | |
| 716 st = s->streams[i]; | |
| 717 if (st->parser) { | |
| 718 av_parser_parse(st->parser, &st->codec, | |
| 719 &pkt->data, &pkt->size, | |
| 720 NULL, 0); | |
| 721 if (pkt->size) | |
| 722 goto got_packet; | |
| 723 } | |
| 724 } | |
| 725 /* no more packets: really terminates parsing */ | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
726 return ret; |
| 333 | 727 } |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
728 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
729 /* convert the packet time stamp units and handle wrapping */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
730 s->cur_pkt.pts = convert_timestamp_units(s, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
731 &s->last_pkt_pts, &s->last_pkt_pts_frac, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
732 &s->last_pkt_stream_pts, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
733 s->cur_pkt.pts); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
734 s->cur_pkt.dts = convert_timestamp_units(s, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
735 &s->last_pkt_dts, &s->last_pkt_dts_frac, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
736 &s->last_pkt_stream_dts, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
737 s->cur_pkt.dts); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
738 #if 0 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
739 if (s->cur_pkt.stream_index == 1) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
740 if (s->cur_pkt.pts != AV_NOPTS_VALUE) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
741 printf("PACKET pts=%0.3f\n", |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
742 (double)s->cur_pkt.pts / AV_TIME_BASE); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
743 if (s->cur_pkt.dts != AV_NOPTS_VALUE) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
744 printf("PACKET dts=%0.3f\n", |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
745 (double)s->cur_pkt.dts / AV_TIME_BASE); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
746 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
747 #endif |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
748 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
749 /* duration field */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
750 if (s->cur_pkt.duration != 0) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
751 s->cur_pkt.duration = ((int64_t)s->cur_pkt.duration * AV_TIME_BASE * s->pts_num) / |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
752 s->pts_den; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
753 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
754 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
755 st = s->streams[s->cur_pkt.stream_index]; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
756 s->cur_st = st; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
757 s->cur_ptr = s->cur_pkt.data; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
758 s->cur_len = s->cur_pkt.size; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
759 if (st->need_parsing && !st->parser) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
760 st->parser = av_parser_init(st->codec.codec_id); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
761 if (!st->parser) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
762 /* no parser available : just output the raw packets */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
763 st->need_parsing = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
764 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
765 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
766 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
767 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
768 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
769 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
770 /** |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
771 * Return the next frame of a stream. The returned packet is valid |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
772 * until the next av_read_frame() or until av_close_input_file() and |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
773 * must be freed with av_free_packet. For video, the packet contains |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
774 * exactly one frame. For audio, it contains an integer number of |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
775 * frames if each frame has a known fixed size (e.g. PCM or ADPCM |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
776 * data). If the audio frames have a variable size (e.g. MPEG audio), |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
777 * then it contains one frame. |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
778 * |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
779 * pkt->pts, pkt->dts and pkt->duration are always set to correct |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
780 * values in AV_TIME_BASE unit (and guessed if the format cannot |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
781 * provided them). pkt->pts can be AV_NOPTS_VALUE if the video format |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
782 * has B frames, so it is better to rely on pkt->dts if you do not |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
783 * decompress the payload. |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
784 * |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
785 * Return 0 if OK, < 0 if error or end of file. |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
786 */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
787 int av_read_frame(AVFormatContext *s, AVPacket *pkt) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
788 { |
| 0 | 789 AVPacketList *pktl; |
| 790 | |
| 791 pktl = s->packet_buffer; | |
| 792 if (pktl) { | |
| 793 /* read packet from packet buffer, if there is data */ | |
| 794 *pkt = pktl->pkt; | |
| 795 s->packet_buffer = pktl->next; | |
| 796 av_free(pktl); | |
| 797 return 0; | |
| 798 } else { | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
799 return av_read_frame_internal(s, pkt); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
800 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
801 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
802 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
803 /* XXX: suppress the packet queue */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
804 static void flush_packet_queue(AVFormatContext *s) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
805 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
806 AVPacketList *pktl; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
807 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
808 for(;;) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
809 pktl = s->packet_buffer; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
810 if (!pktl) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
811 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
812 s->packet_buffer = pktl->next; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
813 av_free_packet(&pktl->pkt); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
814 av_free(pktl); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
815 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
816 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
817 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
818 /*******************************************************/ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
819 /* seek support */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
820 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
821 /* flush the frame reader */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
822 static void av_read_frame_flush(AVFormatContext *s) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
823 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
824 AVStream *st; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
825 int i; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
826 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
827 flush_packet_queue(s); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
828 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
829 /* free previous packet */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
830 if (s->cur_st) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
831 if (s->cur_st->parser) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
832 av_free_packet(&s->cur_pkt); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
833 s->cur_st = NULL; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
834 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
835 /* fail safe */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
836 s->cur_ptr = NULL; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
837 s->cur_len = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
838 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
839 /* for each stream, reset read state */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
840 for(i = 0; i < s->nb_streams; i++) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
841 st = s->streams[i]; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
842 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
843 if (st->parser) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
844 av_parser_close(st->parser); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
845 st->parser = NULL; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
846 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
847 st->got_frame = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
848 st->cur_dts = 0; /* we set the current DTS to an unspecified origin */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
849 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
850 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
851 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
852 static void add_index_entry(AVStream *st, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
853 int64_t pos, int64_t timestamp, int flags) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
854 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
855 AVIndexEntry *entries, *ie; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
856 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
857 entries = av_fast_realloc(st->index_entries, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
858 &st->index_entries_allocated_size, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
859 (st->nb_index_entries + 1) * |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
860 sizeof(AVIndexEntry)); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
861 if (entries) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
862 st->index_entries = entries; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
863 ie = &entries[st->nb_index_entries++]; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
864 ie->pos = pos; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
865 ie->timestamp = timestamp; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
866 ie->flags = flags; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
867 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
868 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
869 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
870 /* build an index for raw streams using a parser */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
871 static void av_build_index_raw(AVFormatContext *s) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
872 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
873 AVPacket pkt1, *pkt = &pkt1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
874 int ret; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
875 AVStream *st; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
876 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
877 st = s->streams[0]; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
878 av_read_frame_flush(s); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
879 url_fseek(&s->pb, s->data_offset, SEEK_SET); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
880 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
881 for(;;) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
882 ret = av_read_frame(s, pkt); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
883 if (ret < 0) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
884 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
885 if (pkt->stream_index == 0 && st->parser && |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
886 (pkt->flags & PKT_FLAG_KEY)) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
887 add_index_entry(st, st->parser->frame_offset, pkt->dts, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
888 AVINDEX_KEYFRAME); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
889 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
890 av_free_packet(pkt); |
| 0 | 891 } |
| 892 } | |
| 893 | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
894 /* return TRUE if we deal with a raw stream (raw codec data and |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
895 parsing needed) */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
896 static int is_raw_stream(AVFormatContext *s) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
897 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
898 AVStream *st; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
899 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
900 if (s->nb_streams != 1) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
901 return 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
902 st = s->streams[0]; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
903 if (!st->need_parsing) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
904 return 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
905 return 1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
906 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
907 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
908 /* return the largest index entry whose timestamp is <= |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
909 wanted_timestamp */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
910 static int index_search_timestamp(AVIndexEntry *entries, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
911 int nb_entries, int wanted_timestamp) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
912 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
913 int a, b, m; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
914 int64_t timestamp; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
915 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
916 if (nb_entries <= 0) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
917 return -1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
918 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
919 a = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
920 b = nb_entries - 1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
921 while (a <= b) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
922 m = (a + b) >> 1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
923 timestamp = entries[m].timestamp; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
924 if (timestamp == wanted_timestamp) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
925 goto found; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
926 else if (timestamp > wanted_timestamp) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
927 b = m - 1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
928 } else { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
929 a = m + 1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
930 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
931 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
932 m = a; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
933 if (m > 0) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
934 m--; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
935 found: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
936 return m; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
937 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
938 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
939 static int av_seek_frame_generic(AVFormatContext *s, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
940 int stream_index, int64_t timestamp) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
941 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
942 int index; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
943 AVStream *st; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
944 AVIndexEntry *ie; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
945 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
946 if (!s->index_built) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
947 if (is_raw_stream(s)) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
948 av_build_index_raw(s); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
949 } else { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
950 return -1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
951 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
952 s->index_built = 1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
953 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
954 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
955 if (stream_index < 0) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
956 stream_index = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
957 st = s->streams[stream_index]; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
958 index = index_search_timestamp(st->index_entries, st->nb_index_entries, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
959 timestamp); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
960 if (index < 0) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
961 return -1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
962 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
963 /* now we have found the index, we can seek */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
964 ie = &st->index_entries[index]; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
965 av_read_frame_flush(s); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
966 url_fseek(&s->pb, ie->pos, SEEK_SET); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
967 st->cur_dts = ie->timestamp; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
968 return 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
969 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
970 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
971 /** |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
972 * Seek to the key frame just before the frame at timestamp |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
973 * 'timestamp' in 'stream_index'. If stream_index is (-1), a default |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
974 * stream is selected |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
975 */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
976 int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
977 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
978 int ret; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
979 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
980 av_read_frame_flush(s); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
981 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
982 /* first, we try the format specific seek */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
983 if (s->iformat->read_seek) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
984 ret = s->iformat->read_seek(s, stream_index, timestamp); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
985 else |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
986 ret = -1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
987 if (ret >= 0) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
988 return 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
989 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
990 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
991 return av_seek_frame_generic(s, stream_index, timestamp); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
992 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
993 |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
994 /*******************************************************/ |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
995 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
996 /* return TRUE if the stream has accurate timings for at least one component */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
997 static int av_has_timings(AVFormatContext *ic) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
998 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
999 int i; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1000 AVStream *st; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1001 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1002 for(i = 0;i < ic->nb_streams; i++) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1003 st = ic->streams[i]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1004 if (st->start_time != AV_NOPTS_VALUE && |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1005 st->duration != AV_NOPTS_VALUE) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1006 return 1; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1007 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1008 return 0; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1009 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1010 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1011 /* estimate the stream timings from the one of each components. Also |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1012 compute the global bitrate if possible */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1013 static void av_update_stream_timings(AVFormatContext *ic) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1014 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1015 int64_t start_time, end_time, end_time1; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1016 int i; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1017 AVStream *st; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1018 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1019 start_time = MAXINT64; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1020 end_time = MININT64; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1021 for(i = 0;i < ic->nb_streams; i++) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1022 st = ic->streams[i]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1023 if (st->start_time != AV_NOPTS_VALUE) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1024 if (st->start_time < start_time) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1025 start_time = st->start_time; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1026 if (st->duration != AV_NOPTS_VALUE) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1027 end_time1 = st->start_time + st->duration; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1028 if (end_time1 > end_time) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1029 end_time = end_time1; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1030 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1031 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1032 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1033 if (start_time != MAXINT64) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1034 ic->start_time = start_time; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1035 if (end_time != MAXINT64) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1036 ic->duration = end_time - start_time; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1037 if (ic->file_size > 0) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1038 /* compute the bit rate */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1039 ic->bit_rate = (double)ic->file_size * 8.0 * AV_TIME_BASE / |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1040 (double)ic->duration; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1041 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1042 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1043 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1044 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1045 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1046 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1047 static void fill_all_stream_timings(AVFormatContext *ic) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1048 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1049 int i; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1050 AVStream *st; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1051 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1052 av_update_stream_timings(ic); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1053 for(i = 0;i < ic->nb_streams; i++) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1054 st = ic->streams[i]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1055 if (st->start_time == AV_NOPTS_VALUE) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1056 st->start_time = ic->start_time; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1057 st->duration = ic->duration; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1058 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1059 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1060 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1061 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1062 static void av_estimate_timings_from_bit_rate(AVFormatContext *ic) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1063 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1064 int64_t filesize, duration; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1065 int bit_rate, i; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1066 AVStream *st; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1067 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1068 /* if bit_rate is already set, we believe it */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1069 if (ic->bit_rate == 0) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1070 bit_rate = 0; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1071 for(i=0;i<ic->nb_streams;i++) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1072 st = ic->streams[i]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1073 bit_rate += st->codec.bit_rate; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1074 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1075 ic->bit_rate = bit_rate; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1076 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1077 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1078 /* if duration is already set, we believe it */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1079 if (ic->duration == AV_NOPTS_VALUE && |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1080 ic->bit_rate != 0 && |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1081 ic->file_size != 0) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1082 filesize = ic->file_size; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1083 if (filesize > 0) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1084 duration = (int64_t)((8 * AV_TIME_BASE * (double)filesize) / (double)ic->bit_rate); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1085 for(i = 0; i < ic->nb_streams; i++) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1086 st = ic->streams[i]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1087 if (st->start_time == AV_NOPTS_VALUE || |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1088 st->duration == AV_NOPTS_VALUE) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1089 st->start_time = 0; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1090 st->duration = duration; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1091 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1092 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1093 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1094 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1095 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1096 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1097 #define DURATION_MAX_READ_SIZE 250000 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1098 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1099 /* only usable for MPEG-PS streams */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1100 static void av_estimate_timings_from_pts(AVFormatContext *ic) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1101 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1102 AVPacket pkt1, *pkt = &pkt1; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1103 AVStream *st; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1104 int read_size, i, ret; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1105 int64_t start_time, end_time, end_time1; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1106 int64_t filesize, offset, duration; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1107 |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1108 /* free previous packet */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1109 if (ic->cur_st && ic->cur_st->parser) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1110 av_free_packet(&ic->cur_pkt); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1111 ic->cur_st = NULL; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1112 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1113 /* flush packet queue */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1114 flush_packet_queue(ic); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1115 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1116 |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1117 /* we read the first packets to get the first PTS (not fully |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1118 accurate, but it is enough now) */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1119 url_fseek(&ic->pb, 0, SEEK_SET); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1120 read_size = 0; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1121 for(;;) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1122 if (read_size >= DURATION_MAX_READ_SIZE) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1123 break; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1124 /* if all info is available, we can stop */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1125 for(i = 0;i < ic->nb_streams; i++) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1126 st = ic->streams[i]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1127 if (st->start_time == AV_NOPTS_VALUE) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1128 break; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1129 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1130 if (i == ic->nb_streams) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1131 break; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1132 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1133 ret = av_read_packet(ic, pkt); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1134 if (ret != 0) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1135 break; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1136 read_size += pkt->size; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1137 st = ic->streams[pkt->stream_index]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1138 if (pkt->pts != AV_NOPTS_VALUE) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1139 if (st->start_time == AV_NOPTS_VALUE) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1140 st->start_time = (int64_t)((double)pkt->pts * ic->pts_num * (double)AV_TIME_BASE / ic->pts_den); |
|
224
8d1569be0ee1
memory leak fix by (Tom Dexter <devel at www dot digitalaudiorock dot com>)
michaelni
parents:
205
diff
changeset
|
1141 } |
|
8d1569be0ee1
memory leak fix by (Tom Dexter <devel at www dot digitalaudiorock dot com>)
michaelni
parents:
205
diff
changeset
|
1142 av_free_packet(pkt); |
|
8d1569be0ee1
memory leak fix by (Tom Dexter <devel at www dot digitalaudiorock dot com>)
michaelni
parents:
205
diff
changeset
|
1143 } |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1144 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1145 /* we compute the minimum start_time and use it as default */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1146 start_time = MAXINT64; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1147 for(i = 0; i < ic->nb_streams; i++) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1148 st = ic->streams[i]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1149 if (st->start_time != AV_NOPTS_VALUE && |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1150 st->start_time < start_time) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1151 start_time = st->start_time; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1152 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1153 if (start_time != MAXINT64) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1154 ic->start_time = start_time; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1155 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1156 /* estimate the end time (duration) */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1157 /* XXX: may need to support wrapping */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1158 filesize = ic->file_size; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1159 offset = filesize - DURATION_MAX_READ_SIZE; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1160 if (offset < 0) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1161 offset = 0; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1162 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1163 url_fseek(&ic->pb, offset, SEEK_SET); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1164 read_size = 0; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1165 for(;;) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1166 if (read_size >= DURATION_MAX_READ_SIZE) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1167 break; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1168 /* if all info is available, we can stop */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1169 for(i = 0;i < ic->nb_streams; i++) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1170 st = ic->streams[i]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1171 if (st->duration == AV_NOPTS_VALUE) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1172 break; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1173 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1174 if (i == ic->nb_streams) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1175 break; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1176 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1177 ret = av_read_packet(ic, pkt); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1178 if (ret != 0) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1179 break; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1180 read_size += pkt->size; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1181 st = ic->streams[pkt->stream_index]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1182 if (pkt->pts != AV_NOPTS_VALUE) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1183 end_time = (int64_t)((double)pkt->pts * ic->pts_num * (double)AV_TIME_BASE / ic->pts_den); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1184 duration = end_time - st->start_time; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1185 if (duration > 0) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1186 if (st->duration == AV_NOPTS_VALUE || |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1187 st->duration < duration) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1188 st->duration = duration; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1189 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1190 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1191 av_free_packet(pkt); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1192 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1193 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1194 /* estimate total duration */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1195 end_time = MININT64; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1196 for(i = 0;i < ic->nb_streams; i++) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1197 st = ic->streams[i]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1198 if (st->duration != AV_NOPTS_VALUE) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1199 end_time1 = st->start_time + st->duration; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1200 if (end_time1 > end_time) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1201 end_time = end_time1; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1202 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1203 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1204 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1205 /* update start_time (new stream may have been created, so we do |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1206 it at the end */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1207 if (ic->start_time != AV_NOPTS_VALUE) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1208 for(i = 0; i < ic->nb_streams; i++) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1209 st = ic->streams[i]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1210 if (st->start_time == AV_NOPTS_VALUE) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1211 st->start_time = ic->start_time; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1212 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1213 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1214 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1215 if (end_time != MININT64) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1216 /* put dummy values for duration if needed */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1217 for(i = 0;i < ic->nb_streams; i++) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1218 st = ic->streams[i]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1219 if (st->duration == AV_NOPTS_VALUE && |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1220 st->start_time != AV_NOPTS_VALUE) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1221 st->duration = end_time - st->start_time; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1222 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1223 ic->duration = end_time - ic->start_time; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1224 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1225 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1226 url_fseek(&ic->pb, 0, SEEK_SET); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1227 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1228 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1229 static void av_estimate_timings(AVFormatContext *ic) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1230 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1231 URLContext *h; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1232 int64_t file_size; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1233 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1234 /* get the file size, if possible */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1235 if (ic->iformat->flags & AVFMT_NOFILE) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1236 file_size = 0; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1237 } else { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1238 h = url_fileno(&ic->pb); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1239 file_size = url_filesize(h); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1240 if (file_size < 0) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1241 file_size = 0; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1242 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1243 ic->file_size = file_size; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1244 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1245 if (ic->iformat == &mpegps_demux) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1246 /* get accurate estimate from the PTSes */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1247 av_estimate_timings_from_pts(ic); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1248 } else if (av_has_timings(ic)) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1249 /* at least one components has timings - we use them for all |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1250 the components */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1251 fill_all_stream_timings(ic); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1252 } else { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1253 /* less precise: use bit rate info */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1254 av_estimate_timings_from_bit_rate(ic); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1255 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1256 av_update_stream_timings(ic); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1257 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1258 #if 0 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1259 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1260 int i; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1261 AVStream *st; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1262 for(i = 0;i < ic->nb_streams; i++) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1263 st = ic->streams[i]; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1264 printf("%d: start_time: %0.3f duration: %0.3f\n", |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1265 i, (double)st->start_time / AV_TIME_BASE, |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1266 (double)st->duration / AV_TIME_BASE); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1267 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1268 printf("stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n", |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1269 (double)ic->start_time / AV_TIME_BASE, |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1270 (double)ic->duration / AV_TIME_BASE, |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1271 ic->bit_rate / 1000); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1272 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1273 #endif |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1274 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1275 |
| 0 | 1276 static int has_codec_parameters(AVCodecContext *enc) |
| 1277 { | |
| 1278 int val; | |
| 1279 switch(enc->codec_type) { | |
| 1280 case CODEC_TYPE_AUDIO: | |
| 1281 val = enc->sample_rate; | |
| 1282 break; | |
| 1283 case CODEC_TYPE_VIDEO: | |
| 1284 val = enc->width; | |
| 1285 break; | |
| 1286 default: | |
| 1287 val = 1; | |
| 1288 break; | |
| 1289 } | |
| 1290 return (val != 0); | |
| 1291 } | |
| 1292 | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1293 static int try_decode_frame(AVStream *st, const uint8_t *data, int size) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1294 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1295 int16_t *samples; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1296 AVCodec *codec; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1297 int got_picture, ret; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1298 AVFrame picture; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1299 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1300 codec = avcodec_find_decoder(st->codec.codec_id); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1301 if (!codec) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1302 return -1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1303 ret = avcodec_open(&st->codec, codec); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1304 if (ret < 0) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1305 return ret; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1306 switch(st->codec.codec_type) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1307 case CODEC_TYPE_VIDEO: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1308 ret = avcodec_decode_video(&st->codec, &picture, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1309 &got_picture, (uint8_t *)data, size); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1310 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1311 case CODEC_TYPE_AUDIO: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1312 samples = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1313 if (!samples) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1314 goto fail; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1315 ret = avcodec_decode_audio(&st->codec, samples, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1316 &got_picture, (uint8_t *)data, size); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1317 av_free(samples); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1318 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1319 default: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1320 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1321 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1322 fail: |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1323 avcodec_close(&st->codec); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1324 return ret; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1325 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1326 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1327 /* absolute maximum size we read until we abort */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1328 #define MAX_READ_SIZE 5000000 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1329 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1330 /* maximum duration until we stop analysing the stream */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1331 #define MAX_STREAM_DURATION ((int)(AV_TIME_BASE * 1.0)) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1332 |
| 0 | 1333 /** |
| 1334 * Read the beginning of a media file to get stream information. This | |
| 1335 * is useful for file formats with no headers such as MPEG. This | |
| 1336 * function also compute the real frame rate in case of mpeg2 repeat | |
| 1337 * frame mode. | |
| 1338 * | |
| 1339 * @param ic media file handle | |
| 1340 * @return >=0 if OK. AVERROR_xxx if error. | |
| 1341 */ | |
| 1342 int av_find_stream_info(AVFormatContext *ic) | |
| 1343 { | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1344 int i, count, ret, read_size; |
| 0 | 1345 AVStream *st; |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1346 AVPacket pkt1, *pkt; |
| 0 | 1347 AVPacketList *pktl=NULL, **ppktl; |
| 1348 | |
| 1349 count = 0; | |
| 1350 read_size = 0; | |
| 1351 ppktl = &ic->packet_buffer; | |
| 1352 for(;;) { | |
| 1353 /* check if one codec still needs to be handled */ | |
| 1354 for(i=0;i<ic->nb_streams;i++) { | |
| 1355 st = ic->streams[i]; | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1356 if (!has_codec_parameters(&st->codec)) |
| 0 | 1357 break; |
| 1358 } | |
| 1359 if (i == ic->nb_streams) { | |
| 1360 /* NOTE: if the format has no header, then we need to read | |
| 1361 some packets to get most of the streams, so we cannot | |
| 1362 stop here */ | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1363 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) { |
| 0 | 1364 /* if we found the info for all the codecs, we can stop */ |
| 1365 ret = count; | |
| 1366 break; | |
| 1367 } | |
| 1368 } else { | |
| 1369 /* we did not get all the codec info, but we read too much data */ | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1370 if (read_size >= MAX_READ_SIZE) { |
| 0 | 1371 ret = count; |
| 1372 break; | |
| 1373 } | |
| 1374 } | |
| 1375 | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1376 /* NOTE: a new stream can be added there if no header in file |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1377 (AVFMTCTX_NOHEADER) */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1378 ret = av_read_frame_internal(ic, &pkt1); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1379 if (ret < 0) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1380 /* EOF or error */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1381 ret = -1; /* we could not have all the codec parameters before EOF */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1382 if ((ic->ctx_flags & AVFMTCTX_NOHEADER) && |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1383 i == ic->nb_streams) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1384 ret = 0; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1385 break; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1386 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1387 |
| 0 | 1388 pktl = av_mallocz(sizeof(AVPacketList)); |
| 1389 if (!pktl) { | |
| 1390 ret = AVERROR_NOMEM; | |
| 1391 break; | |
| 1392 } | |
| 1393 | |
| 1394 /* add the packet in the buffered packet list */ | |
| 1395 *ppktl = pktl; | |
| 1396 ppktl = &pktl->next; | |
| 1397 | |
| 1398 pkt = &pktl->pkt; | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1399 *pkt = pkt1; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1400 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1401 /* duplicate the packet */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1402 if (av_dup_packet(pkt) < 0) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1403 ret = AVERROR_NOMEM; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1404 break; |
| 0 | 1405 } |
| 1406 | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1407 read_size += pkt->size; |
| 0 | 1408 |
| 1409 st = ic->streams[pkt->stream_index]; | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1410 st->codec_info_duration += pkt->duration; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1411 if (pkt->duration != 0) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1412 st->codec_info_nb_frames++; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1413 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1414 /* if still no information, we try to open the codec and to |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1415 decompress the frame. We try to avoid that in most cases as |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1416 it takes longer and uses more memory. For MPEG4, we need to |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1417 decompress for Quicktime. */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1418 if (!has_codec_parameters(&st->codec) && |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1419 (st->codec.codec_id == CODEC_ID_FLV1 || |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1420 st->codec.codec_id == CODEC_ID_H264 || |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1421 st->codec.codec_id == CODEC_ID_H263 || |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1422 (st->codec.codec_id == CODEC_ID_MPEG4 && !st->need_parsing))) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1423 try_decode_frame(st, pkt->data, pkt->size); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1424 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1425 if (st->codec_info_duration >= MAX_STREAM_DURATION) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1426 break; |
| 0 | 1427 } |
| 1428 count++; | |
| 1429 } | |
| 1430 | |
| 1431 /* set real frame rate info */ | |
| 1432 for(i=0;i<ic->nb_streams;i++) { | |
| 1433 st = ic->streams[i]; | |
| 1434 if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1435 /* compute the real frame rate for telecine */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1436 if ((st->codec.codec_id == CODEC_ID_MPEG1VIDEO || |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1437 st->codec.codec_id == CODEC_ID_MPEG2VIDEO) && |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1438 st->codec.sub_id == 2) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1439 if (st->codec_info_nb_frames >= 20) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1440 float coded_frame_rate, est_frame_rate; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1441 est_frame_rate = ((double)st->codec_info_nb_frames * AV_TIME_BASE) / |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1442 (double)st->codec_info_duration ; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1443 coded_frame_rate = (double)st->codec.frame_rate / |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1444 (double)st->codec.frame_rate_base; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1445 #if 0 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1446 printf("telecine: coded_frame_rate=%0.3f est_frame_rate=%0.3f\n", |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1447 coded_frame_rate, est_frame_rate); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1448 #endif |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1449 /* if we detect that it could be a telecine, we |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1450 signal it. It would be better to do it at a |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1451 higher level as it can change in a film */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1452 if (coded_frame_rate >= 24.97 && |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1453 (est_frame_rate >= 23.5 && est_frame_rate < 24.5)) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1454 st->r_frame_rate = 24024; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1455 st->r_frame_rate_base = 1001; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1456 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1457 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1458 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1459 /* if no real frame rate, use the codec one */ |
|
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
1460 if (!st->r_frame_rate){ |
|
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
1461 st->r_frame_rate = st->codec.frame_rate; |
|
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
1462 st->r_frame_rate_base = st->codec.frame_rate_base; |
|
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
1463 } |
| 0 | 1464 } |
| 1465 } | |
| 1466 | |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1467 av_estimate_timings(ic); |
| 0 | 1468 return ret; |
| 1469 } | |
| 1470 | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1471 /*******************************************************/ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1472 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1473 /** |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1474 * start playing a network based stream (e.g. RTSP stream) at the |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1475 * current position |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1476 */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1477 int av_read_play(AVFormatContext *s) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1478 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1479 if (!s->iformat->read_play) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1480 return AVERROR_NOTSUPP; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1481 return s->iformat->read_play(s); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1482 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1483 |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1484 /** |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1485 * pause a network based stream (e.g. RTSP stream). Use av_read_play() |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1486 * to resume it. |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1487 */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1488 int av_read_pause(AVFormatContext *s) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1489 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1490 if (!s->iformat->read_pause) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1491 return AVERROR_NOTSUPP; |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1492 return s->iformat->read_pause(s); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1493 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1494 |
| 0 | 1495 /** |
| 1496 * Close a media file (but not its codecs) | |
| 1497 * | |
| 1498 * @param s media file handle | |
| 1499 */ | |
| 1500 void av_close_input_file(AVFormatContext *s) | |
| 1501 { | |
| 172 | 1502 int i, must_open_file; |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1503 AVStream *st; |
| 0 | 1504 |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1505 /* free previous packet */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1506 if (s->cur_st && s->cur_st->parser) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1507 av_free_packet(&s->cur_pkt); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1508 |
| 0 | 1509 if (s->iformat->read_close) |
| 1510 s->iformat->read_close(s); | |
| 1511 for(i=0;i<s->nb_streams;i++) { | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1512 /* free all data in a stream component */ |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1513 st = s->streams[i]; |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1514 if (st->parser) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1515 av_parser_close(st->parser); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1516 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1517 av_free(st->index_entries); |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1518 av_free(st); |
| 0 | 1519 } |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1520 flush_packet_queue(s); |
| 172 | 1521 must_open_file = 1; |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1522 if (s->iformat->flags & AVFMT_NOFILE) { |
| 172 | 1523 must_open_file = 0; |
| 1524 } | |
| 1525 if (must_open_file) { | |
| 0 | 1526 url_fclose(&s->pb); |
| 1527 } | |
| 1528 av_freep(&s->priv_data); | |
| 1529 av_free(s); | |
| 1530 } | |
| 1531 | |
| 1532 /** | |
| 1533 * Add a new stream to a media file. Can only be called in the | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1534 * read_header function. If the flag AVFMTCTX_NOHEADER is in the |
|
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1535 * format context, then new streams can be added in read_packet too. |
| 0 | 1536 * |
| 1537 * | |
| 1538 * @param s media file handle | |
|
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1539 * @param id file format dependent stream id |
| 0 | 1540 */ |
| 1541 AVStream *av_new_stream(AVFormatContext *s, int id) | |
| 1542 { | |
| 1543 AVStream *st; | |
| 1544 | |
| 1545 if (s->nb_streams >= MAX_STREAMS) | |
| 1546 return NULL; | |
| 1547 | |
| 1548 st = av_mallocz(sizeof(AVStream)); | |
| 1549 if (!st) | |
| 1550 return NULL; | |
| 5 | 1551 avcodec_get_context_defaults(&st->codec); |
| 193 | 1552 if (s->iformat) { |
| 1553 /* no default bitrate if decoding */ | |
| 1554 st->codec.bit_rate = 0; | |
| 1555 } | |
| 0 | 1556 st->index = s->nb_streams; |
| 1557 st->id = id; | |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1558 st->start_time = AV_NOPTS_VALUE; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1559 st->duration = AV_NOPTS_VALUE; |
| 0 | 1560 s->streams[s->nb_streams++] = st; |
| 1561 return st; | |
| 1562 } | |
| 1563 | |
| 1564 /************************************************************/ | |
| 1565 /* output media file */ | |
| 1566 | |
| 20 | 1567 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
| 1568 { | |
| 1569 int ret; | |
| 32 | 1570 |
| 1571 if (s->oformat->priv_data_size > 0) { | |
| 1572 s->priv_data = av_mallocz(s->oformat->priv_data_size); | |
| 1573 if (!s->priv_data) | |
| 1574 return AVERROR_NOMEM; | |
| 1575 } else | |
| 1576 s->priv_data = NULL; | |
| 1577 | |
| 20 | 1578 if (s->oformat->set_parameters) { |
| 1579 ret = s->oformat->set_parameters(s, ap); | |
| 1580 if (ret < 0) | |
| 1581 return ret; | |
| 1582 } | |
| 1583 return 0; | |
| 1584 } | |
| 1585 | |
| 0 | 1586 /** |
| 1587 * allocate the stream private data and write the stream header to an | |
| 1588 * output media file | |
| 1589 * | |
| 1590 * @param s media file handle | |
| 1591 * @return 0 if OK. AVERROR_xxx if error. | |
| 1592 */ | |
| 1593 int av_write_header(AVFormatContext *s) | |
| 1594 { | |
| 1595 int ret, i; | |
| 1596 AVStream *st; | |
| 1597 | |
| 1598 /* default pts settings is MPEG like */ | |
| 1599 av_set_pts_info(s, 33, 1, 90000); | |
| 1600 ret = s->oformat->write_header(s); | |
| 1601 if (ret < 0) | |
| 1602 return ret; | |
| 1603 | |
| 1604 /* init PTS generation */ | |
| 1605 for(i=0;i<s->nb_streams;i++) { | |
| 1606 st = s->streams[i]; | |
| 1607 | |
| 1608 switch (st->codec.codec_type) { | |
| 1609 case CODEC_TYPE_AUDIO: | |
| 1610 av_frac_init(&st->pts, 0, 0, | |
| 65 | 1611 (int64_t)s->pts_num * st->codec.sample_rate); |
| 0 | 1612 break; |
| 1613 case CODEC_TYPE_VIDEO: | |
| 1614 av_frac_init(&st->pts, 0, 0, | |
| 65 | 1615 (int64_t)s->pts_num * st->codec.frame_rate); |
| 0 | 1616 break; |
| 1617 default: | |
| 1618 break; | |
| 1619 } | |
| 1620 } | |
| 1621 return 0; | |
| 1622 } | |
| 1623 | |
| 1624 /** | |
| 1625 * Write a packet to an output media file. The packet shall contain | |
| 1626 * one audio or video frame. | |
| 1627 * | |
| 1628 * @param s media file handle | |
| 1629 * @param stream_index stream index | |
| 1630 * @param buf buffer containing the frame data | |
| 1631 * @param size size of buffer | |
| 1632 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted. | |
| 1633 */ | |
| 1634 int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf, | |
| 1635 int size) | |
| 1636 { | |
| 1637 AVStream *st; | |
| 65 | 1638 int64_t pts_mask; |
| 0 | 1639 int ret, frame_size; |
| 1640 | |
| 1641 st = s->streams[stream_index]; | |
| 1642 pts_mask = (1LL << s->pts_wrap_bits) - 1; | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1643 ret = s->oformat->write_packet(s, stream_index, buf, size, |
| 0 | 1644 st->pts.val & pts_mask); |
| 1645 if (ret < 0) | |
| 1646 return ret; | |
| 1647 | |
| 1648 /* update pts */ | |
| 1649 switch (st->codec.codec_type) { | |
| 1650 case CODEC_TYPE_AUDIO: | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1651 frame_size = get_audio_frame_size(&st->codec, size); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1652 if (frame_size >= 0) { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1653 av_frac_add(&st->pts, |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1654 (int64_t)s->pts_den * frame_size); |
| 0 | 1655 } |
| 1656 break; | |
| 1657 case CODEC_TYPE_VIDEO: | |
| 1658 av_frac_add(&st->pts, | |
|
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
1659 (int64_t)s->pts_den * st->codec.frame_rate_base); |
| 0 | 1660 break; |
| 1661 default: | |
| 1662 break; | |
| 1663 } | |
| 1664 return ret; | |
| 1665 } | |
| 1666 | |
| 1667 /** | |
| 1668 * write the stream trailer to an output media file and and free the | |
| 1669 * file private data. | |
| 1670 * | |
| 1671 * @param s media file handle | |
| 1672 * @return 0 if OK. AVERROR_xxx if error. */ | |
| 1673 int av_write_trailer(AVFormatContext *s) | |
| 1674 { | |
| 1675 int ret; | |
| 1676 ret = s->oformat->write_trailer(s); | |
| 1677 av_freep(&s->priv_data); | |
| 1678 return ret; | |
| 1679 } | |
| 1680 | |
| 1681 /* "user interface" functions */ | |
| 1682 | |
| 1683 void dump_format(AVFormatContext *ic, | |
| 1684 int index, | |
| 1685 const char *url, | |
| 1686 int is_output) | |
| 1687 { | |
| 1688 int i, flags; | |
| 1689 char buf[256]; | |
| 1690 | |
| 1691 fprintf(stderr, "%s #%d, %s, %s '%s':\n", | |
| 1692 is_output ? "Output" : "Input", | |
| 1693 index, | |
| 1694 is_output ? ic->oformat->name : ic->iformat->name, | |
| 1695 is_output ? "to" : "from", url); | |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1696 if (!is_output) { |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1697 fprintf(stderr, " Duration: "); |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1698 if (ic->duration != AV_NOPTS_VALUE) { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1699 int hours, mins, secs, us; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1700 secs = ic->duration / AV_TIME_BASE; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1701 us = ic->duration % AV_TIME_BASE; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1702 mins = secs / 60; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1703 secs %= 60; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1704 hours = mins / 60; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1705 mins %= 60; |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1706 fprintf(stderr, "%02d:%02d:%02d.%01d", hours, mins, secs, |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1707 (10 * us) / AV_TIME_BASE); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1708 } else { |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1709 fprintf(stderr, "N/A"); |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1710 } |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1711 fprintf(stderr, ", bitrate: "); |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1712 if (ic->bit_rate) { |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1713 fprintf(stderr,"%d kb/s", ic->bit_rate / 1000); |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1714 } else { |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1715 fprintf(stderr, "N/A"); |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1716 } |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1717 fprintf(stderr, "\n"); |
|
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1718 } |
| 0 | 1719 for(i=0;i<ic->nb_streams;i++) { |
| 1720 AVStream *st = ic->streams[i]; | |
| 1721 avcodec_string(buf, sizeof(buf), &st->codec, is_output); | |
| 1722 fprintf(stderr, " Stream #%d.%d", index, i); | |
| 1723 /* the pid is an important information, so we display it */ | |
| 1724 /* XXX: add a generic system */ | |
| 1725 if (is_output) | |
| 1726 flags = ic->oformat->flags; | |
| 1727 else | |
| 1728 flags = ic->iformat->flags; | |
| 1729 if (flags & AVFMT_SHOW_IDS) { | |
| 1730 fprintf(stderr, "[0x%x]", st->id); | |
| 1731 } | |
| 1732 fprintf(stderr, ": %s\n", buf); | |
| 1733 } | |
| 1734 } | |
| 1735 | |
| 1736 typedef struct { | |
| 168 | 1737 const char *abv; |
| 0 | 1738 int width, height; |
| 168 | 1739 int frame_rate, frame_rate_base; |
| 1740 } AbvEntry; | |
| 0 | 1741 |
| 168 | 1742 static AbvEntry frame_abvs[] = { |
| 204 | 1743 { "ntsc", 720, 480, 30000, 1001 }, |
| 1744 { "pal", 720, 576, 25, 1 }, | |
| 1745 { "qntsc", 352, 240, 30000, 1001 }, /* VCD compliant ntsc */ | |
| 1746 { "qpal", 352, 288, 25, 1 }, /* VCD compliant pal */ | |
| 205 | 1747 { "sntsc", 640, 480, 30000, 1001 }, /* square pixel ntsc */ |
| 1748 { "spal", 768, 576, 25, 1 }, /* square pixel pal */ | |
| 168 | 1749 { "film", 352, 240, 24, 1 }, |
| 1750 { "ntsc-film", 352, 240, 24000, 1001 }, | |
| 1751 { "sqcif", 128, 96, 0, 0 }, | |
| 1752 { "qcif", 176, 144, 0, 0 }, | |
| 1753 { "cif", 352, 288, 0, 0 }, | |
| 1754 { "4cif", 704, 576, 0, 0 }, | |
| 0 | 1755 }; |
| 168 | 1756 |
| 0 | 1757 int parse_image_size(int *width_ptr, int *height_ptr, const char *str) |
| 1758 { | |
| 1759 int i; | |
| 168 | 1760 int n = sizeof(frame_abvs) / sizeof(AbvEntry); |
| 0 | 1761 const char *p; |
| 1762 int frame_width = 0, frame_height = 0; | |
| 1763 | |
| 1764 for(i=0;i<n;i++) { | |
| 168 | 1765 if (!strcmp(frame_abvs[i].abv, str)) { |
| 1766 frame_width = frame_abvs[i].width; | |
| 1767 frame_height = frame_abvs[i].height; | |
| 0 | 1768 break; |
| 1769 } | |
| 1770 } | |
| 1771 if (i == n) { | |
| 1772 p = str; | |
| 1773 frame_width = strtol(p, (char **)&p, 10); | |
| 1774 if (*p) | |
| 1775 p++; | |
| 1776 frame_height = strtol(p, (char **)&p, 10); | |
| 1777 } | |
| 1778 if (frame_width <= 0 || frame_height <= 0) | |
| 1779 return -1; | |
| 1780 *width_ptr = frame_width; | |
| 1781 *height_ptr = frame_height; | |
| 1782 return 0; | |
| 1783 } | |
| 1784 | |
| 168 | 1785 int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg) |
| 1786 { | |
| 1787 int i; | |
| 1788 char* cp; | |
| 1789 | |
| 1790 /* First, we check our abbreviation table */ | |
| 1791 for (i = 0; i < sizeof(frame_abvs)/sizeof(*frame_abvs); ++i) | |
| 1792 if (!strcmp(frame_abvs[i].abv, arg)) { | |
| 1793 *frame_rate = frame_abvs[i].frame_rate; | |
| 1794 *frame_rate_base = frame_abvs[i].frame_rate_base; | |
| 1795 return 0; | |
| 1796 } | |
| 1797 | |
| 1798 /* Then, we try to parse it as fraction */ | |
| 1799 cp = strchr(arg, '/'); | |
| 1800 if (cp) { | |
| 1801 char* cpp; | |
| 1802 *frame_rate = strtol(arg, &cpp, 10); | |
| 1803 if (cpp != arg || cpp == cp) | |
| 1804 *frame_rate_base = strtol(cp+1, &cpp, 10); | |
| 1805 else | |
| 1806 *frame_rate = 0; | |
| 1807 } | |
| 1808 else { | |
| 1809 /* Finally we give up and parse it as double */ | |
| 1810 *frame_rate_base = DEFAULT_FRAME_RATE_BASE; | |
| 1811 *frame_rate = (int)(strtod(arg, 0) * (*frame_rate_base) + 0.5); | |
| 1812 } | |
| 1813 if (!*frame_rate || !*frame_rate_base) | |
| 1814 return -1; | |
| 1815 else | |
| 1816 return 0; | |
| 1817 } | |
| 1818 | |
| 0 | 1819 /* Syntax: |
| 1820 * - If not a duration: | |
| 1821 * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]} | |
| 1822 * Time is localtime unless Z is suffixed to the end. In this case GMT | |
| 1823 * Return the date in micro seconds since 1970 | |
| 1824 * - If duration: | |
| 1825 * HH[:MM[:SS[.m...]]] | |
| 1826 * S+[.m...] | |
| 1827 */ | |
| 65 | 1828 int64_t parse_date(const char *datestr, int duration) |
| 0 | 1829 { |
| 1830 const char *p; | |
| 65 | 1831 int64_t t; |
| 0 | 1832 struct tm dt; |
| 1833 int i; | |
| 1834 static const char *date_fmt[] = { | |
| 1835 "%Y-%m-%d", | |
| 1836 "%Y%m%d", | |
| 1837 }; | |
| 1838 static const char *time_fmt[] = { | |
| 1839 "%H:%M:%S", | |
| 1840 "%H%M%S", | |
| 1841 }; | |
| 1842 const char *q; | |
| 1843 int is_utc, len; | |
| 1844 char lastch; | |
| 1845 time_t now = time(0); | |
| 1846 | |
| 1847 len = strlen(datestr); | |
| 1848 if (len > 0) | |
| 1849 lastch = datestr[len - 1]; | |
| 1850 else | |
| 1851 lastch = '\0'; | |
| 1852 is_utc = (lastch == 'z' || lastch == 'Z'); | |
| 1853 | |
| 1854 memset(&dt, 0, sizeof(dt)); | |
| 1855 | |
| 1856 p = datestr; | |
| 1857 q = NULL; | |
| 1858 if (!duration) { | |
| 1859 for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) { | |
|
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
229
diff
changeset
|
1860 q = small_strptime(p, date_fmt[i], &dt); |
| 0 | 1861 if (q) { |
| 1862 break; | |
| 1863 } | |
| 1864 } | |
| 1865 | |
| 1866 if (!q) { | |
| 1867 if (is_utc) { | |
| 1868 dt = *gmtime(&now); | |
| 1869 } else { | |
| 1870 dt = *localtime(&now); | |
| 1871 } | |
| 1872 dt.tm_hour = dt.tm_min = dt.tm_sec = 0; | |
| 1873 } else { | |
| 1874 p = q; | |
| 1875 } | |
| 1876 | |
| 1877 if (*p == 'T' || *p == 't' || *p == ' ') | |
| 1878 p++; | |
| 1879 | |
| 1880 for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) { | |
|
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
229
diff
changeset
|
1881 q = small_strptime(p, time_fmt[i], &dt); |
| 0 | 1882 if (q) { |
| 1883 break; | |
| 1884 } | |
| 1885 } | |
| 1886 } else { | |
|
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
229
diff
changeset
|
1887 q = small_strptime(p, time_fmt[0], &dt); |
| 0 | 1888 if (!q) { |
| 1889 dt.tm_sec = strtol(p, (char **)&q, 10); | |
| 1890 dt.tm_min = 0; | |
| 1891 dt.tm_hour = 0; | |
| 1892 } | |
| 1893 } | |
| 1894 | |
| 1895 /* Now we have all the fields that we can get */ | |
| 1896 if (!q) { | |
| 1897 if (duration) | |
| 1898 return 0; | |
| 1899 else | |
| 65 | 1900 return now * int64_t_C(1000000); |
| 0 | 1901 } |
| 1902 | |
| 1903 if (duration) { | |
| 1904 t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec; | |
| 1905 } else { | |
| 1906 dt.tm_isdst = -1; /* unknown */ | |
| 1907 if (is_utc) { | |
| 1908 t = mktimegm(&dt); | |
| 1909 } else { | |
| 1910 t = mktime(&dt); | |
| 1911 } | |
| 1912 } | |
| 1913 | |
| 1914 t *= 1000000; | |
| 1915 | |
| 1916 if (*q == '.') { | |
| 1917 int val, n; | |
| 1918 q++; | |
| 1919 for (val = 0, n = 100000; n >= 1; n /= 10, q++) { | |
| 1920 if (!isdigit(*q)) | |
| 1921 break; | |
| 1922 val += n * (*q - '0'); | |
| 1923 } | |
| 1924 t += val; | |
| 1925 } | |
| 1926 return t; | |
| 1927 } | |
| 1928 | |
| 1929 /* syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done. Return | |
| 1930 1 if found */ | |
| 1931 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info) | |
| 1932 { | |
| 1933 const char *p; | |
| 1934 char tag[128], *q; | |
| 1935 | |
| 1936 p = info; | |
| 1937 if (*p == '?') | |
| 1938 p++; | |
| 1939 for(;;) { | |
| 1940 q = tag; | |
| 1941 while (*p != '\0' && *p != '=' && *p != '&') { | |
| 1942 if ((q - tag) < sizeof(tag) - 1) | |
| 1943 *q++ = *p; | |
| 1944 p++; | |
| 1945 } | |
| 1946 *q = '\0'; | |
| 1947 q = arg; | |
| 1948 if (*p == '=') { | |
| 1949 p++; | |
| 1950 while (*p != '&' && *p != '\0') { | |
| 1951 if ((q - arg) < arg_size - 1) { | |
| 1952 if (*p == '+') | |
| 1953 *q++ = ' '; | |
| 1954 else | |
| 1955 *q++ = *p; | |
| 1956 } | |
| 1957 p++; | |
| 1958 } | |
| 1959 *q = '\0'; | |
| 1960 } | |
| 1961 if (!strcmp(tag, tag1)) | |
| 1962 return 1; | |
| 1963 if (*p != '&') | |
| 1964 break; | |
| 1965 p++; | |
| 1966 } | |
| 1967 return 0; | |
| 1968 } | |
| 1969 | |
| 1970 /* Return in 'buf' the path with '%d' replaced by number. Also handles | |
| 1971 the '%0nd' format where 'n' is the total number of digits and | |
| 1972 '%%'. Return 0 if OK, and -1 if format error */ | |
| 1973 int get_frame_filename(char *buf, int buf_size, | |
| 1974 const char *path, int number) | |
| 1975 { | |
| 1976 const char *p; | |
|
290
7a3ed84008ec
GCC 3.3.2 warnings patch by (Panagiotis Issaris <takis at lumumba dot luc dot ac dot be>)
michael
parents:
230
diff
changeset
|
1977 char *q, buf1[20], c; |
|
7a3ed84008ec
GCC 3.3.2 warnings patch by (Panagiotis Issaris <takis at lumumba dot luc dot ac dot be>)
michael
parents:
230
diff
changeset
|
1978 int nd, len, percentd_found; |
| 0 | 1979 |
| 1980 q = buf; | |
| 1981 p = path; | |
| 1982 percentd_found = 0; | |
| 1983 for(;;) { | |
| 1984 c = *p++; | |
| 1985 if (c == '\0') | |
| 1986 break; | |
| 1987 if (c == '%') { | |
|
9
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1988 do { |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1989 nd = 0; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1990 while (isdigit(*p)) { |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1991 nd = nd * 10 + *p++ - '0'; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1992 } |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1993 c = *p++; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1994 } while (isdigit(c)); |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1995 |
| 0 | 1996 switch(c) { |
| 1997 case '%': | |
| 1998 goto addchar; | |
| 1999 case 'd': | |
| 2000 if (percentd_found) | |
| 2001 goto fail; | |
| 2002 percentd_found = 1; | |
| 2003 snprintf(buf1, sizeof(buf1), "%0*d", nd, number); | |
| 2004 len = strlen(buf1); | |
| 2005 if ((q - buf + len) > buf_size - 1) | |
| 2006 goto fail; | |
| 2007 memcpy(q, buf1, len); | |
| 2008 q += len; | |
| 2009 break; | |
| 2010 default: | |
| 2011 goto fail; | |
| 2012 } | |
| 2013 } else { | |
| 2014 addchar: | |
| 2015 if ((q - buf) < buf_size - 1) | |
| 2016 *q++ = c; | |
| 2017 } | |
| 2018 } | |
| 2019 if (!percentd_found) | |
| 2020 goto fail; | |
| 2021 *q = '\0'; | |
| 2022 return 0; | |
| 2023 fail: | |
| 2024 *q = '\0'; | |
| 2025 return -1; | |
| 2026 } | |
| 2027 | |
| 2028 /** | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2029 * Print nice hexa dump of a buffer |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2030 * @param f stream for output |
| 0 | 2031 * @param buf buffer |
| 2032 * @param size buffer size | |
| 2033 */ | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2034 void av_hex_dump(FILE *f, uint8_t *buf, int size) |
| 0 | 2035 { |
| 2036 int len, i, j, c; | |
| 2037 | |
| 2038 for(i=0;i<size;i+=16) { | |
| 2039 len = size - i; | |
| 2040 if (len > 16) | |
| 2041 len = 16; | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2042 fprintf(f, "%08x ", i); |
| 0 | 2043 for(j=0;j<16;j++) { |
| 2044 if (j < len) | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2045 fprintf(f, " %02x", buf[i+j]); |
| 0 | 2046 else |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2047 fprintf(f, " "); |
| 0 | 2048 } |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2049 fprintf(f, " "); |
| 0 | 2050 for(j=0;j<len;j++) { |
| 2051 c = buf[i+j]; | |
| 2052 if (c < ' ' || c > '~') | |
| 2053 c = '.'; | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2054 fprintf(f, "%c", c); |
| 0 | 2055 } |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2056 fprintf(f, "\n"); |
| 0 | 2057 } |
| 2058 } | |
| 2059 | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2060 /** |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2061 * Print on 'f' a nice dump of a packet |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2062 * @param f stream for output |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2063 * @param pkt packet to dump |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2064 * @param dump_payload true if the payload must be displayed too |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2065 */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2066 void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2067 { |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2068 fprintf(f, "stream #%d:\n", pkt->stream_index); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2069 fprintf(f, " keyframe=%d\n", ((pkt->flags & PKT_FLAG_KEY) != 0)); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2070 fprintf(f, " duration=%0.3f\n", (double)pkt->duration / AV_TIME_BASE); |
| 333 | 2071 /* DTS is _always_ valid after av_read_frame() */ |
| 2072 fprintf(f, " dts="); | |
| 2073 if (pkt->dts == AV_NOPTS_VALUE) | |
| 2074 fprintf(f, "N/A"); | |
| 2075 else | |
| 2076 fprintf(f, "%0.3f", (double)pkt->dts / AV_TIME_BASE); | |
|
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2077 /* PTS may be not known if B frames are present */ |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2078 fprintf(f, " pts="); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2079 if (pkt->pts == AV_NOPTS_VALUE) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2080 fprintf(f, "N/A"); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2081 else |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2082 fprintf(f, "%0.3f", (double)pkt->pts / AV_TIME_BASE); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2083 fprintf(f, "\n"); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2084 fprintf(f, " size=%d\n", pkt->size); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2085 if (dump_payload) |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2086 av_hex_dump(f, pkt->data, pkt->size); |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2087 } |
|
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2088 |
| 0 | 2089 void url_split(char *proto, int proto_size, |
| 2090 char *hostname, int hostname_size, | |
| 2091 int *port_ptr, | |
| 2092 char *path, int path_size, | |
| 2093 const char *url) | |
| 2094 { | |
| 2095 const char *p; | |
| 2096 char *q; | |
| 2097 int port; | |
| 2098 | |
| 2099 port = -1; | |
| 2100 | |
| 2101 p = url; | |
| 2102 q = proto; | |
| 2103 while (*p != ':' && *p != '\0') { | |
| 2104 if ((q - proto) < proto_size - 1) | |
| 2105 *q++ = *p; | |
| 2106 p++; | |
| 2107 } | |
| 2108 if (proto_size > 0) | |
| 2109 *q = '\0'; | |
| 2110 if (*p == '\0') { | |
| 2111 if (proto_size > 0) | |
| 2112 proto[0] = '\0'; | |
| 2113 if (hostname_size > 0) | |
| 2114 hostname[0] = '\0'; | |
| 2115 p = url; | |
| 2116 } else { | |
| 2117 p++; | |
| 2118 if (*p == '/') | |
| 2119 p++; | |
| 2120 if (*p == '/') | |
| 2121 p++; | |
| 2122 q = hostname; | |
| 2123 while (*p != ':' && *p != '/' && *p != '?' && *p != '\0') { | |
| 2124 if ((q - hostname) < hostname_size - 1) | |
| 2125 *q++ = *p; | |
| 2126 p++; | |
| 2127 } | |
| 2128 if (hostname_size > 0) | |
| 2129 *q = '\0'; | |
| 2130 if (*p == ':') { | |
| 2131 p++; | |
| 2132 port = strtoul(p, (char **)&p, 10); | |
| 2133 } | |
| 2134 } | |
| 2135 if (port_ptr) | |
| 2136 *port_ptr = port; | |
| 2137 pstrcpy(path, path_size, p); | |
| 2138 } | |
| 2139 | |
| 2140 /** | |
| 2141 * Set the pts for a given stream | |
| 2142 * @param s stream | |
| 2143 * @param pts_wrap_bits number of bits effectively used by the pts | |
| 2144 * (used for wrap control, 33 is the value for MPEG) | |
| 2145 * @param pts_num numerator to convert to seconds (MPEG: 1) | |
| 2146 * @param pts_den denominator to convert to seconds (MPEG: 90000) | |
| 2147 */ | |
| 2148 void av_set_pts_info(AVFormatContext *s, int pts_wrap_bits, | |
| 2149 int pts_num, int pts_den) | |
| 2150 { | |
| 2151 s->pts_wrap_bits = pts_wrap_bits; | |
| 2152 s->pts_num = pts_num; | |
| 2153 s->pts_den = pts_den; | |
| 2154 } | |
| 2155 | |
| 2156 /* fraction handling */ | |
| 2157 | |
| 2158 /** | |
| 2159 * f = val + (num / den) + 0.5. 'num' is normalized so that it is such | |
| 2160 * as 0 <= num < den. | |
| 2161 * | |
| 2162 * @param f fractional number | |
| 2163 * @param val integer value | |
| 2164 * @param num must be >= 0 | |
| 2165 * @param den must be >= 1 | |
| 2166 */ | |
| 65 | 2167 void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den) |
| 0 | 2168 { |
| 2169 num += (den >> 1); | |
| 2170 if (num >= den) { | |
| 2171 val += num / den; | |
| 2172 num = num % den; | |
| 2173 } | |
| 2174 f->val = val; | |
| 2175 f->num = num; | |
| 2176 f->den = den; | |
| 2177 } | |
| 2178 | |
| 2179 /* set f to (val + 0.5) */ | |
| 65 | 2180 void av_frac_set(AVFrac *f, int64_t val) |
| 0 | 2181 { |
| 2182 f->val = val; | |
| 2183 f->num = f->den >> 1; | |
| 2184 } | |
| 2185 | |
| 2186 /** | |
| 2187 * Fractionnal addition to f: f = f + (incr / f->den) | |
| 2188 * | |
| 2189 * @param f fractional number | |
| 2190 * @param incr increment, can be positive or negative | |
| 2191 */ | |
| 65 | 2192 void av_frac_add(AVFrac *f, int64_t incr) |
| 0 | 2193 { |
| 65 | 2194 int64_t num, den; |
| 0 | 2195 |
| 2196 num = f->num + incr; | |
| 2197 den = f->den; | |
| 2198 if (num < 0) { | |
| 2199 f->val += num / den; | |
| 2200 num = num % den; | |
| 2201 if (num < 0) { | |
| 2202 num += den; | |
| 2203 f->val--; | |
| 2204 } | |
| 2205 } else if (num >= den) { | |
| 2206 f->val += num / den; | |
| 2207 num = num % den; | |
| 2208 } | |
| 2209 f->num = num; | |
| 2210 } | |
| 20 | 2211 |
| 2212 /** | |
| 2213 * register a new image format | |
| 2214 * @param img_fmt Image format descriptor | |
| 2215 */ | |
| 2216 void av_register_image_format(AVImageFormat *img_fmt) | |
| 2217 { | |
| 2218 AVImageFormat **p; | |
| 2219 | |
| 2220 p = &first_image_format; | |
| 2221 while (*p != NULL) p = &(*p)->next; | |
| 2222 *p = img_fmt; | |
| 2223 img_fmt->next = NULL; | |
| 2224 } | |
| 2225 | |
| 2226 /* guess image format */ | |
| 2227 AVImageFormat *av_probe_image_format(AVProbeData *pd) | |
| 2228 { | |
| 2229 AVImageFormat *fmt1, *fmt; | |
| 2230 int score, score_max; | |
| 2231 | |
| 2232 fmt = NULL; | |
| 2233 score_max = 0; | |
| 2234 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
| 2235 if (fmt1->img_probe) { | |
| 2236 score = fmt1->img_probe(pd); | |
| 2237 if (score > score_max) { | |
| 2238 score_max = score; | |
| 2239 fmt = fmt1; | |
| 2240 } | |
| 2241 } | |
| 2242 } | |
| 2243 return fmt; | |
| 2244 } | |
| 2245 | |
| 2246 AVImageFormat *guess_image_format(const char *filename) | |
| 2247 { | |
| 2248 AVImageFormat *fmt1; | |
| 2249 | |
| 2250 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
| 2251 if (fmt1->extensions && match_ext(filename, fmt1->extensions)) | |
| 2252 return fmt1; | |
| 2253 } | |
| 2254 return NULL; | |
| 2255 } | |
| 2256 | |
| 2257 /** | |
| 2258 * Read an image from a stream. | |
| 2259 * @param gb byte stream containing the image | |
| 2260 * @param fmt image format, NULL if probing is required | |
| 2261 */ | |
| 2262 int av_read_image(ByteIOContext *pb, const char *filename, | |
| 2263 AVImageFormat *fmt, | |
| 2264 int (*alloc_cb)(void *, AVImageInfo *info), void *opaque) | |
| 2265 { | |
| 2266 char buf[PROBE_BUF_SIZE]; | |
| 2267 AVProbeData probe_data, *pd = &probe_data; | |
| 2268 offset_t pos; | |
| 2269 int ret; | |
| 2270 | |
| 2271 if (!fmt) { | |
| 64 | 2272 pd->filename = filename; |
| 20 | 2273 pd->buf = buf; |
| 2274 pos = url_ftell(pb); | |
| 2275 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE); | |
| 2276 url_fseek(pb, pos, SEEK_SET); | |
| 2277 fmt = av_probe_image_format(pd); | |
| 2278 } | |
| 2279 if (!fmt) | |
| 2280 return AVERROR_NOFMT; | |
| 2281 ret = fmt->img_read(pb, alloc_cb, opaque); | |
| 2282 return ret; | |
| 2283 } | |
| 2284 | |
| 2285 /** | |
| 2286 * Write an image to a stream. | |
| 2287 * @param pb byte stream for the image output | |
| 2288 * @param fmt image format | |
| 2289 * @param img image data and informations | |
| 2290 */ | |
| 2291 int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img) | |
| 2292 { | |
| 2293 return fmt->img_write(pb, img); | |
| 2294 } | |
| 2295 |
