Mercurial > libavformat.hg
annotate utils.c @ 198:64dbc0b60dbe libavformat
win32 compile fix
| author | bellard |
|---|---|
| date | Sun, 24 Aug 2003 13:59:05 +0000 |
| parents | b8a3fb61d39a |
| children | 66a05c4f8350 |
| 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 #include <ctype.h> | |
|
22
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
21 #ifdef CONFIG_WIN32 |
|
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
22 #define strcasecmp _stricmp |
|
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
23 #include <sys/types.h> |
|
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
24 #include <sys/timeb.h> |
|
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
25 #elif defined(CONFIG_OS2) |
|
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
26 #include <string.h> |
|
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
27 #define strcasecmp stricmp |
|
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
28 #include <sys/time.h> |
|
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
29 #else |
| 0 | 30 #include <unistd.h> |
| 31 #include <fcntl.h> | |
| 32 #include <sys/time.h> | |
| 33 #endif | |
| 34 #include <time.h> | |
| 35 | |
| 36 #ifndef HAVE_STRPTIME | |
| 37 #include "strptime.h" | |
| 38 #endif | |
| 39 | |
| 40 AVInputFormat *first_iformat; | |
| 41 AVOutputFormat *first_oformat; | |
| 20 | 42 AVImageFormat *first_image_format; |
| 0 | 43 |
| 44 void av_register_input_format(AVInputFormat *format) | |
| 45 { | |
| 46 AVInputFormat **p; | |
| 47 p = &first_iformat; | |
| 48 while (*p != NULL) p = &(*p)->next; | |
| 49 *p = format; | |
| 50 format->next = NULL; | |
| 51 } | |
| 52 | |
| 53 void av_register_output_format(AVOutputFormat *format) | |
| 54 { | |
| 55 AVOutputFormat **p; | |
| 56 p = &first_oformat; | |
| 57 while (*p != NULL) p = &(*p)->next; | |
| 58 *p = format; | |
| 59 format->next = NULL; | |
| 60 } | |
| 61 | |
| 62 int match_ext(const char *filename, const char *extensions) | |
| 63 { | |
| 64 const char *ext, *p; | |
| 65 char ext1[32], *q; | |
| 66 | |
| 67 ext = strrchr(filename, '.'); | |
| 68 if (ext) { | |
| 69 ext++; | |
| 70 p = extensions; | |
| 71 for(;;) { | |
| 72 q = ext1; | |
| 73 while (*p != '\0' && *p != ',') | |
| 74 *q++ = *p++; | |
| 75 *q = '\0'; | |
| 76 if (!strcasecmp(ext1, ext)) | |
| 77 return 1; | |
| 78 if (*p == '\0') | |
| 79 break; | |
| 80 p++; | |
| 81 } | |
| 82 } | |
| 83 return 0; | |
| 84 } | |
| 85 | |
| 86 AVOutputFormat *guess_format(const char *short_name, const char *filename, | |
| 87 const char *mime_type) | |
| 88 { | |
| 89 AVOutputFormat *fmt, *fmt_found; | |
| 90 int score_max, score; | |
| 91 | |
| 20 | 92 /* specific test for image sequences */ |
| 21 | 93 if (!short_name && filename && |
| 94 filename_number_test(filename) >= 0 && | |
| 95 guess_image_format(filename)) { | |
| 20 | 96 return guess_format("image", NULL, NULL); |
| 97 } | |
| 98 | |
| 0 | 99 /* find the proper file type */ |
| 100 fmt_found = NULL; | |
| 101 score_max = 0; | |
| 102 fmt = first_oformat; | |
| 103 while (fmt != NULL) { | |
| 104 score = 0; | |
| 105 if (fmt->name && short_name && !strcmp(fmt->name, short_name)) | |
| 106 score += 100; | |
| 107 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type)) | |
| 108 score += 10; | |
| 109 if (filename && fmt->extensions && | |
| 110 match_ext(filename, fmt->extensions)) { | |
| 111 score += 5; | |
| 112 } | |
| 113 if (score > score_max) { | |
| 114 score_max = score; | |
| 115 fmt_found = fmt; | |
| 116 } | |
| 117 fmt = fmt->next; | |
| 118 } | |
| 119 return fmt_found; | |
| 120 } | |
| 121 | |
| 122 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename, | |
| 123 const char *mime_type) | |
| 124 { | |
| 125 AVOutputFormat *fmt = guess_format(short_name, filename, mime_type); | |
| 126 | |
| 127 if (fmt) { | |
| 128 AVOutputFormat *stream_fmt; | |
| 129 char stream_format_name[64]; | |
| 130 | |
| 131 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name); | |
| 132 stream_fmt = guess_format(stream_format_name, NULL, NULL); | |
| 133 | |
| 134 if (stream_fmt) | |
| 135 fmt = stream_fmt; | |
| 136 } | |
| 137 | |
| 138 return fmt; | |
| 139 } | |
| 140 | |
| 141 AVInputFormat *av_find_input_format(const char *short_name) | |
| 142 { | |
| 143 AVInputFormat *fmt; | |
| 144 for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) { | |
| 145 if (!strcmp(fmt->name, short_name)) | |
| 146 return fmt; | |
| 147 } | |
| 148 return NULL; | |
| 149 } | |
| 150 | |
| 151 /* memory handling */ | |
| 152 | |
| 153 /** | |
|
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
154 * Default packet destructor |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
155 */ |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
156 static void av_destruct_packet(AVPacket *pkt) |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
157 { |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
158 av_free(pkt->data); |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
159 pkt->data = NULL; pkt->size = 0; |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
160 } |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
161 |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
162 /** |
| 0 | 163 * Allocate the payload of a packet and intialized its fields to default values. |
| 164 * | |
| 165 * @param pkt packet | |
| 166 * @param size wanted payload size | |
| 167 * @return 0 if OK. AVERROR_xxx otherwise. | |
| 168 */ | |
| 169 int av_new_packet(AVPacket *pkt, int size) | |
| 170 { | |
|
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
171 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
|
172 if (!data) |
| 0 | 173 return AVERROR_NOMEM; |
|
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
174 memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
| 0 | 175 |
|
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
176 av_init_packet(pkt); |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
177 pkt->data = data; |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
178 pkt->size = size; |
|
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
179 pkt->destruct = av_destruct_packet; |
| 0 | 180 return 0; |
| 181 } | |
| 182 | |
| 183 /* fifo handling */ | |
| 184 | |
| 185 int fifo_init(FifoBuffer *f, int size) | |
| 186 { | |
| 187 f->buffer = av_malloc(size); | |
| 188 if (!f->buffer) | |
| 189 return -1; | |
| 190 f->end = f->buffer + size; | |
| 191 f->wptr = f->rptr = f->buffer; | |
| 192 return 0; | |
| 193 } | |
| 194 | |
| 195 void fifo_free(FifoBuffer *f) | |
| 196 { | |
| 197 av_free(f->buffer); | |
| 198 } | |
| 199 | |
| 65 | 200 int fifo_size(FifoBuffer *f, uint8_t *rptr) |
| 0 | 201 { |
| 202 int size; | |
| 203 | |
| 204 if (f->wptr >= rptr) { | |
| 205 size = f->wptr - rptr; | |
| 206 } else { | |
| 207 size = (f->end - rptr) + (f->wptr - f->buffer); | |
| 208 } | |
| 209 return size; | |
| 210 } | |
| 211 | |
| 212 /* get data from the fifo (return -1 if not enough data) */ | |
| 65 | 213 int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr) |
| 0 | 214 { |
| 65 | 215 uint8_t *rptr = *rptr_ptr; |
| 0 | 216 int size, len; |
| 217 | |
| 218 if (f->wptr >= rptr) { | |
| 219 size = f->wptr - rptr; | |
| 220 } else { | |
| 221 size = (f->end - rptr) + (f->wptr - f->buffer); | |
| 222 } | |
| 223 | |
| 224 if (size < buf_size) | |
| 225 return -1; | |
| 226 while (buf_size > 0) { | |
| 227 len = f->end - rptr; | |
| 228 if (len > buf_size) | |
| 229 len = buf_size; | |
| 230 memcpy(buf, rptr, len); | |
| 231 buf += len; | |
| 232 rptr += len; | |
| 233 if (rptr >= f->end) | |
| 234 rptr = f->buffer; | |
| 235 buf_size -= len; | |
| 236 } | |
| 237 *rptr_ptr = rptr; | |
| 238 return 0; | |
| 239 } | |
| 240 | |
| 65 | 241 void fifo_write(FifoBuffer *f, uint8_t *buf, int size, uint8_t **wptr_ptr) |
| 0 | 242 { |
| 243 int len; | |
| 65 | 244 uint8_t *wptr; |
| 0 | 245 wptr = *wptr_ptr; |
| 246 while (size > 0) { | |
| 247 len = f->end - wptr; | |
| 248 if (len > size) | |
| 249 len = size; | |
| 250 memcpy(wptr, buf, len); | |
| 251 wptr += len; | |
| 252 if (wptr >= f->end) | |
| 253 wptr = f->buffer; | |
| 254 buf += len; | |
| 255 size -= len; | |
| 256 } | |
| 257 *wptr_ptr = wptr; | |
| 258 } | |
| 259 | |
| 260 int filename_number_test(const char *filename) | |
| 261 { | |
| 262 char buf[1024]; | |
| 263 return get_frame_filename(buf, sizeof(buf), filename, 1); | |
| 264 } | |
| 265 | |
| 266 /* guess file format */ | |
| 267 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened) | |
| 268 { | |
| 269 AVInputFormat *fmt1, *fmt; | |
| 270 int score, score_max; | |
| 271 | |
| 272 fmt = NULL; | |
| 273 score_max = 0; | |
| 274 for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) { | |
| 275 if (!is_opened && !(fmt1->flags & AVFMT_NOFILE)) | |
| 276 continue; | |
| 277 score = 0; | |
| 278 if (fmt1->read_probe) { | |
| 279 score = fmt1->read_probe(pd); | |
| 280 } else if (fmt1->extensions) { | |
| 281 if (match_ext(pd->filename, fmt1->extensions)) { | |
| 282 score = 50; | |
| 283 } | |
| 284 } | |
| 285 if (score > score_max) { | |
| 286 score_max = score; | |
| 287 fmt = fmt1; | |
| 288 } | |
| 289 } | |
| 290 return fmt; | |
| 291 } | |
| 292 | |
| 293 /************************************************************/ | |
| 294 /* input media file */ | |
| 295 | |
| 296 #define PROBE_BUF_SIZE 2048 | |
| 297 | |
| 298 /** | |
| 299 * Open a media file as input. The codec are not opened. Only the file | |
| 300 * header (if present) is read. | |
| 301 * | |
| 302 * @param ic_ptr the opened media file handle is put here | |
| 303 * @param filename filename to open. | |
| 304 * @param fmt if non NULL, force the file format to use | |
| 305 * @param buf_size optional buffer size (zero if default is OK) | |
| 306 * @param ap additionnal parameters needed when opening the file (NULL if default) | |
| 307 * @return 0 if OK. AVERROR_xxx otherwise. | |
| 308 */ | |
| 309 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename, | |
| 310 AVInputFormat *fmt, | |
| 311 int buf_size, | |
| 312 AVFormatParameters *ap) | |
| 313 { | |
| 314 AVFormatContext *ic = NULL; | |
| 172 | 315 int err, must_open_file; |
| 0 | 316 char buf[PROBE_BUF_SIZE]; |
| 317 AVProbeData probe_data, *pd = &probe_data; | |
| 318 | |
| 319 ic = av_mallocz(sizeof(AVFormatContext)); | |
| 320 if (!ic) { | |
| 321 err = AVERROR_NOMEM; | |
| 322 goto fail; | |
| 323 } | |
|
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
|
324 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
|
325 ic->start_time = AV_NOPTS_VALUE; |
| 0 | 326 pstrcpy(ic->filename, sizeof(ic->filename), filename); |
| 327 pd->filename = ic->filename; | |
| 328 pd->buf = buf; | |
| 329 pd->buf_size = 0; | |
| 330 | |
| 331 if (!fmt) { | |
| 332 /* guess format if no file can be opened */ | |
| 333 fmt = av_probe_input_format(pd, 0); | |
| 334 } | |
| 335 | |
| 172 | 336 /* do not open file if the format does not need it. XXX: specific |
| 337 hack needed to handle RTSP/TCP */ | |
| 338 must_open_file = 1; | |
| 198 | 339 if ((fmt && (fmt->flags & AVFMT_NOFILE)) |
| 340 #ifdef CONFIG_NETWORK | |
| 341 || (fmt == &rtp_demux && !strcmp(filename, "null")) | |
| 342 #endif | |
| 343 ) { | |
| 172 | 344 must_open_file = 0; |
| 345 } | |
| 346 | |
| 347 if (!fmt || must_open_file) { | |
| 20 | 348 /* if no file needed do not try to open one */ |
| 0 | 349 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) { |
| 350 err = AVERROR_IO; | |
| 351 goto fail; | |
| 352 } | |
| 353 if (buf_size > 0) { | |
| 354 url_setbufsize(&ic->pb, buf_size); | |
| 355 } | |
| 356 if (!fmt) { | |
| 357 /* read probe data */ | |
| 358 pd->buf_size = get_buffer(&ic->pb, buf, PROBE_BUF_SIZE); | |
| 359 url_fseek(&ic->pb, 0, SEEK_SET); | |
| 360 } | |
| 361 } | |
| 362 | |
| 363 /* guess file format */ | |
| 364 if (!fmt) { | |
| 365 fmt = av_probe_input_format(pd, 1); | |
| 366 } | |
| 367 | |
| 368 /* if still no format found, error */ | |
| 369 if (!fmt) { | |
| 370 err = AVERROR_NOFMT; | |
|
90
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
85
diff
changeset
|
371 goto fail1; |
| 0 | 372 } |
| 373 | |
| 374 /* XXX: suppress this hack for redirectors */ | |
|
22
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
375 #ifdef CONFIG_NETWORK |
| 0 | 376 if (fmt == &redir_demux) { |
| 377 err = redir_open(ic_ptr, &ic->pb); | |
| 378 url_fclose(&ic->pb); | |
| 379 av_free(ic); | |
| 380 return err; | |
| 381 } | |
|
11
932b59c66c60
mingw patch by (Bill Eldridge <bill at rfa dot org>)
michaelni
parents:
9
diff
changeset
|
382 #endif |
| 0 | 383 |
| 384 ic->iformat = fmt; | |
| 385 | |
| 20 | 386 /* check filename in case of an image number is expected */ |
| 387 if (ic->iformat->flags & AVFMT_NEEDNUMBER) { | |
| 388 if (filename_number_test(ic->filename) < 0) { | |
| 389 err = AVERROR_NUMEXPECTED; | |
| 390 goto fail1; | |
| 391 } | |
| 392 } | |
| 393 | |
| 0 | 394 /* allocate private data */ |
| 32 | 395 if (fmt->priv_data_size > 0) { |
| 396 ic->priv_data = av_mallocz(fmt->priv_data_size); | |
| 397 if (!ic->priv_data) { | |
| 398 err = AVERROR_NOMEM; | |
|
90
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
85
diff
changeset
|
399 goto fail1; |
| 32 | 400 } |
| 401 } else | |
| 402 ic->priv_data = NULL; | |
| 0 | 403 |
| 404 /* default pts settings is MPEG like */ | |
| 405 av_set_pts_info(ic, 33, 1, 90000); | |
| 406 | |
| 407 err = ic->iformat->read_header(ic, ap); | |
| 408 if (err < 0) | |
| 409 goto fail1; | |
| 410 *ic_ptr = ic; | |
| 411 return 0; | |
| 412 fail1: | |
| 172 | 413 if (!fmt || must_open_file) { |
| 0 | 414 url_fclose(&ic->pb); |
| 415 } | |
| 416 fail: | |
| 417 if (ic) { | |
| 418 av_freep(&ic->priv_data); | |
| 419 } | |
| 420 av_free(ic); | |
| 421 *ic_ptr = NULL; | |
| 422 return err; | |
| 423 } | |
| 424 | |
| 425 /** | |
| 426 * Read a packet from a media file | |
| 427 * @param s media file handle | |
| 428 * @param pkt is filled | |
| 429 * @return 0 if OK. AVERROR_xxx if error. | |
| 430 */ | |
| 431 int av_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 432 { | |
| 433 AVPacketList *pktl; | |
| 434 | |
| 435 pktl = s->packet_buffer; | |
| 436 if (pktl) { | |
| 437 /* read packet from packet buffer, if there is data */ | |
| 438 *pkt = pktl->pkt; | |
| 439 s->packet_buffer = pktl->next; | |
| 440 av_free(pktl); | |
| 441 return 0; | |
| 442 } else { | |
| 443 return s->iformat->read_packet(s, pkt); | |
| 444 } | |
| 445 } | |
| 446 | |
|
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
|
447 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
448 /* 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
|
449 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
|
450 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
451 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
|
452 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
|
453 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
454 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
|
455 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
|
456 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
|
457 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
|
458 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
|
459 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
460 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
|
461 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
462 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
463 /* 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
|
464 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
|
465 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
|
466 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
467 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
|
468 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
|
469 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
|
470 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
471 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
|
472 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
|
473 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
|
474 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
|
475 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
|
476 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
|
477 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
|
478 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
|
479 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
|
480 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
|
481 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
|
482 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
483 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
484 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
485 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
|
486 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
|
487 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
|
488 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
|
489 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
|
490 /* 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
|
491 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
|
492 (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
|
493 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
494 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
495 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
496 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
497 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
498 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
499 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
|
500 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
501 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
|
502 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
|
503 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
504 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
|
505 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
|
506 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
|
507 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
|
508 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
|
509 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
|
510 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
511 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
512 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
513 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
514 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
|
515 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
516 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
|
517 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
|
518 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
|
519 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
520 /* 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
|
521 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
|
522 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
|
523 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
|
524 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
|
525 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
|
526 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
527 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
|
528 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
529 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
530 /* 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
|
531 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
|
532 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
|
533 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
|
534 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
|
535 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
|
536 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
|
537 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
|
538 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
|
539 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
|
540 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
|
541 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
|
542 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
|
543 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
544 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
545 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
546 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
547 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
548 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
549 static void flush_packet_queue(AVFormatContext *s) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
550 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
551 AVPacketList *pktl; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
552 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
553 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
|
554 pktl = s->packet_buffer; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
555 if (!pktl) |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
556 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
|
557 s->packet_buffer = pktl->next; |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
558 av_free(pktl); |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
559 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
560 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
561 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
562 #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
|
563 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
564 /* 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
|
565 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
|
566 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
567 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
|
568 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
|
569 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
|
570 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
|
571 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
|
572 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
573 /* 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
|
574 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
|
575 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
|
576 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
|
577 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
|
578 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
|
579 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
|
580 /* 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
|
581 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
|
582 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
|
583 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
|
584 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
|
585 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
586 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
|
587 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
|
588 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
589 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
|
590 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
|
591 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
|
592 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
|
593 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
|
594 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
|
595 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
|
596 st->start_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
|
597 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
598 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
|
599 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
600 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
601 /* 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
|
602 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
|
603 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
|
604 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
|
605 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
|
606 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
|
607 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
|
608 } |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
609 fprintf(stderr, "start=%lld\n", start_time); |
|
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
|
610 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
|
611 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
|
612 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
613 /* 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
|
614 /* 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
|
615 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
|
616 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
|
617 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
|
618 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
|
619 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
620 /* flush packet queue */ |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
621 flush_packet_queue(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
|
622 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
623 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
|
624 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
|
625 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
|
626 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
|
627 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
|
628 /* 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
|
629 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
|
630 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
|
631 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
|
632 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
|
633 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
634 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
|
635 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
|
636 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
637 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
|
638 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
|
639 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
|
640 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
|
641 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
|
642 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
|
643 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
|
644 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
|
645 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
|
646 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
|
647 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
|
648 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
|
649 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
650 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
651 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
|
652 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
653 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
654 /* 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
|
655 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
|
656 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
|
657 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
|
658 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
|
659 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
|
660 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
|
661 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
|
662 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
663 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
664 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
665 /* 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
|
666 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
|
667 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
|
668 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
|
669 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
|
670 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
|
671 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
|
672 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
673 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
674 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
675 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
|
676 /* 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
|
677 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
|
678 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
|
679 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
|
680 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
|
681 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
|
682 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
683 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
|
684 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
685 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
686 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
|
687 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
688 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
689 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
|
690 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
691 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
|
692 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
|
693 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
694 /* 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
|
695 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
|
696 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
|
697 } 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
|
698 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
|
699 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
|
700 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
|
701 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
|
702 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
703 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
|
704 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
705 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
|
706 /* 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
|
707 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
|
708 } 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
|
709 /* 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
|
710 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
|
711 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
|
712 } 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
|
713 /* 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
|
714 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
|
715 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
716 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
|
717 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
718 #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
|
719 { |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
720 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
|
721 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
|
722 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
|
723 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
|
724 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
|
725 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
|
726 (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
|
727 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
728 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
|
729 (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
|
730 (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
|
731 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
|
732 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
733 #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
|
734 } |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
735 |
| 0 | 736 /* state for codec information */ |
| 737 #define CSTATE_NOTFOUND 0 | |
| 738 #define CSTATE_DECODING 1 | |
| 739 #define CSTATE_FOUND 2 | |
| 740 | |
| 741 static int has_codec_parameters(AVCodecContext *enc) | |
| 742 { | |
| 743 int val; | |
| 744 switch(enc->codec_type) { | |
| 745 case CODEC_TYPE_AUDIO: | |
| 746 val = enc->sample_rate; | |
| 747 break; | |
| 748 case CODEC_TYPE_VIDEO: | |
| 749 val = enc->width; | |
| 750 break; | |
| 751 default: | |
| 752 val = 1; | |
| 753 break; | |
| 754 } | |
| 755 return (val != 0); | |
| 756 } | |
| 757 | |
| 758 /** | |
| 759 * Read the beginning of a media file to get stream information. This | |
| 760 * is useful for file formats with no headers such as MPEG. This | |
| 761 * function also compute the real frame rate in case of mpeg2 repeat | |
| 762 * frame mode. | |
| 763 * | |
| 764 * @param ic media file handle | |
| 765 * @return >=0 if OK. AVERROR_xxx if error. | |
| 766 */ | |
| 767 int av_find_stream_info(AVFormatContext *ic) | |
| 768 { | |
| 769 int i, count, ret, got_picture, size, read_size; | |
| 770 AVCodec *codec; | |
| 771 AVStream *st; | |
| 772 AVPacket *pkt; | |
| 7 | 773 AVFrame picture; |
| 0 | 774 AVPacketList *pktl=NULL, **ppktl; |
| 775 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2]; | |
| 65 | 776 uint8_t *ptr; |
| 0 | 777 int min_read_size, max_read_size; |
| 778 | |
| 779 /* typical mpeg ts rate is 40 Mbits. DVD rate is about 10 | |
|
127
b7ce3b3dc171
VOB stream patch ba (Brian Foley <bfoley at compsoc dot nuigalway dot ie>)
michaelni
parents:
90
diff
changeset
|
780 Mbits. We read at most 0.2 second of file to find all streams */ |
| 0 | 781 |
| 782 /* XXX: base it on stream bitrate when possible */ | |
| 783 if (ic->iformat == &mpegts_demux) { | |
| 784 /* maximum number of bytes we accept to read to find all the streams | |
| 785 in a file */ | |
|
127
b7ce3b3dc171
VOB stream patch ba (Brian Foley <bfoley at compsoc dot nuigalway dot ie>)
michaelni
parents:
90
diff
changeset
|
786 min_read_size = 6000000; |
| 0 | 787 } else { |
|
127
b7ce3b3dc171
VOB stream patch ba (Brian Foley <bfoley at compsoc dot nuigalway dot ie>)
michaelni
parents:
90
diff
changeset
|
788 min_read_size = 250000; |
| 0 | 789 } |
| 790 /* max read size is 2 seconds of video max */ | |
|
127
b7ce3b3dc171
VOB stream patch ba (Brian Foley <bfoley at compsoc dot nuigalway dot ie>)
michaelni
parents:
90
diff
changeset
|
791 max_read_size = min_read_size * 10; |
| 0 | 792 |
| 793 /* set initial codec state */ | |
| 794 for(i=0;i<ic->nb_streams;i++) { | |
| 795 st = ic->streams[i]; | |
| 796 if (has_codec_parameters(&st->codec)) | |
| 797 st->codec_info_state = CSTATE_FOUND; | |
| 798 else | |
| 799 st->codec_info_state = CSTATE_NOTFOUND; | |
| 800 st->codec_info_nb_repeat_frames = 0; | |
| 801 st->codec_info_nb_real_frames = 0; | |
| 802 } | |
| 803 | |
| 804 count = 0; | |
| 805 read_size = 0; | |
| 806 ppktl = &ic->packet_buffer; | |
| 807 for(;;) { | |
| 808 /* check if one codec still needs to be handled */ | |
| 809 for(i=0;i<ic->nb_streams;i++) { | |
| 810 st = ic->streams[i]; | |
| 811 if (st->codec_info_state != CSTATE_FOUND) | |
| 812 break; | |
| 813 } | |
| 814 if (i == ic->nb_streams) { | |
| 815 /* NOTE: if the format has no header, then we need to read | |
| 816 some packets to get most of the streams, so we cannot | |
| 817 stop here */ | |
| 818 if (!(ic->iformat->flags & AVFMT_NOHEADER) || | |
| 819 read_size >= min_read_size) { | |
| 820 /* if we found the info for all the codecs, we can stop */ | |
| 821 ret = count; | |
| 822 break; | |
| 823 } | |
| 824 } else { | |
| 825 /* we did not get all the codec info, but we read too much data */ | |
| 826 if (read_size >= max_read_size) { | |
| 827 ret = count; | |
| 828 break; | |
| 829 } | |
| 830 } | |
| 831 | |
| 832 pktl = av_mallocz(sizeof(AVPacketList)); | |
| 833 if (!pktl) { | |
| 834 ret = AVERROR_NOMEM; | |
| 835 break; | |
| 836 } | |
| 837 | |
| 838 /* add the packet in the buffered packet list */ | |
| 839 *ppktl = pktl; | |
| 840 ppktl = &pktl->next; | |
| 841 | |
| 842 /* NOTE: a new stream can be added there if no header in file | |
| 843 (AVFMT_NOHEADER) */ | |
| 844 pkt = &pktl->pkt; | |
| 845 if (ic->iformat->read_packet(ic, pkt) < 0) { | |
| 846 /* EOF or error */ | |
| 847 ret = -1; /* we could not have all the codec parameters before EOF */ | |
| 848 if ((ic->iformat->flags & AVFMT_NOHEADER) && | |
| 849 i == ic->nb_streams) | |
| 850 ret = 0; | |
| 851 break; | |
| 852 } | |
| 853 read_size += pkt->size; | |
| 854 | |
| 855 /* open new codecs */ | |
| 856 for(i=0;i<ic->nb_streams;i++) { | |
| 857 st = ic->streams[i]; | |
| 858 if (st->codec_info_state == CSTATE_NOTFOUND) { | |
| 859 /* set to found in case of error */ | |
| 860 st->codec_info_state = CSTATE_FOUND; | |
| 861 codec = avcodec_find_decoder(st->codec.codec_id); | |
| 862 if (codec) { | |
| 863 if(codec->capabilities & CODEC_CAP_TRUNCATED) | |
| 864 st->codec.flags |= CODEC_FLAG_TRUNCATED; | |
| 865 | |
| 866 ret = avcodec_open(&st->codec, codec); | |
| 867 if (ret >= 0) | |
| 868 st->codec_info_state = CSTATE_DECODING; | |
| 869 } | |
| 870 } | |
| 871 } | |
| 872 | |
| 873 st = ic->streams[pkt->stream_index]; | |
| 874 if (st->codec_info_state == CSTATE_DECODING) { | |
| 875 /* decode the data and update codec parameters */ | |
| 876 ptr = pkt->data; | |
| 877 size = pkt->size; | |
| 878 while (size > 0) { | |
| 879 switch(st->codec.codec_type) { | |
| 880 case CODEC_TYPE_VIDEO: | |
| 881 ret = avcodec_decode_video(&st->codec, &picture, | |
| 882 &got_picture, ptr, size); | |
| 883 break; | |
| 884 case CODEC_TYPE_AUDIO: | |
| 885 ret = avcodec_decode_audio(&st->codec, samples, | |
| 886 &got_picture, ptr, size); | |
| 887 break; | |
| 888 default: | |
| 889 ret = -1; | |
| 890 break; | |
| 891 } | |
| 892 if (ret < 0) { | |
| 893 /* if error, simply ignore because another packet | |
| 894 may be OK */ | |
| 895 break; | |
| 896 } | |
| 897 if (got_picture) { | |
| 898 /* we got the parameters - now we can stop | |
| 899 examining this stream */ | |
| 900 /* XXX: add a codec info so that we can decide if | |
| 901 the codec can repeat frames */ | |
| 902 if (st->codec.codec_id == CODEC_ID_MPEG1VIDEO && | |
| 903 ic->iformat != &mpegts_demux && | |
| 904 st->codec.sub_id == 2) { | |
| 905 /* for mpeg2 video, we want to know the real | |
| 906 frame rate, so we decode 40 frames. In mpeg | |
| 907 TS case we do not do it because it would be | |
| 908 too long */ | |
| 909 st->codec_info_nb_real_frames++; | |
|
70
e851fa232508
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
65
diff
changeset
|
910 st->codec_info_nb_repeat_frames += st->codec.coded_frame->repeat_pict; |
| 0 | 911 #if 0 |
| 912 /* XXX: testing */ | |
| 913 if ((st->codec_info_nb_real_frames % 24) == 23) { | |
| 914 st->codec_info_nb_repeat_frames += 2; | |
| 915 } | |
| 916 #endif | |
| 917 /* stop after 40 frames */ | |
| 918 if (st->codec_info_nb_real_frames >= 40) { | |
|
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
919 av_reduce( |
|
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
920 &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
|
921 &st->r_frame_rate_base, |
|
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
922 (int64_t)st->codec.frame_rate * st->codec_info_nb_real_frames, |
|
162
35386fc4d47d
mpeg1 bad frame_rate_base fix by (Arthur van Hoff (javanator))
michaelni
parents:
127
diff
changeset
|
923 (st->codec_info_nb_real_frames + (st->codec_info_nb_repeat_frames >> 1)) * st->codec.frame_rate_base, |
|
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
924 1<<30); |
| 0 | 925 goto close_codec; |
| 926 } | |
| 927 } else { | |
| 928 close_codec: | |
| 929 st->codec_info_state = CSTATE_FOUND; | |
| 930 avcodec_close(&st->codec); | |
| 931 break; | |
| 932 } | |
| 933 } | |
| 934 ptr += ret; | |
| 935 size -= ret; | |
| 936 } | |
| 937 } | |
| 938 count++; | |
| 939 } | |
| 940 | |
| 941 /* close each codec if there are opened */ | |
| 942 for(i=0;i<ic->nb_streams;i++) { | |
| 943 st = ic->streams[i]; | |
| 944 if (st->codec_info_state == CSTATE_DECODING) | |
| 945 avcodec_close(&st->codec); | |
| 946 } | |
| 947 | |
| 948 /* set real frame rate info */ | |
| 949 for(i=0;i<ic->nb_streams;i++) { | |
| 950 st = ic->streams[i]; | |
| 951 if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
|
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
952 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
|
953 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
|
954 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
|
955 } |
| 0 | 956 } |
| 957 } | |
| 958 | |
|
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
|
959 |
|
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
960 av_estimate_timings(ic); |
| 0 | 961 return ret; |
| 962 } | |
| 963 | |
| 964 /** | |
| 965 * Close a media file (but not its codecs) | |
| 966 * | |
| 967 * @param s media file handle | |
| 968 */ | |
| 969 void av_close_input_file(AVFormatContext *s) | |
| 970 { | |
| 172 | 971 int i, must_open_file; |
| 0 | 972 |
| 973 if (s->iformat->read_close) | |
| 974 s->iformat->read_close(s); | |
| 975 for(i=0;i<s->nb_streams;i++) { | |
| 976 av_free(s->streams[i]); | |
| 977 } | |
| 978 if (s->packet_buffer) { | |
| 979 AVPacketList *p, *p1; | |
| 980 p = s->packet_buffer; | |
| 981 while (p != NULL) { | |
| 982 p1 = p->next; | |
| 983 av_free_packet(&p->pkt); | |
| 984 av_free(p); | |
| 985 p = p1; | |
| 986 } | |
| 987 s->packet_buffer = NULL; | |
| 988 } | |
| 172 | 989 must_open_file = 1; |
| 198 | 990 if ((s->iformat->flags & AVFMT_NOFILE) |
| 991 #ifdef CONFIG_NETWORK | |
| 992 || (s->iformat == &rtp_demux && !strcmp(s->filename, "null")) | |
| 993 #endif | |
| 994 ) { | |
| 172 | 995 must_open_file = 0; |
| 996 } | |
| 997 if (must_open_file) { | |
| 0 | 998 url_fclose(&s->pb); |
| 999 } | |
| 1000 av_freep(&s->priv_data); | |
| 1001 av_free(s); | |
| 1002 } | |
| 1003 | |
| 1004 /** | |
| 1005 * Add a new stream to a media file. Can only be called in the | |
| 1006 * read_header function. If the flag AVFMT_NOHEADER is in the format | |
| 1007 * description, then new streams can be added in read_packet too. | |
| 1008 * | |
| 1009 * | |
| 1010 * @param s media file handle | |
| 1011 * @param id file format dependent stream id | |
| 1012 */ | |
| 1013 AVStream *av_new_stream(AVFormatContext *s, int id) | |
| 1014 { | |
| 1015 AVStream *st; | |
| 1016 | |
| 1017 if (s->nb_streams >= MAX_STREAMS) | |
| 1018 return NULL; | |
| 1019 | |
| 1020 st = av_mallocz(sizeof(AVStream)); | |
| 1021 if (!st) | |
| 1022 return NULL; | |
| 5 | 1023 avcodec_get_context_defaults(&st->codec); |
| 193 | 1024 if (s->iformat) { |
| 1025 /* no default bitrate if decoding */ | |
| 1026 st->codec.bit_rate = 0; | |
| 1027 } | |
| 0 | 1028 st->index = s->nb_streams; |
| 1029 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
|
1030 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
|
1031 st->duration = AV_NOPTS_VALUE; |
| 0 | 1032 s->streams[s->nb_streams++] = st; |
| 1033 return st; | |
| 1034 } | |
| 1035 | |
| 1036 /************************************************************/ | |
| 1037 /* output media file */ | |
| 1038 | |
| 20 | 1039 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
| 1040 { | |
| 1041 int ret; | |
| 32 | 1042 |
| 1043 if (s->oformat->priv_data_size > 0) { | |
| 1044 s->priv_data = av_mallocz(s->oformat->priv_data_size); | |
| 1045 if (!s->priv_data) | |
| 1046 return AVERROR_NOMEM; | |
| 1047 } else | |
| 1048 s->priv_data = NULL; | |
| 1049 | |
| 20 | 1050 if (s->oformat->set_parameters) { |
| 1051 ret = s->oformat->set_parameters(s, ap); | |
| 1052 if (ret < 0) | |
| 1053 return ret; | |
| 1054 } | |
| 1055 return 0; | |
| 1056 } | |
| 1057 | |
| 0 | 1058 /** |
| 1059 * allocate the stream private data and write the stream header to an | |
| 1060 * output media file | |
| 1061 * | |
| 1062 * @param s media file handle | |
| 1063 * @return 0 if OK. AVERROR_xxx if error. | |
| 1064 */ | |
| 1065 int av_write_header(AVFormatContext *s) | |
| 1066 { | |
| 1067 int ret, i; | |
| 1068 AVStream *st; | |
| 1069 | |
| 1070 /* default pts settings is MPEG like */ | |
| 1071 av_set_pts_info(s, 33, 1, 90000); | |
| 1072 ret = s->oformat->write_header(s); | |
| 1073 if (ret < 0) | |
| 1074 return ret; | |
| 1075 | |
| 1076 /* init PTS generation */ | |
| 1077 for(i=0;i<s->nb_streams;i++) { | |
| 1078 st = s->streams[i]; | |
| 1079 | |
| 1080 switch (st->codec.codec_type) { | |
| 1081 case CODEC_TYPE_AUDIO: | |
| 1082 av_frac_init(&st->pts, 0, 0, | |
| 65 | 1083 (int64_t)s->pts_num * st->codec.sample_rate); |
| 0 | 1084 break; |
| 1085 case CODEC_TYPE_VIDEO: | |
| 1086 av_frac_init(&st->pts, 0, 0, | |
| 65 | 1087 (int64_t)s->pts_num * st->codec.frame_rate); |
| 0 | 1088 break; |
| 1089 default: | |
| 1090 break; | |
| 1091 } | |
| 1092 } | |
| 1093 return 0; | |
| 1094 } | |
| 1095 | |
| 1096 /** | |
| 1097 * Write a packet to an output media file. The packet shall contain | |
| 1098 * one audio or video frame. | |
| 1099 * | |
| 1100 * @param s media file handle | |
| 1101 * @param stream_index stream index | |
| 1102 * @param buf buffer containing the frame data | |
| 1103 * @param size size of buffer | |
| 1104 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted. | |
| 1105 */ | |
| 1106 int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf, | |
| 1107 int size) | |
| 1108 { | |
| 1109 AVStream *st; | |
| 65 | 1110 int64_t pts_mask; |
| 0 | 1111 int ret, frame_size; |
| 1112 | |
| 1113 st = s->streams[stream_index]; | |
| 1114 pts_mask = (1LL << s->pts_wrap_bits) - 1; | |
| 1115 ret = s->oformat->write_packet(s, stream_index, (uint8_t *)buf, size, | |
| 1116 st->pts.val & pts_mask); | |
| 1117 if (ret < 0) | |
| 1118 return ret; | |
| 1119 | |
| 1120 /* update pts */ | |
| 1121 switch (st->codec.codec_type) { | |
| 1122 case CODEC_TYPE_AUDIO: | |
| 1123 if (st->codec.frame_size <= 1) { | |
| 1124 frame_size = size / st->codec.channels; | |
| 1125 /* specific hack for pcm codecs because no frame size is provided */ | |
| 1126 switch(st->codec.codec_id) { | |
| 1127 case CODEC_ID_PCM_S16LE: | |
| 1128 case CODEC_ID_PCM_S16BE: | |
| 1129 case CODEC_ID_PCM_U16LE: | |
| 1130 case CODEC_ID_PCM_U16BE: | |
| 1131 frame_size >>= 1; | |
| 1132 break; | |
| 1133 default: | |
| 1134 break; | |
| 1135 } | |
| 1136 } else { | |
| 1137 frame_size = st->codec.frame_size; | |
| 1138 } | |
| 1139 av_frac_add(&st->pts, | |
| 65 | 1140 (int64_t)s->pts_den * frame_size); |
| 0 | 1141 break; |
| 1142 case CODEC_TYPE_VIDEO: | |
| 1143 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
|
1144 (int64_t)s->pts_den * st->codec.frame_rate_base); |
| 0 | 1145 break; |
| 1146 default: | |
| 1147 break; | |
| 1148 } | |
| 1149 return ret; | |
| 1150 } | |
| 1151 | |
| 1152 /** | |
| 1153 * write the stream trailer to an output media file and and free the | |
| 1154 * file private data. | |
| 1155 * | |
| 1156 * @param s media file handle | |
| 1157 * @return 0 if OK. AVERROR_xxx if error. */ | |
| 1158 int av_write_trailer(AVFormatContext *s) | |
| 1159 { | |
| 1160 int ret; | |
| 1161 ret = s->oformat->write_trailer(s); | |
| 1162 av_freep(&s->priv_data); | |
| 1163 return ret; | |
| 1164 } | |
| 1165 | |
| 1166 /* "user interface" functions */ | |
| 1167 | |
| 1168 void dump_format(AVFormatContext *ic, | |
| 1169 int index, | |
| 1170 const char *url, | |
| 1171 int is_output) | |
| 1172 { | |
| 1173 int i, flags; | |
| 1174 char buf[256]; | |
| 1175 | |
| 1176 fprintf(stderr, "%s #%d, %s, %s '%s':\n", | |
| 1177 is_output ? "Output" : "Input", | |
| 1178 index, | |
| 1179 is_output ? ic->oformat->name : ic->iformat->name, | |
| 1180 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
|
1181 if (!is_output) { |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1182 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
|
1183 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
|
1184 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
|
1185 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
|
1186 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
|
1187 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
|
1188 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
|
1189 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
|
1190 mins %= 60; |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1191 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
|
1192 (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
|
1193 } else { |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1194 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
|
1195 } |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1196 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
|
1197 if (ic->bit_rate) { |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1198 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
|
1199 } else { |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1200 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
|
1201 } |
|
197
b8a3fb61d39a
all human-readable output should go into stderr for now. We really
romansh
parents:
193
diff
changeset
|
1202 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
|
1203 } |
| 0 | 1204 for(i=0;i<ic->nb_streams;i++) { |
| 1205 AVStream *st = ic->streams[i]; | |
| 1206 avcodec_string(buf, sizeof(buf), &st->codec, is_output); | |
| 1207 fprintf(stderr, " Stream #%d.%d", index, i); | |
| 1208 /* the pid is an important information, so we display it */ | |
| 1209 /* XXX: add a generic system */ | |
| 1210 if (is_output) | |
| 1211 flags = ic->oformat->flags; | |
| 1212 else | |
| 1213 flags = ic->iformat->flags; | |
| 1214 if (flags & AVFMT_SHOW_IDS) { | |
| 1215 fprintf(stderr, "[0x%x]", st->id); | |
| 1216 } | |
| 1217 fprintf(stderr, ": %s\n", buf); | |
| 1218 } | |
| 1219 } | |
| 1220 | |
| 1221 typedef struct { | |
| 168 | 1222 const char *abv; |
| 0 | 1223 int width, height; |
| 168 | 1224 int frame_rate, frame_rate_base; |
| 1225 } AbvEntry; | |
| 0 | 1226 |
| 168 | 1227 static AbvEntry frame_abvs[] = { |
| 1228 { "ntsc", 352, 240, 30000, 1001 }, | |
| 1229 { "pal", 352, 288, 25, 1 }, | |
| 1230 { "film", 352, 240, 24, 1 }, | |
| 1231 { "ntsc-film", 352, 240, 24000, 1001 }, | |
| 1232 { "sqcif", 128, 96, 0, 0 }, | |
| 1233 { "qcif", 176, 144, 0, 0 }, | |
| 1234 { "cif", 352, 288, 0, 0 }, | |
| 1235 { "4cif", 704, 576, 0, 0 }, | |
| 0 | 1236 }; |
| 168 | 1237 |
| 0 | 1238 int parse_image_size(int *width_ptr, int *height_ptr, const char *str) |
| 1239 { | |
| 1240 int i; | |
| 168 | 1241 int n = sizeof(frame_abvs) / sizeof(AbvEntry); |
| 0 | 1242 const char *p; |
| 1243 int frame_width = 0, frame_height = 0; | |
| 1244 | |
| 1245 for(i=0;i<n;i++) { | |
| 168 | 1246 if (!strcmp(frame_abvs[i].abv, str)) { |
| 1247 frame_width = frame_abvs[i].width; | |
| 1248 frame_height = frame_abvs[i].height; | |
| 0 | 1249 break; |
| 1250 } | |
| 1251 } | |
| 1252 if (i == n) { | |
| 1253 p = str; | |
| 1254 frame_width = strtol(p, (char **)&p, 10); | |
| 1255 if (*p) | |
| 1256 p++; | |
| 1257 frame_height = strtol(p, (char **)&p, 10); | |
| 1258 } | |
| 1259 if (frame_width <= 0 || frame_height <= 0) | |
| 1260 return -1; | |
| 1261 *width_ptr = frame_width; | |
| 1262 *height_ptr = frame_height; | |
| 1263 return 0; | |
| 1264 } | |
| 1265 | |
| 168 | 1266 int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg) |
| 1267 { | |
| 1268 int i; | |
| 1269 char* cp; | |
| 1270 | |
| 1271 /* First, we check our abbreviation table */ | |
| 1272 for (i = 0; i < sizeof(frame_abvs)/sizeof(*frame_abvs); ++i) | |
| 1273 if (!strcmp(frame_abvs[i].abv, arg)) { | |
| 1274 *frame_rate = frame_abvs[i].frame_rate; | |
| 1275 *frame_rate_base = frame_abvs[i].frame_rate_base; | |
| 1276 return 0; | |
| 1277 } | |
| 1278 | |
| 1279 /* Then, we try to parse it as fraction */ | |
| 1280 cp = strchr(arg, '/'); | |
| 1281 if (cp) { | |
| 1282 char* cpp; | |
| 1283 *frame_rate = strtol(arg, &cpp, 10); | |
| 1284 if (cpp != arg || cpp == cp) | |
| 1285 *frame_rate_base = strtol(cp+1, &cpp, 10); | |
| 1286 else | |
| 1287 *frame_rate = 0; | |
| 1288 } | |
| 1289 else { | |
| 1290 /* Finally we give up and parse it as double */ | |
| 1291 *frame_rate_base = DEFAULT_FRAME_RATE_BASE; | |
| 1292 *frame_rate = (int)(strtod(arg, 0) * (*frame_rate_base) + 0.5); | |
| 1293 } | |
| 1294 if (!*frame_rate || !*frame_rate_base) | |
| 1295 return -1; | |
| 1296 else | |
| 1297 return 0; | |
| 1298 } | |
| 1299 | |
| 65 | 1300 int64_t av_gettime(void) |
| 0 | 1301 { |
| 1302 #ifdef CONFIG_WIN32 | |
| 1303 struct _timeb tb; | |
| 1304 _ftime(&tb); | |
| 65 | 1305 return ((int64_t)tb.time * int64_t_C(1000) + (int64_t)tb.millitm) * int64_t_C(1000); |
| 0 | 1306 #else |
| 1307 struct timeval tv; | |
| 1308 gettimeofday(&tv,NULL); | |
| 65 | 1309 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec; |
| 0 | 1310 #endif |
| 1311 } | |
| 1312 | |
| 1313 static time_t mktimegm(struct tm *tm) | |
| 1314 { | |
| 1315 time_t t; | |
| 1316 | |
| 1317 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday; | |
| 1318 | |
| 1319 if (m < 3) { | |
| 1320 m += 12; | |
| 1321 y--; | |
| 1322 } | |
| 1323 | |
| 1324 t = 86400 * | |
| 1325 (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469); | |
| 1326 | |
| 1327 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec; | |
| 1328 | |
| 1329 return t; | |
| 1330 } | |
| 1331 | |
| 1332 /* Syntax: | |
| 1333 * - If not a duration: | |
| 1334 * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]} | |
| 1335 * Time is localtime unless Z is suffixed to the end. In this case GMT | |
| 1336 * Return the date in micro seconds since 1970 | |
| 1337 * - If duration: | |
| 1338 * HH[:MM[:SS[.m...]]] | |
| 1339 * S+[.m...] | |
| 1340 */ | |
| 65 | 1341 int64_t parse_date(const char *datestr, int duration) |
| 0 | 1342 { |
| 1343 const char *p; | |
| 65 | 1344 int64_t t; |
| 0 | 1345 struct tm dt; |
| 1346 int i; | |
| 1347 static const char *date_fmt[] = { | |
| 1348 "%Y-%m-%d", | |
| 1349 "%Y%m%d", | |
| 1350 }; | |
| 1351 static const char *time_fmt[] = { | |
| 1352 "%H:%M:%S", | |
| 1353 "%H%M%S", | |
| 1354 }; | |
| 1355 const char *q; | |
| 1356 int is_utc, len; | |
| 1357 char lastch; | |
| 1358 time_t now = time(0); | |
| 1359 | |
| 1360 len = strlen(datestr); | |
| 1361 if (len > 0) | |
| 1362 lastch = datestr[len - 1]; | |
| 1363 else | |
| 1364 lastch = '\0'; | |
| 1365 is_utc = (lastch == 'z' || lastch == 'Z'); | |
| 1366 | |
| 1367 memset(&dt, 0, sizeof(dt)); | |
| 1368 | |
| 1369 p = datestr; | |
| 1370 q = NULL; | |
| 1371 if (!duration) { | |
| 1372 for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) { | |
| 1373 q = strptime(p, date_fmt[i], &dt); | |
| 1374 if (q) { | |
| 1375 break; | |
| 1376 } | |
| 1377 } | |
| 1378 | |
| 1379 if (!q) { | |
| 1380 if (is_utc) { | |
| 1381 dt = *gmtime(&now); | |
| 1382 } else { | |
| 1383 dt = *localtime(&now); | |
| 1384 } | |
| 1385 dt.tm_hour = dt.tm_min = dt.tm_sec = 0; | |
| 1386 } else { | |
| 1387 p = q; | |
| 1388 } | |
| 1389 | |
| 1390 if (*p == 'T' || *p == 't' || *p == ' ') | |
| 1391 p++; | |
| 1392 | |
| 1393 for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) { | |
| 1394 q = strptime(p, time_fmt[i], &dt); | |
| 1395 if (q) { | |
| 1396 break; | |
| 1397 } | |
| 1398 } | |
| 1399 } else { | |
| 1400 q = strptime(p, time_fmt[0], &dt); | |
| 1401 if (!q) { | |
| 1402 dt.tm_sec = strtol(p, (char **)&q, 10); | |
| 1403 dt.tm_min = 0; | |
| 1404 dt.tm_hour = 0; | |
| 1405 } | |
| 1406 } | |
| 1407 | |
| 1408 /* Now we have all the fields that we can get */ | |
| 1409 if (!q) { | |
| 1410 if (duration) | |
| 1411 return 0; | |
| 1412 else | |
| 65 | 1413 return now * int64_t_C(1000000); |
| 0 | 1414 } |
| 1415 | |
| 1416 if (duration) { | |
| 1417 t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec; | |
| 1418 } else { | |
| 1419 dt.tm_isdst = -1; /* unknown */ | |
| 1420 if (is_utc) { | |
| 1421 t = mktimegm(&dt); | |
| 1422 } else { | |
| 1423 t = mktime(&dt); | |
| 1424 } | |
| 1425 } | |
| 1426 | |
| 1427 t *= 1000000; | |
| 1428 | |
| 1429 if (*q == '.') { | |
| 1430 int val, n; | |
| 1431 q++; | |
| 1432 for (val = 0, n = 100000; n >= 1; n /= 10, q++) { | |
| 1433 if (!isdigit(*q)) | |
| 1434 break; | |
| 1435 val += n * (*q - '0'); | |
| 1436 } | |
| 1437 t += val; | |
| 1438 } | |
| 1439 return t; | |
| 1440 } | |
| 1441 | |
| 1442 /* syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done. Return | |
| 1443 1 if found */ | |
| 1444 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info) | |
| 1445 { | |
| 1446 const char *p; | |
| 1447 char tag[128], *q; | |
| 1448 | |
| 1449 p = info; | |
| 1450 if (*p == '?') | |
| 1451 p++; | |
| 1452 for(;;) { | |
| 1453 q = tag; | |
| 1454 while (*p != '\0' && *p != '=' && *p != '&') { | |
| 1455 if ((q - tag) < sizeof(tag) - 1) | |
| 1456 *q++ = *p; | |
| 1457 p++; | |
| 1458 } | |
| 1459 *q = '\0'; | |
| 1460 q = arg; | |
| 1461 if (*p == '=') { | |
| 1462 p++; | |
| 1463 while (*p != '&' && *p != '\0') { | |
| 1464 if ((q - arg) < arg_size - 1) { | |
| 1465 if (*p == '+') | |
| 1466 *q++ = ' '; | |
| 1467 else | |
| 1468 *q++ = *p; | |
| 1469 } | |
| 1470 p++; | |
| 1471 } | |
| 1472 *q = '\0'; | |
| 1473 } | |
| 1474 if (!strcmp(tag, tag1)) | |
| 1475 return 1; | |
| 1476 if (*p != '&') | |
| 1477 break; | |
| 1478 p++; | |
| 1479 } | |
| 1480 return 0; | |
| 1481 } | |
| 1482 | |
| 1483 /* Return in 'buf' the path with '%d' replaced by number. Also handles | |
| 1484 the '%0nd' format where 'n' is the total number of digits and | |
| 1485 '%%'. Return 0 if OK, and -1 if format error */ | |
| 1486 int get_frame_filename(char *buf, int buf_size, | |
| 1487 const char *path, int number) | |
| 1488 { | |
| 1489 const char *p; | |
| 1490 char *q, buf1[20]; | |
| 1491 int nd, len, c, percentd_found; | |
| 1492 | |
| 1493 q = buf; | |
| 1494 p = path; | |
| 1495 percentd_found = 0; | |
| 1496 for(;;) { | |
| 1497 c = *p++; | |
| 1498 if (c == '\0') | |
| 1499 break; | |
| 1500 if (c == '%') { | |
|
9
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1501 do { |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1502 nd = 0; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1503 while (isdigit(*p)) { |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1504 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
|
1505 } |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1506 c = *p++; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1507 if (c == '*' && nd > 0) { |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1508 // The nd field is actually the modulus |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1509 number = number % nd; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1510 c = *p++; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1511 nd = 0; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1512 } |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1513 } while (isdigit(c)); |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1514 |
| 0 | 1515 switch(c) { |
| 1516 case '%': | |
| 1517 goto addchar; | |
| 1518 case 'd': | |
| 1519 if (percentd_found) | |
| 1520 goto fail; | |
| 1521 percentd_found = 1; | |
| 1522 snprintf(buf1, sizeof(buf1), "%0*d", nd, number); | |
| 1523 len = strlen(buf1); | |
| 1524 if ((q - buf + len) > buf_size - 1) | |
| 1525 goto fail; | |
| 1526 memcpy(q, buf1, len); | |
| 1527 q += len; | |
| 1528 break; | |
| 1529 default: | |
| 1530 goto fail; | |
| 1531 } | |
| 1532 } else { | |
| 1533 addchar: | |
| 1534 if ((q - buf) < buf_size - 1) | |
| 1535 *q++ = c; | |
| 1536 } | |
| 1537 } | |
| 1538 if (!percentd_found) | |
| 1539 goto fail; | |
| 1540 *q = '\0'; | |
| 1541 return 0; | |
| 1542 fail: | |
| 1543 *q = '\0'; | |
| 1544 return -1; | |
| 1545 } | |
| 1546 | |
| 1547 /** | |
| 1548 * | |
| 1549 * Print on stdout a nice hexa dump of a buffer | |
| 1550 * @param buf buffer | |
| 1551 * @param size buffer size | |
| 1552 */ | |
| 65 | 1553 void av_hex_dump(uint8_t *buf, int size) |
| 0 | 1554 { |
| 1555 int len, i, j, c; | |
| 1556 | |
| 1557 for(i=0;i<size;i+=16) { | |
| 1558 len = size - i; | |
| 1559 if (len > 16) | |
| 1560 len = 16; | |
| 1561 printf("%08x ", i); | |
| 1562 for(j=0;j<16;j++) { | |
| 1563 if (j < len) | |
| 1564 printf(" %02x", buf[i+j]); | |
| 1565 else | |
| 1566 printf(" "); | |
| 1567 } | |
| 1568 printf(" "); | |
| 1569 for(j=0;j<len;j++) { | |
| 1570 c = buf[i+j]; | |
| 1571 if (c < ' ' || c > '~') | |
| 1572 c = '.'; | |
| 1573 printf("%c", c); | |
| 1574 } | |
| 1575 printf("\n"); | |
| 1576 } | |
| 1577 } | |
| 1578 | |
| 1579 void url_split(char *proto, int proto_size, | |
| 1580 char *hostname, int hostname_size, | |
| 1581 int *port_ptr, | |
| 1582 char *path, int path_size, | |
| 1583 const char *url) | |
| 1584 { | |
| 1585 const char *p; | |
| 1586 char *q; | |
| 1587 int port; | |
| 1588 | |
| 1589 port = -1; | |
| 1590 | |
| 1591 p = url; | |
| 1592 q = proto; | |
| 1593 while (*p != ':' && *p != '\0') { | |
| 1594 if ((q - proto) < proto_size - 1) | |
| 1595 *q++ = *p; | |
| 1596 p++; | |
| 1597 } | |
| 1598 if (proto_size > 0) | |
| 1599 *q = '\0'; | |
| 1600 if (*p == '\0') { | |
| 1601 if (proto_size > 0) | |
| 1602 proto[0] = '\0'; | |
| 1603 if (hostname_size > 0) | |
| 1604 hostname[0] = '\0'; | |
| 1605 p = url; | |
| 1606 } else { | |
| 1607 p++; | |
| 1608 if (*p == '/') | |
| 1609 p++; | |
| 1610 if (*p == '/') | |
| 1611 p++; | |
| 1612 q = hostname; | |
| 1613 while (*p != ':' && *p != '/' && *p != '?' && *p != '\0') { | |
| 1614 if ((q - hostname) < hostname_size - 1) | |
| 1615 *q++ = *p; | |
| 1616 p++; | |
| 1617 } | |
| 1618 if (hostname_size > 0) | |
| 1619 *q = '\0'; | |
| 1620 if (*p == ':') { | |
| 1621 p++; | |
| 1622 port = strtoul(p, (char **)&p, 10); | |
| 1623 } | |
| 1624 } | |
| 1625 if (port_ptr) | |
| 1626 *port_ptr = port; | |
| 1627 pstrcpy(path, path_size, p); | |
| 1628 } | |
| 1629 | |
| 1630 /** | |
| 1631 * Set the pts for a given stream | |
| 1632 * @param s stream | |
| 1633 * @param pts_wrap_bits number of bits effectively used by the pts | |
| 1634 * (used for wrap control, 33 is the value for MPEG) | |
| 1635 * @param pts_num numerator to convert to seconds (MPEG: 1) | |
| 1636 * @param pts_den denominator to convert to seconds (MPEG: 90000) | |
| 1637 */ | |
| 1638 void av_set_pts_info(AVFormatContext *s, int pts_wrap_bits, | |
| 1639 int pts_num, int pts_den) | |
| 1640 { | |
| 1641 s->pts_wrap_bits = pts_wrap_bits; | |
| 1642 s->pts_num = pts_num; | |
| 1643 s->pts_den = pts_den; | |
| 1644 } | |
| 1645 | |
| 1646 /* fraction handling */ | |
| 1647 | |
| 1648 /** | |
| 1649 * f = val + (num / den) + 0.5. 'num' is normalized so that it is such | |
| 1650 * as 0 <= num < den. | |
| 1651 * | |
| 1652 * @param f fractional number | |
| 1653 * @param val integer value | |
| 1654 * @param num must be >= 0 | |
| 1655 * @param den must be >= 1 | |
| 1656 */ | |
| 65 | 1657 void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den) |
| 0 | 1658 { |
| 1659 num += (den >> 1); | |
| 1660 if (num >= den) { | |
| 1661 val += num / den; | |
| 1662 num = num % den; | |
| 1663 } | |
| 1664 f->val = val; | |
| 1665 f->num = num; | |
| 1666 f->den = den; | |
| 1667 } | |
| 1668 | |
| 1669 /* set f to (val + 0.5) */ | |
| 65 | 1670 void av_frac_set(AVFrac *f, int64_t val) |
| 0 | 1671 { |
| 1672 f->val = val; | |
| 1673 f->num = f->den >> 1; | |
| 1674 } | |
| 1675 | |
| 1676 /** | |
| 1677 * Fractionnal addition to f: f = f + (incr / f->den) | |
| 1678 * | |
| 1679 * @param f fractional number | |
| 1680 * @param incr increment, can be positive or negative | |
| 1681 */ | |
| 65 | 1682 void av_frac_add(AVFrac *f, int64_t incr) |
| 0 | 1683 { |
| 65 | 1684 int64_t num, den; |
| 0 | 1685 |
| 1686 num = f->num + incr; | |
| 1687 den = f->den; | |
| 1688 if (num < 0) { | |
| 1689 f->val += num / den; | |
| 1690 num = num % den; | |
| 1691 if (num < 0) { | |
| 1692 num += den; | |
| 1693 f->val--; | |
| 1694 } | |
| 1695 } else if (num >= den) { | |
| 1696 f->val += num / den; | |
| 1697 num = num % den; | |
| 1698 } | |
| 1699 f->num = num; | |
| 1700 } | |
| 20 | 1701 |
| 1702 /** | |
| 1703 * register a new image format | |
| 1704 * @param img_fmt Image format descriptor | |
| 1705 */ | |
| 1706 void av_register_image_format(AVImageFormat *img_fmt) | |
| 1707 { | |
| 1708 AVImageFormat **p; | |
| 1709 | |
| 1710 p = &first_image_format; | |
| 1711 while (*p != NULL) p = &(*p)->next; | |
| 1712 *p = img_fmt; | |
| 1713 img_fmt->next = NULL; | |
| 1714 } | |
| 1715 | |
| 1716 /* guess image format */ | |
| 1717 AVImageFormat *av_probe_image_format(AVProbeData *pd) | |
| 1718 { | |
| 1719 AVImageFormat *fmt1, *fmt; | |
| 1720 int score, score_max; | |
| 1721 | |
| 1722 fmt = NULL; | |
| 1723 score_max = 0; | |
| 1724 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
| 1725 if (fmt1->img_probe) { | |
| 1726 score = fmt1->img_probe(pd); | |
| 1727 if (score > score_max) { | |
| 1728 score_max = score; | |
| 1729 fmt = fmt1; | |
| 1730 } | |
| 1731 } | |
| 1732 } | |
| 1733 return fmt; | |
| 1734 } | |
| 1735 | |
| 1736 AVImageFormat *guess_image_format(const char *filename) | |
| 1737 { | |
| 1738 AVImageFormat *fmt1; | |
| 1739 | |
| 1740 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
| 1741 if (fmt1->extensions && match_ext(filename, fmt1->extensions)) | |
| 1742 return fmt1; | |
| 1743 } | |
| 1744 return NULL; | |
| 1745 } | |
| 1746 | |
| 1747 /** | |
| 1748 * Read an image from a stream. | |
| 1749 * @param gb byte stream containing the image | |
| 1750 * @param fmt image format, NULL if probing is required | |
| 1751 */ | |
| 1752 int av_read_image(ByteIOContext *pb, const char *filename, | |
| 1753 AVImageFormat *fmt, | |
| 1754 int (*alloc_cb)(void *, AVImageInfo *info), void *opaque) | |
| 1755 { | |
| 1756 char buf[PROBE_BUF_SIZE]; | |
| 1757 AVProbeData probe_data, *pd = &probe_data; | |
| 1758 offset_t pos; | |
| 1759 int ret; | |
| 1760 | |
| 1761 if (!fmt) { | |
| 64 | 1762 pd->filename = filename; |
| 20 | 1763 pd->buf = buf; |
| 1764 pos = url_ftell(pb); | |
| 1765 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE); | |
| 1766 url_fseek(pb, pos, SEEK_SET); | |
| 1767 fmt = av_probe_image_format(pd); | |
| 1768 } | |
| 1769 if (!fmt) | |
| 1770 return AVERROR_NOFMT; | |
| 1771 ret = fmt->img_read(pb, alloc_cb, opaque); | |
| 1772 return ret; | |
| 1773 } | |
| 1774 | |
| 1775 /** | |
| 1776 * Write an image to a stream. | |
| 1777 * @param pb byte stream for the image output | |
| 1778 * @param fmt image format | |
| 1779 * @param img image data and informations | |
| 1780 */ | |
| 1781 int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img) | |
| 1782 { | |
| 1783 return fmt->img_write(pb, img); | |
| 1784 } | |
| 1785 |
