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