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