Mercurial > libavformat.hg
annotate utils.c @ 20:3d52de18ecc3 libavformat
added still image support
| author | bellard |
|---|---|
| date | Sat, 11 Jan 2003 05:02:14 +0000 |
| parents | 932b59c66c60 |
| children | 3d2d2f8725f9 |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * Various utilities for ffmpeg system | |
| 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard | |
| 4 * | |
| 5 * This library is free software; you can redistribute it and/or | |
| 6 * modify it under the terms of the GNU Lesser General Public | |
| 7 * License as published by the Free Software Foundation; either | |
| 8 * version 2 of the License, or (at your option) any later version. | |
| 9 * | |
| 10 * This library is distributed in the hope that it will be useful, | |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 * Lesser General Public License for more details. | |
| 14 * | |
| 15 * You should have received a copy of the GNU Lesser General Public | |
| 16 * License along with this library; if not, write to the Free Software | |
| 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 */ | |
| 19 #include "avformat.h" | |
| 20 #include <ctype.h> | |
| 21 #ifndef CONFIG_WIN32 | |
| 22 #include <unistd.h> | |
| 23 #include <fcntl.h> | |
| 24 #include <sys/time.h> | |
| 25 #else | |
| 26 #define strcasecmp _stricmp | |
| 27 #include <sys/types.h> | |
| 28 #include <sys/timeb.h> | |
| 29 #endif | |
| 30 #include <time.h> | |
| 31 | |
| 32 #ifndef HAVE_STRPTIME | |
| 33 #include "strptime.h" | |
| 34 #endif | |
| 35 | |
| 36 AVInputFormat *first_iformat; | |
| 37 AVOutputFormat *first_oformat; | |
| 20 | 38 AVImageFormat *first_image_format; |
| 0 | 39 |
| 40 void av_register_input_format(AVInputFormat *format) | |
| 41 { | |
| 42 AVInputFormat **p; | |
| 43 p = &first_iformat; | |
| 44 while (*p != NULL) p = &(*p)->next; | |
| 45 *p = format; | |
| 46 format->next = NULL; | |
| 47 } | |
| 48 | |
| 49 void av_register_output_format(AVOutputFormat *format) | |
| 50 { | |
| 51 AVOutputFormat **p; | |
| 52 p = &first_oformat; | |
| 53 while (*p != NULL) p = &(*p)->next; | |
| 54 *p = format; | |
| 55 format->next = NULL; | |
| 56 } | |
| 57 | |
| 58 int match_ext(const char *filename, const char *extensions) | |
| 59 { | |
| 60 const char *ext, *p; | |
| 61 char ext1[32], *q; | |
| 62 | |
| 63 ext = strrchr(filename, '.'); | |
| 64 if (ext) { | |
| 65 ext++; | |
| 66 p = extensions; | |
| 67 for(;;) { | |
| 68 q = ext1; | |
| 69 while (*p != '\0' && *p != ',') | |
| 70 *q++ = *p++; | |
| 71 *q = '\0'; | |
| 72 if (!strcasecmp(ext1, ext)) | |
| 73 return 1; | |
| 74 if (*p == '\0') | |
| 75 break; | |
| 76 p++; | |
| 77 } | |
| 78 } | |
| 79 return 0; | |
| 80 } | |
| 81 | |
| 82 AVOutputFormat *guess_format(const char *short_name, const char *filename, | |
| 83 const char *mime_type) | |
| 84 { | |
| 85 AVOutputFormat *fmt, *fmt_found; | |
| 86 int score_max, score; | |
| 87 | |
| 20 | 88 /* specific test for image sequences */ |
| 89 if (!short_name && filename && filename_number_test(filename) >= 0) { | |
| 90 return guess_format("image", NULL, NULL); | |
| 91 } | |
| 92 | |
| 0 | 93 /* find the proper file type */ |
| 94 fmt_found = NULL; | |
| 95 score_max = 0; | |
| 96 fmt = first_oformat; | |
| 97 while (fmt != NULL) { | |
| 98 score = 0; | |
| 99 if (fmt->name && short_name && !strcmp(fmt->name, short_name)) | |
| 100 score += 100; | |
| 101 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type)) | |
| 102 score += 10; | |
| 103 if (filename && fmt->extensions && | |
| 104 match_ext(filename, fmt->extensions)) { | |
| 105 score += 5; | |
| 106 } | |
| 107 if (score > score_max) { | |
| 108 score_max = score; | |
| 109 fmt_found = fmt; | |
| 110 } | |
| 111 fmt = fmt->next; | |
| 112 } | |
| 113 return fmt_found; | |
| 114 } | |
| 115 | |
| 116 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename, | |
| 117 const char *mime_type) | |
| 118 { | |
| 119 AVOutputFormat *fmt = guess_format(short_name, filename, mime_type); | |
| 120 | |
| 121 if (fmt) { | |
| 122 AVOutputFormat *stream_fmt; | |
| 123 char stream_format_name[64]; | |
| 124 | |
| 125 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name); | |
| 126 stream_fmt = guess_format(stream_format_name, NULL, NULL); | |
| 127 | |
| 128 if (stream_fmt) | |
| 129 fmt = stream_fmt; | |
| 130 } | |
| 131 | |
| 132 return fmt; | |
| 133 } | |
| 134 | |
| 135 AVInputFormat *av_find_input_format(const char *short_name) | |
| 136 { | |
| 137 AVInputFormat *fmt; | |
| 138 for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) { | |
| 139 if (!strcmp(fmt->name, short_name)) | |
| 140 return fmt; | |
| 141 } | |
| 142 return NULL; | |
| 143 } | |
| 144 | |
| 145 /* memory handling */ | |
| 146 | |
| 147 /** | |
| 148 * Allocate the payload of a packet and intialized its fields to default values. | |
| 149 * | |
| 150 * @param pkt packet | |
| 151 * @param size wanted payload size | |
| 152 * @return 0 if OK. AVERROR_xxx otherwise. | |
| 153 */ | |
| 154 int av_new_packet(AVPacket *pkt, int size) | |
| 155 { | |
| 156 int i; | |
| 157 pkt->data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); | |
| 158 if (!pkt->data) | |
| 159 return AVERROR_NOMEM; | |
| 160 pkt->size = size; | |
| 161 /* sane state */ | |
| 162 pkt->pts = AV_NOPTS_VALUE; | |
| 163 pkt->stream_index = 0; | |
| 164 pkt->flags = 0; | |
| 165 | |
| 166 for(i=0; i<FF_INPUT_BUFFER_PADDING_SIZE; i++) | |
| 167 pkt->data[size+i]= 0; | |
| 168 | |
| 169 return 0; | |
| 170 } | |
| 171 | |
| 172 /** | |
| 173 * Free a packet | |
| 174 * | |
| 175 * @param pkt packet to free | |
| 176 */ | |
| 177 void av_free_packet(AVPacket *pkt) | |
| 178 { | |
| 179 av_freep(&pkt->data); | |
| 180 /* fail safe */ | |
| 181 pkt->size = 0; | |
| 182 } | |
| 183 | |
| 184 /* fifo handling */ | |
| 185 | |
| 186 int fifo_init(FifoBuffer *f, int size) | |
| 187 { | |
| 188 f->buffer = av_malloc(size); | |
| 189 if (!f->buffer) | |
| 190 return -1; | |
| 191 f->end = f->buffer + size; | |
| 192 f->wptr = f->rptr = f->buffer; | |
| 193 return 0; | |
| 194 } | |
| 195 | |
| 196 void fifo_free(FifoBuffer *f) | |
| 197 { | |
| 198 av_free(f->buffer); | |
| 199 } | |
| 200 | |
| 201 int fifo_size(FifoBuffer *f, UINT8 *rptr) | |
| 202 { | |
| 203 int size; | |
| 204 | |
| 205 if (f->wptr >= rptr) { | |
| 206 size = f->wptr - rptr; | |
| 207 } else { | |
| 208 size = (f->end - rptr) + (f->wptr - f->buffer); | |
| 209 } | |
| 210 return size; | |
| 211 } | |
| 212 | |
| 213 /* get data from the fifo (return -1 if not enough data) */ | |
| 214 int fifo_read(FifoBuffer *f, UINT8 *buf, int buf_size, UINT8 **rptr_ptr) | |
| 215 { | |
| 216 UINT8 *rptr = *rptr_ptr; | |
| 217 int size, len; | |
| 218 | |
| 219 if (f->wptr >= rptr) { | |
| 220 size = f->wptr - rptr; | |
| 221 } else { | |
| 222 size = (f->end - rptr) + (f->wptr - f->buffer); | |
| 223 } | |
| 224 | |
| 225 if (size < buf_size) | |
| 226 return -1; | |
| 227 while (buf_size > 0) { | |
| 228 len = f->end - rptr; | |
| 229 if (len > buf_size) | |
| 230 len = buf_size; | |
| 231 memcpy(buf, rptr, len); | |
| 232 buf += len; | |
| 233 rptr += len; | |
| 234 if (rptr >= f->end) | |
| 235 rptr = f->buffer; | |
| 236 buf_size -= len; | |
| 237 } | |
| 238 *rptr_ptr = rptr; | |
| 239 return 0; | |
| 240 } | |
| 241 | |
| 242 void fifo_write(FifoBuffer *f, UINT8 *buf, int size, UINT8 **wptr_ptr) | |
| 243 { | |
| 244 int len; | |
| 245 UINT8 *wptr; | |
| 246 wptr = *wptr_ptr; | |
| 247 while (size > 0) { | |
| 248 len = f->end - wptr; | |
| 249 if (len > size) | |
| 250 len = size; | |
| 251 memcpy(wptr, buf, len); | |
| 252 wptr += len; | |
| 253 if (wptr >= f->end) | |
| 254 wptr = f->buffer; | |
| 255 buf += len; | |
| 256 size -= len; | |
| 257 } | |
| 258 *wptr_ptr = wptr; | |
| 259 } | |
| 260 | |
| 261 int filename_number_test(const char *filename) | |
| 262 { | |
| 263 char buf[1024]; | |
| 264 return get_frame_filename(buf, sizeof(buf), filename, 1); | |
| 265 } | |
| 266 | |
| 267 /* guess file format */ | |
| 268 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened) | |
| 269 { | |
| 270 AVInputFormat *fmt1, *fmt; | |
| 271 int score, score_max; | |
| 272 | |
| 273 fmt = NULL; | |
| 274 score_max = 0; | |
| 275 for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) { | |
| 276 if (!is_opened && !(fmt1->flags & AVFMT_NOFILE)) | |
| 277 continue; | |
| 278 score = 0; | |
| 279 if (fmt1->read_probe) { | |
| 280 score = fmt1->read_probe(pd); | |
| 281 } else if (fmt1->extensions) { | |
| 282 if (match_ext(pd->filename, fmt1->extensions)) { | |
| 283 score = 50; | |
| 284 } | |
| 285 } | |
| 286 if (score > score_max) { | |
| 287 score_max = score; | |
| 288 fmt = fmt1; | |
| 289 } | |
| 290 } | |
| 291 return fmt; | |
| 292 } | |
| 293 | |
| 294 /************************************************************/ | |
| 295 /* input media file */ | |
| 296 | |
| 297 #define PROBE_BUF_SIZE 2048 | |
| 298 | |
| 299 /** | |
| 300 * Open a media file as input. The codec are not opened. Only the file | |
| 301 * header (if present) is read. | |
| 302 * | |
| 303 * @param ic_ptr the opened media file handle is put here | |
| 304 * @param filename filename to open. | |
| 305 * @param fmt if non NULL, force the file format to use | |
| 306 * @param buf_size optional buffer size (zero if default is OK) | |
| 307 * @param ap additionnal parameters needed when opening the file (NULL if default) | |
| 308 * @return 0 if OK. AVERROR_xxx otherwise. | |
| 309 */ | |
| 310 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename, | |
| 311 AVInputFormat *fmt, | |
| 312 int buf_size, | |
| 313 AVFormatParameters *ap) | |
| 314 { | |
| 315 AVFormatContext *ic = NULL; | |
| 316 int err; | |
| 317 char buf[PROBE_BUF_SIZE]; | |
| 318 AVProbeData probe_data, *pd = &probe_data; | |
| 319 | |
| 320 ic = av_mallocz(sizeof(AVFormatContext)); | |
| 321 if (!ic) { | |
| 322 err = AVERROR_NOMEM; | |
| 323 goto fail; | |
| 324 } | |
| 325 pstrcpy(ic->filename, sizeof(ic->filename), filename); | |
| 326 pd->filename = ic->filename; | |
| 327 pd->buf = buf; | |
| 328 pd->buf_size = 0; | |
| 329 | |
| 330 if (!fmt) { | |
| 331 /* guess format if no file can be opened */ | |
| 332 fmt = av_probe_input_format(pd, 0); | |
| 333 } | |
| 334 | |
| 335 if (!fmt || !(fmt->flags & AVFMT_NOFILE)) { | |
| 20 | 336 /* if no file needed do not try to open one */ |
| 0 | 337 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) { |
| 338 err = AVERROR_IO; | |
| 339 goto fail; | |
| 340 } | |
| 341 if (buf_size > 0) { | |
| 342 url_setbufsize(&ic->pb, buf_size); | |
| 343 } | |
| 344 if (!fmt) { | |
| 345 /* read probe data */ | |
| 346 pd->buf_size = get_buffer(&ic->pb, buf, PROBE_BUF_SIZE); | |
| 347 url_fseek(&ic->pb, 0, SEEK_SET); | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 /* guess file format */ | |
| 352 if (!fmt) { | |
| 353 fmt = av_probe_input_format(pd, 1); | |
| 354 } | |
| 355 | |
| 356 /* if still no format found, error */ | |
| 357 if (!fmt) { | |
| 358 err = AVERROR_NOFMT; | |
| 359 goto fail; | |
| 360 } | |
| 361 | |
| 362 /* XXX: suppress this hack for redirectors */ | |
|
11
932b59c66c60
mingw patch by (Bill Eldridge <bill at rfa dot org>)
michaelni
parents:
9
diff
changeset
|
363 #ifndef __MINGW32__ |
| 0 | 364 if (fmt == &redir_demux) { |
| 365 err = redir_open(ic_ptr, &ic->pb); | |
| 366 url_fclose(&ic->pb); | |
| 367 av_free(ic); | |
| 368 return err; | |
| 369 } | |
|
11
932b59c66c60
mingw patch by (Bill Eldridge <bill at rfa dot org>)
michaelni
parents:
9
diff
changeset
|
370 #endif |
| 0 | 371 |
| 372 ic->iformat = fmt; | |
| 373 | |
| 20 | 374 /* check filename in case of an image number is expected */ |
| 375 if (ic->iformat->flags & AVFMT_NEEDNUMBER) { | |
| 376 if (filename_number_test(ic->filename) < 0) { | |
| 377 err = AVERROR_NUMEXPECTED; | |
| 378 goto fail1; | |
| 379 } | |
| 380 } | |
| 381 | |
| 0 | 382 /* allocate private data */ |
| 383 ic->priv_data = av_mallocz(fmt->priv_data_size); | |
| 384 if (!ic->priv_data) { | |
| 385 err = AVERROR_NOMEM; | |
| 386 goto fail; | |
| 387 } | |
| 388 | |
| 389 /* default pts settings is MPEG like */ | |
| 390 av_set_pts_info(ic, 33, 1, 90000); | |
| 391 | |
| 392 err = ic->iformat->read_header(ic, ap); | |
| 393 if (err < 0) | |
| 394 goto fail1; | |
| 395 *ic_ptr = ic; | |
| 396 return 0; | |
| 397 fail1: | |
| 398 if (!(fmt->flags & AVFMT_NOFILE)) { | |
| 399 url_fclose(&ic->pb); | |
| 400 } | |
| 401 fail: | |
| 402 if (ic) { | |
| 403 av_freep(&ic->priv_data); | |
| 404 } | |
| 405 av_free(ic); | |
| 406 *ic_ptr = NULL; | |
| 407 return err; | |
| 408 } | |
| 409 | |
| 410 /** | |
| 411 * Read a packet from a media file | |
| 412 * @param s media file handle | |
| 413 * @param pkt is filled | |
| 414 * @return 0 if OK. AVERROR_xxx if error. | |
| 415 */ | |
| 416 int av_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 417 { | |
| 418 AVPacketList *pktl; | |
| 419 | |
| 420 pktl = s->packet_buffer; | |
| 421 if (pktl) { | |
| 422 /* read packet from packet buffer, if there is data */ | |
| 423 *pkt = pktl->pkt; | |
| 424 s->packet_buffer = pktl->next; | |
| 425 av_free(pktl); | |
| 426 return 0; | |
| 427 } else { | |
| 428 return s->iformat->read_packet(s, pkt); | |
| 429 } | |
| 430 } | |
| 431 | |
| 432 /* state for codec information */ | |
| 433 #define CSTATE_NOTFOUND 0 | |
| 434 #define CSTATE_DECODING 1 | |
| 435 #define CSTATE_FOUND 2 | |
| 436 | |
| 437 static int has_codec_parameters(AVCodecContext *enc) | |
| 438 { | |
| 439 int val; | |
| 440 switch(enc->codec_type) { | |
| 441 case CODEC_TYPE_AUDIO: | |
| 442 val = enc->sample_rate; | |
| 443 break; | |
| 444 case CODEC_TYPE_VIDEO: | |
| 445 val = enc->width; | |
| 446 break; | |
| 447 default: | |
| 448 val = 1; | |
| 449 break; | |
| 450 } | |
| 451 return (val != 0); | |
| 452 } | |
| 453 | |
| 454 /** | |
| 455 * Read the beginning of a media file to get stream information. This | |
| 456 * is useful for file formats with no headers such as MPEG. This | |
| 457 * function also compute the real frame rate in case of mpeg2 repeat | |
| 458 * frame mode. | |
| 459 * | |
| 460 * @param ic media file handle | |
| 461 * @return >=0 if OK. AVERROR_xxx if error. | |
| 462 */ | |
| 463 int av_find_stream_info(AVFormatContext *ic) | |
| 464 { | |
| 465 int i, count, ret, got_picture, size, read_size; | |
| 466 AVCodec *codec; | |
| 467 AVStream *st; | |
| 468 AVPacket *pkt; | |
| 7 | 469 AVFrame picture; |
| 0 | 470 AVPacketList *pktl=NULL, **ppktl; |
| 471 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2]; | |
| 472 UINT8 *ptr; | |
| 473 int min_read_size, max_read_size; | |
| 474 | |
| 475 /* typical mpeg ts rate is 40 Mbits. DVD rate is about 10 | |
| 476 Mbits. We read at most 0.1 second of file to find all streams */ | |
| 477 | |
| 478 /* XXX: base it on stream bitrate when possible */ | |
| 479 if (ic->iformat == &mpegts_demux) { | |
| 480 /* maximum number of bytes we accept to read to find all the streams | |
| 481 in a file */ | |
| 482 min_read_size = 3000000; | |
| 483 } else { | |
| 484 min_read_size = 125000; | |
| 485 } | |
| 486 /* max read size is 2 seconds of video max */ | |
| 487 max_read_size = min_read_size * 20; | |
| 488 | |
| 489 /* set initial codec state */ | |
| 490 for(i=0;i<ic->nb_streams;i++) { | |
| 491 st = ic->streams[i]; | |
| 492 if (has_codec_parameters(&st->codec)) | |
| 493 st->codec_info_state = CSTATE_FOUND; | |
| 494 else | |
| 495 st->codec_info_state = CSTATE_NOTFOUND; | |
| 496 st->codec_info_nb_repeat_frames = 0; | |
| 497 st->codec_info_nb_real_frames = 0; | |
| 498 } | |
| 499 | |
| 500 count = 0; | |
| 501 read_size = 0; | |
| 502 ppktl = &ic->packet_buffer; | |
| 503 for(;;) { | |
| 504 /* check if one codec still needs to be handled */ | |
| 505 for(i=0;i<ic->nb_streams;i++) { | |
| 506 st = ic->streams[i]; | |
| 507 if (st->codec_info_state != CSTATE_FOUND) | |
| 508 break; | |
| 509 } | |
| 510 if (i == ic->nb_streams) { | |
| 511 /* NOTE: if the format has no header, then we need to read | |
| 512 some packets to get most of the streams, so we cannot | |
| 513 stop here */ | |
| 514 if (!(ic->iformat->flags & AVFMT_NOHEADER) || | |
| 515 read_size >= min_read_size) { | |
| 516 /* if we found the info for all the codecs, we can stop */ | |
| 517 ret = count; | |
| 518 break; | |
| 519 } | |
| 520 } else { | |
| 521 /* we did not get all the codec info, but we read too much data */ | |
| 522 if (read_size >= max_read_size) { | |
| 523 ret = count; | |
| 524 break; | |
| 525 } | |
| 526 } | |
| 527 | |
| 528 pktl = av_mallocz(sizeof(AVPacketList)); | |
| 529 if (!pktl) { | |
| 530 ret = AVERROR_NOMEM; | |
| 531 break; | |
| 532 } | |
| 533 | |
| 534 /* add the packet in the buffered packet list */ | |
| 535 *ppktl = pktl; | |
| 536 ppktl = &pktl->next; | |
| 537 | |
| 538 /* NOTE: a new stream can be added there if no header in file | |
| 539 (AVFMT_NOHEADER) */ | |
| 540 pkt = &pktl->pkt; | |
| 541 if (ic->iformat->read_packet(ic, pkt) < 0) { | |
| 542 /* EOF or error */ | |
| 543 ret = -1; /* we could not have all the codec parameters before EOF */ | |
| 544 if ((ic->iformat->flags & AVFMT_NOHEADER) && | |
| 545 i == ic->nb_streams) | |
| 546 ret = 0; | |
| 547 break; | |
| 548 } | |
| 549 read_size += pkt->size; | |
| 550 | |
| 551 /* open new codecs */ | |
| 552 for(i=0;i<ic->nb_streams;i++) { | |
| 553 st = ic->streams[i]; | |
| 554 if (st->codec_info_state == CSTATE_NOTFOUND) { | |
| 555 /* set to found in case of error */ | |
| 556 st->codec_info_state = CSTATE_FOUND; | |
| 557 codec = avcodec_find_decoder(st->codec.codec_id); | |
| 558 if (codec) { | |
| 559 if(codec->capabilities & CODEC_CAP_TRUNCATED) | |
| 560 st->codec.flags |= CODEC_FLAG_TRUNCATED; | |
| 561 | |
| 562 ret = avcodec_open(&st->codec, codec); | |
| 563 if (ret >= 0) | |
| 564 st->codec_info_state = CSTATE_DECODING; | |
| 565 } | |
| 566 } | |
| 567 } | |
| 568 | |
| 569 st = ic->streams[pkt->stream_index]; | |
| 570 if (st->codec_info_state == CSTATE_DECODING) { | |
| 571 /* decode the data and update codec parameters */ | |
| 572 ptr = pkt->data; | |
| 573 size = pkt->size; | |
| 574 while (size > 0) { | |
| 575 switch(st->codec.codec_type) { | |
| 576 case CODEC_TYPE_VIDEO: | |
| 577 ret = avcodec_decode_video(&st->codec, &picture, | |
| 578 &got_picture, ptr, size); | |
| 579 break; | |
| 580 case CODEC_TYPE_AUDIO: | |
| 581 ret = avcodec_decode_audio(&st->codec, samples, | |
| 582 &got_picture, ptr, size); | |
| 583 break; | |
| 584 default: | |
| 585 ret = -1; | |
| 586 break; | |
| 587 } | |
| 588 if (ret < 0) { | |
| 589 /* if error, simply ignore because another packet | |
| 590 may be OK */ | |
| 591 break; | |
| 592 } | |
| 593 if (got_picture) { | |
| 594 /* we got the parameters - now we can stop | |
| 595 examining this stream */ | |
| 596 /* XXX: add a codec info so that we can decide if | |
| 597 the codec can repeat frames */ | |
| 598 if (st->codec.codec_id == CODEC_ID_MPEG1VIDEO && | |
| 599 ic->iformat != &mpegts_demux && | |
| 600 st->codec.sub_id == 2) { | |
| 601 /* for mpeg2 video, we want to know the real | |
| 602 frame rate, so we decode 40 frames. In mpeg | |
| 603 TS case we do not do it because it would be | |
| 604 too long */ | |
| 605 st->codec_info_nb_real_frames++; | |
| 606 st->codec_info_nb_repeat_frames += st->codec.repeat_pict; | |
| 607 #if 0 | |
| 608 /* XXX: testing */ | |
| 609 if ((st->codec_info_nb_real_frames % 24) == 23) { | |
| 610 st->codec_info_nb_repeat_frames += 2; | |
| 611 } | |
| 612 #endif | |
| 613 /* stop after 40 frames */ | |
| 614 if (st->codec_info_nb_real_frames >= 40) { | |
| 615 st->r_frame_rate = (st->codec.frame_rate * | |
| 616 st->codec_info_nb_real_frames) / | |
| 617 (st->codec_info_nb_real_frames + | |
| 618 (st->codec_info_nb_repeat_frames >> 1)); | |
| 619 goto close_codec; | |
| 620 } | |
| 621 } else { | |
| 622 close_codec: | |
| 623 st->codec_info_state = CSTATE_FOUND; | |
| 624 avcodec_close(&st->codec); | |
| 625 break; | |
| 626 } | |
| 627 } | |
| 628 ptr += ret; | |
| 629 size -= ret; | |
| 630 } | |
| 631 } | |
| 632 count++; | |
| 633 } | |
| 634 | |
| 635 /* close each codec if there are opened */ | |
| 636 for(i=0;i<ic->nb_streams;i++) { | |
| 637 st = ic->streams[i]; | |
| 638 if (st->codec_info_state == CSTATE_DECODING) | |
| 639 avcodec_close(&st->codec); | |
| 640 } | |
| 641 | |
| 642 /* set real frame rate info */ | |
| 643 for(i=0;i<ic->nb_streams;i++) { | |
| 644 st = ic->streams[i]; | |
| 645 if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
| 646 if (!st->r_frame_rate) | |
| 647 st->r_frame_rate = st->codec.frame_rate; | |
| 648 } | |
| 649 } | |
| 650 | |
| 651 return ret; | |
| 652 } | |
| 653 | |
| 654 /** | |
| 655 * Close a media file (but not its codecs) | |
| 656 * | |
| 657 * @param s media file handle | |
| 658 */ | |
| 659 void av_close_input_file(AVFormatContext *s) | |
| 660 { | |
| 661 int i; | |
| 662 | |
| 663 if (s->iformat->read_close) | |
| 664 s->iformat->read_close(s); | |
| 665 for(i=0;i<s->nb_streams;i++) { | |
| 666 av_free(s->streams[i]); | |
| 667 } | |
| 668 if (s->packet_buffer) { | |
| 669 AVPacketList *p, *p1; | |
| 670 p = s->packet_buffer; | |
| 671 while (p != NULL) { | |
| 672 p1 = p->next; | |
| 673 av_free_packet(&p->pkt); | |
| 674 av_free(p); | |
| 675 p = p1; | |
| 676 } | |
| 677 s->packet_buffer = NULL; | |
| 678 } | |
| 679 if (!(s->iformat->flags & AVFMT_NOFILE)) { | |
| 680 url_fclose(&s->pb); | |
| 681 } | |
| 682 av_freep(&s->priv_data); | |
| 683 av_free(s); | |
| 684 } | |
| 685 | |
| 686 /** | |
| 687 * Add a new stream to a media file. Can only be called in the | |
| 688 * read_header function. If the flag AVFMT_NOHEADER is in the format | |
| 689 * description, then new streams can be added in read_packet too. | |
| 690 * | |
| 691 * | |
| 692 * @param s media file handle | |
| 693 * @param id file format dependent stream id | |
| 694 */ | |
| 695 AVStream *av_new_stream(AVFormatContext *s, int id) | |
| 696 { | |
| 697 AVStream *st; | |
| 698 | |
| 699 if (s->nb_streams >= MAX_STREAMS) | |
| 700 return NULL; | |
| 701 | |
| 702 st = av_mallocz(sizeof(AVStream)); | |
| 703 if (!st) | |
| 704 return NULL; | |
| 5 | 705 avcodec_get_context_defaults(&st->codec); |
| 706 | |
| 0 | 707 st->index = s->nb_streams; |
| 708 st->id = id; | |
| 709 s->streams[s->nb_streams++] = st; | |
| 710 return st; | |
| 711 } | |
| 712 | |
| 713 /************************************************************/ | |
| 714 /* output media file */ | |
| 715 | |
| 20 | 716 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
| 717 { | |
| 718 int ret; | |
| 719 | |
| 720 s->priv_data = av_mallocz(s->oformat->priv_data_size); | |
| 721 if (!s->priv_data) | |
| 722 return AVERROR_NOMEM; | |
| 723 if (s->oformat->set_parameters) { | |
| 724 ret = s->oformat->set_parameters(s, ap); | |
| 725 if (ret < 0) | |
| 726 return ret; | |
| 727 } | |
| 728 return 0; | |
| 729 } | |
| 730 | |
| 0 | 731 /** |
| 732 * allocate the stream private data and write the stream header to an | |
| 733 * output media file | |
| 734 * | |
| 735 * @param s media file handle | |
| 736 * @return 0 if OK. AVERROR_xxx if error. | |
| 737 */ | |
| 738 int av_write_header(AVFormatContext *s) | |
| 739 { | |
| 740 int ret, i; | |
| 741 AVStream *st; | |
| 742 | |
| 743 /* default pts settings is MPEG like */ | |
| 744 av_set_pts_info(s, 33, 1, 90000); | |
| 745 ret = s->oformat->write_header(s); | |
| 746 if (ret < 0) | |
| 747 return ret; | |
| 748 | |
| 749 /* init PTS generation */ | |
| 750 for(i=0;i<s->nb_streams;i++) { | |
| 751 st = s->streams[i]; | |
| 752 | |
| 753 switch (st->codec.codec_type) { | |
| 754 case CODEC_TYPE_AUDIO: | |
| 755 av_frac_init(&st->pts, 0, 0, | |
| 756 (INT64)s->pts_num * st->codec.sample_rate); | |
| 757 break; | |
| 758 case CODEC_TYPE_VIDEO: | |
| 759 av_frac_init(&st->pts, 0, 0, | |
| 760 (INT64)s->pts_num * st->codec.frame_rate); | |
| 761 break; | |
| 762 default: | |
| 763 break; | |
| 764 } | |
| 765 } | |
| 766 return 0; | |
| 767 } | |
| 768 | |
| 769 /** | |
| 770 * Write a packet to an output media file. The packet shall contain | |
| 771 * one audio or video frame. | |
| 772 * | |
| 773 * @param s media file handle | |
| 774 * @param stream_index stream index | |
| 775 * @param buf buffer containing the frame data | |
| 776 * @param size size of buffer | |
| 777 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted. | |
| 778 */ | |
| 779 int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf, | |
| 780 int size) | |
| 781 { | |
| 782 AVStream *st; | |
| 783 INT64 pts_mask; | |
| 784 int ret, frame_size; | |
| 785 | |
| 786 st = s->streams[stream_index]; | |
| 787 pts_mask = (1LL << s->pts_wrap_bits) - 1; | |
| 788 ret = s->oformat->write_packet(s, stream_index, (uint8_t *)buf, size, | |
| 789 st->pts.val & pts_mask); | |
| 790 if (ret < 0) | |
| 791 return ret; | |
| 792 | |
| 793 /* update pts */ | |
| 794 switch (st->codec.codec_type) { | |
| 795 case CODEC_TYPE_AUDIO: | |
| 796 if (st->codec.frame_size <= 1) { | |
| 797 frame_size = size / st->codec.channels; | |
| 798 /* specific hack for pcm codecs because no frame size is provided */ | |
| 799 switch(st->codec.codec_id) { | |
| 800 case CODEC_ID_PCM_S16LE: | |
| 801 case CODEC_ID_PCM_S16BE: | |
| 802 case CODEC_ID_PCM_U16LE: | |
| 803 case CODEC_ID_PCM_U16BE: | |
| 804 frame_size >>= 1; | |
| 805 break; | |
| 806 default: | |
| 807 break; | |
| 808 } | |
| 809 } else { | |
| 810 frame_size = st->codec.frame_size; | |
| 811 } | |
| 812 av_frac_add(&st->pts, | |
| 813 (INT64)s->pts_den * frame_size); | |
| 814 break; | |
| 815 case CODEC_TYPE_VIDEO: | |
| 816 av_frac_add(&st->pts, | |
| 817 (INT64)s->pts_den * FRAME_RATE_BASE); | |
| 818 break; | |
| 819 default: | |
| 820 break; | |
| 821 } | |
| 822 return ret; | |
| 823 } | |
| 824 | |
| 825 /** | |
| 826 * write the stream trailer to an output media file and and free the | |
| 827 * file private data. | |
| 828 * | |
| 829 * @param s media file handle | |
| 830 * @return 0 if OK. AVERROR_xxx if error. */ | |
| 831 int av_write_trailer(AVFormatContext *s) | |
| 832 { | |
| 833 int ret; | |
| 834 ret = s->oformat->write_trailer(s); | |
| 835 av_freep(&s->priv_data); | |
| 836 return ret; | |
| 837 } | |
| 838 | |
| 839 /* "user interface" functions */ | |
| 840 | |
| 841 void dump_format(AVFormatContext *ic, | |
| 842 int index, | |
| 843 const char *url, | |
| 844 int is_output) | |
| 845 { | |
| 846 int i, flags; | |
| 847 char buf[256]; | |
| 848 | |
| 849 fprintf(stderr, "%s #%d, %s, %s '%s':\n", | |
| 850 is_output ? "Output" : "Input", | |
| 851 index, | |
| 852 is_output ? ic->oformat->name : ic->iformat->name, | |
| 853 is_output ? "to" : "from", url); | |
| 854 for(i=0;i<ic->nb_streams;i++) { | |
| 855 AVStream *st = ic->streams[i]; | |
| 856 avcodec_string(buf, sizeof(buf), &st->codec, is_output); | |
| 857 fprintf(stderr, " Stream #%d.%d", index, i); | |
| 858 /* the pid is an important information, so we display it */ | |
| 859 /* XXX: add a generic system */ | |
| 860 if (is_output) | |
| 861 flags = ic->oformat->flags; | |
| 862 else | |
| 863 flags = ic->iformat->flags; | |
| 864 if (flags & AVFMT_SHOW_IDS) { | |
| 865 fprintf(stderr, "[0x%x]", st->id); | |
| 866 } | |
| 867 fprintf(stderr, ": %s\n", buf); | |
| 868 } | |
| 869 } | |
| 870 | |
| 871 typedef struct { | |
| 872 const char *str; | |
| 873 int width, height; | |
| 874 } SizeEntry; | |
| 875 | |
| 876 static SizeEntry sizes[] = { | |
| 877 { "sqcif", 128, 96 }, | |
| 878 { "qcif", 176, 144 }, | |
| 879 { "cif", 352, 288 }, | |
| 880 { "4cif", 704, 576 }, | |
| 881 }; | |
| 882 | |
| 883 int parse_image_size(int *width_ptr, int *height_ptr, const char *str) | |
| 884 { | |
| 885 int i; | |
| 886 int n = sizeof(sizes) / sizeof(SizeEntry); | |
| 887 const char *p; | |
| 888 int frame_width = 0, frame_height = 0; | |
| 889 | |
| 890 for(i=0;i<n;i++) { | |
| 891 if (!strcmp(sizes[i].str, str)) { | |
| 892 frame_width = sizes[i].width; | |
| 893 frame_height = sizes[i].height; | |
| 894 break; | |
| 895 } | |
| 896 } | |
| 897 if (i == n) { | |
| 898 p = str; | |
| 899 frame_width = strtol(p, (char **)&p, 10); | |
| 900 if (*p) | |
| 901 p++; | |
| 902 frame_height = strtol(p, (char **)&p, 10); | |
| 903 } | |
| 904 if (frame_width <= 0 || frame_height <= 0) | |
| 905 return -1; | |
| 906 *width_ptr = frame_width; | |
| 907 *height_ptr = frame_height; | |
| 908 return 0; | |
| 909 } | |
| 910 | |
| 911 INT64 av_gettime(void) | |
| 912 { | |
| 913 #ifdef CONFIG_WIN32 | |
| 914 struct _timeb tb; | |
| 915 _ftime(&tb); | |
| 916 return ((INT64)tb.time * INT64_C(1000) + (INT64)tb.millitm) * INT64_C(1000); | |
| 917 #else | |
| 918 struct timeval tv; | |
| 919 gettimeofday(&tv,NULL); | |
| 920 return (INT64)tv.tv_sec * 1000000 + tv.tv_usec; | |
| 921 #endif | |
| 922 } | |
| 923 | |
| 924 static time_t mktimegm(struct tm *tm) | |
| 925 { | |
| 926 time_t t; | |
| 927 | |
| 928 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday; | |
| 929 | |
| 930 if (m < 3) { | |
| 931 m += 12; | |
| 932 y--; | |
| 933 } | |
| 934 | |
| 935 t = 86400 * | |
| 936 (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469); | |
| 937 | |
| 938 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec; | |
| 939 | |
| 940 return t; | |
| 941 } | |
| 942 | |
| 943 /* Syntax: | |
| 944 * - If not a duration: | |
| 945 * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]} | |
| 946 * Time is localtime unless Z is suffixed to the end. In this case GMT | |
| 947 * Return the date in micro seconds since 1970 | |
| 948 * - If duration: | |
| 949 * HH[:MM[:SS[.m...]]] | |
| 950 * S+[.m...] | |
| 951 */ | |
| 952 INT64 parse_date(const char *datestr, int duration) | |
| 953 { | |
| 954 const char *p; | |
| 955 INT64 t; | |
| 956 struct tm dt; | |
| 957 int i; | |
| 958 static const char *date_fmt[] = { | |
| 959 "%Y-%m-%d", | |
| 960 "%Y%m%d", | |
| 961 }; | |
| 962 static const char *time_fmt[] = { | |
| 963 "%H:%M:%S", | |
| 964 "%H%M%S", | |
| 965 }; | |
| 966 const char *q; | |
| 967 int is_utc, len; | |
| 968 char lastch; | |
| 969 time_t now = time(0); | |
| 970 | |
| 971 len = strlen(datestr); | |
| 972 if (len > 0) | |
| 973 lastch = datestr[len - 1]; | |
| 974 else | |
| 975 lastch = '\0'; | |
| 976 is_utc = (lastch == 'z' || lastch == 'Z'); | |
| 977 | |
| 978 memset(&dt, 0, sizeof(dt)); | |
| 979 | |
| 980 p = datestr; | |
| 981 q = NULL; | |
| 982 if (!duration) { | |
| 983 for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) { | |
| 984 q = strptime(p, date_fmt[i], &dt); | |
| 985 if (q) { | |
| 986 break; | |
| 987 } | |
| 988 } | |
| 989 | |
| 990 if (!q) { | |
| 991 if (is_utc) { | |
| 992 dt = *gmtime(&now); | |
| 993 } else { | |
| 994 dt = *localtime(&now); | |
| 995 } | |
| 996 dt.tm_hour = dt.tm_min = dt.tm_sec = 0; | |
| 997 } else { | |
| 998 p = q; | |
| 999 } | |
| 1000 | |
| 1001 if (*p == 'T' || *p == 't' || *p == ' ') | |
| 1002 p++; | |
| 1003 | |
| 1004 for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) { | |
| 1005 q = strptime(p, time_fmt[i], &dt); | |
| 1006 if (q) { | |
| 1007 break; | |
| 1008 } | |
| 1009 } | |
| 1010 } else { | |
| 1011 q = strptime(p, time_fmt[0], &dt); | |
| 1012 if (!q) { | |
| 1013 dt.tm_sec = strtol(p, (char **)&q, 10); | |
| 1014 dt.tm_min = 0; | |
| 1015 dt.tm_hour = 0; | |
| 1016 } | |
| 1017 } | |
| 1018 | |
| 1019 /* Now we have all the fields that we can get */ | |
| 1020 if (!q) { | |
| 1021 if (duration) | |
| 1022 return 0; | |
| 1023 else | |
| 1024 return now * INT64_C(1000000); | |
| 1025 } | |
| 1026 | |
| 1027 if (duration) { | |
| 1028 t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec; | |
| 1029 } else { | |
| 1030 dt.tm_isdst = -1; /* unknown */ | |
| 1031 if (is_utc) { | |
| 1032 t = mktimegm(&dt); | |
| 1033 } else { | |
| 1034 t = mktime(&dt); | |
| 1035 } | |
| 1036 } | |
| 1037 | |
| 1038 t *= 1000000; | |
| 1039 | |
| 1040 if (*q == '.') { | |
| 1041 int val, n; | |
| 1042 q++; | |
| 1043 for (val = 0, n = 100000; n >= 1; n /= 10, q++) { | |
| 1044 if (!isdigit(*q)) | |
| 1045 break; | |
| 1046 val += n * (*q - '0'); | |
| 1047 } | |
| 1048 t += val; | |
| 1049 } | |
| 1050 return t; | |
| 1051 } | |
| 1052 | |
| 1053 /* syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done. Return | |
| 1054 1 if found */ | |
| 1055 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info) | |
| 1056 { | |
| 1057 const char *p; | |
| 1058 char tag[128], *q; | |
| 1059 | |
| 1060 p = info; | |
| 1061 if (*p == '?') | |
| 1062 p++; | |
| 1063 for(;;) { | |
| 1064 q = tag; | |
| 1065 while (*p != '\0' && *p != '=' && *p != '&') { | |
| 1066 if ((q - tag) < sizeof(tag) - 1) | |
| 1067 *q++ = *p; | |
| 1068 p++; | |
| 1069 } | |
| 1070 *q = '\0'; | |
| 1071 q = arg; | |
| 1072 if (*p == '=') { | |
| 1073 p++; | |
| 1074 while (*p != '&' && *p != '\0') { | |
| 1075 if ((q - arg) < arg_size - 1) { | |
| 1076 if (*p == '+') | |
| 1077 *q++ = ' '; | |
| 1078 else | |
| 1079 *q++ = *p; | |
| 1080 } | |
| 1081 p++; | |
| 1082 } | |
| 1083 *q = '\0'; | |
| 1084 } | |
| 1085 if (!strcmp(tag, tag1)) | |
| 1086 return 1; | |
| 1087 if (*p != '&') | |
| 1088 break; | |
| 1089 p++; | |
| 1090 } | |
| 1091 return 0; | |
| 1092 } | |
| 1093 | |
| 1094 /* Return in 'buf' the path with '%d' replaced by number. Also handles | |
| 1095 the '%0nd' format where 'n' is the total number of digits and | |
| 1096 '%%'. Return 0 if OK, and -1 if format error */ | |
| 1097 int get_frame_filename(char *buf, int buf_size, | |
| 1098 const char *path, int number) | |
| 1099 { | |
| 1100 const char *p; | |
| 1101 char *q, buf1[20]; | |
| 1102 int nd, len, c, percentd_found; | |
| 1103 | |
| 1104 q = buf; | |
| 1105 p = path; | |
| 1106 percentd_found = 0; | |
| 1107 for(;;) { | |
| 1108 c = *p++; | |
| 1109 if (c == '\0') | |
| 1110 break; | |
| 1111 if (c == '%') { | |
|
9
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1112 do { |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1113 nd = 0; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1114 while (isdigit(*p)) { |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1115 nd = nd * 10 + *p++ - '0'; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1116 } |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1117 c = *p++; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1118 if (c == '*' && nd > 0) { |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1119 // The nd field is actually the modulus |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1120 number = number % nd; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1121 c = *p++; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1122 nd = 0; |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1123 } |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1124 } while (isdigit(c)); |
|
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1125 |
| 0 | 1126 switch(c) { |
| 1127 case '%': | |
| 1128 goto addchar; | |
| 1129 case 'd': | |
| 1130 if (percentd_found) | |
| 1131 goto fail; | |
| 1132 percentd_found = 1; | |
| 1133 snprintf(buf1, sizeof(buf1), "%0*d", nd, number); | |
| 1134 len = strlen(buf1); | |
| 1135 if ((q - buf + len) > buf_size - 1) | |
| 1136 goto fail; | |
| 1137 memcpy(q, buf1, len); | |
| 1138 q += len; | |
| 1139 break; | |
| 1140 default: | |
| 1141 goto fail; | |
| 1142 } | |
| 1143 } else { | |
| 1144 addchar: | |
| 1145 if ((q - buf) < buf_size - 1) | |
| 1146 *q++ = c; | |
| 1147 } | |
| 1148 } | |
| 1149 if (!percentd_found) | |
| 1150 goto fail; | |
| 1151 *q = '\0'; | |
| 1152 return 0; | |
| 1153 fail: | |
| 1154 *q = '\0'; | |
| 1155 return -1; | |
| 1156 } | |
| 1157 | |
| 1158 /** | |
| 1159 * | |
| 1160 * Print on stdout a nice hexa dump of a buffer | |
| 1161 * @param buf buffer | |
| 1162 * @param size buffer size | |
| 1163 */ | |
| 1164 void av_hex_dump(UINT8 *buf, int size) | |
| 1165 { | |
| 1166 int len, i, j, c; | |
| 1167 | |
| 1168 for(i=0;i<size;i+=16) { | |
| 1169 len = size - i; | |
| 1170 if (len > 16) | |
| 1171 len = 16; | |
| 1172 printf("%08x ", i); | |
| 1173 for(j=0;j<16;j++) { | |
| 1174 if (j < len) | |
| 1175 printf(" %02x", buf[i+j]); | |
| 1176 else | |
| 1177 printf(" "); | |
| 1178 } | |
| 1179 printf(" "); | |
| 1180 for(j=0;j<len;j++) { | |
| 1181 c = buf[i+j]; | |
| 1182 if (c < ' ' || c > '~') | |
| 1183 c = '.'; | |
| 1184 printf("%c", c); | |
| 1185 } | |
| 1186 printf("\n"); | |
| 1187 } | |
| 1188 } | |
| 1189 | |
| 1190 void url_split(char *proto, int proto_size, | |
| 1191 char *hostname, int hostname_size, | |
| 1192 int *port_ptr, | |
| 1193 char *path, int path_size, | |
| 1194 const char *url) | |
| 1195 { | |
| 1196 const char *p; | |
| 1197 char *q; | |
| 1198 int port; | |
| 1199 | |
| 1200 port = -1; | |
| 1201 | |
| 1202 p = url; | |
| 1203 q = proto; | |
| 1204 while (*p != ':' && *p != '\0') { | |
| 1205 if ((q - proto) < proto_size - 1) | |
| 1206 *q++ = *p; | |
| 1207 p++; | |
| 1208 } | |
| 1209 if (proto_size > 0) | |
| 1210 *q = '\0'; | |
| 1211 if (*p == '\0') { | |
| 1212 if (proto_size > 0) | |
| 1213 proto[0] = '\0'; | |
| 1214 if (hostname_size > 0) | |
| 1215 hostname[0] = '\0'; | |
| 1216 p = url; | |
| 1217 } else { | |
| 1218 p++; | |
| 1219 if (*p == '/') | |
| 1220 p++; | |
| 1221 if (*p == '/') | |
| 1222 p++; | |
| 1223 q = hostname; | |
| 1224 while (*p != ':' && *p != '/' && *p != '?' && *p != '\0') { | |
| 1225 if ((q - hostname) < hostname_size - 1) | |
| 1226 *q++ = *p; | |
| 1227 p++; | |
| 1228 } | |
| 1229 if (hostname_size > 0) | |
| 1230 *q = '\0'; | |
| 1231 if (*p == ':') { | |
| 1232 p++; | |
| 1233 port = strtoul(p, (char **)&p, 10); | |
| 1234 } | |
| 1235 } | |
| 1236 if (port_ptr) | |
| 1237 *port_ptr = port; | |
| 1238 pstrcpy(path, path_size, p); | |
| 1239 } | |
| 1240 | |
| 1241 /** | |
| 1242 * Set the pts for a given stream | |
| 1243 * @param s stream | |
| 1244 * @param pts_wrap_bits number of bits effectively used by the pts | |
| 1245 * (used for wrap control, 33 is the value for MPEG) | |
| 1246 * @param pts_num numerator to convert to seconds (MPEG: 1) | |
| 1247 * @param pts_den denominator to convert to seconds (MPEG: 90000) | |
| 1248 */ | |
| 1249 void av_set_pts_info(AVFormatContext *s, int pts_wrap_bits, | |
| 1250 int pts_num, int pts_den) | |
| 1251 { | |
| 1252 s->pts_wrap_bits = pts_wrap_bits; | |
| 1253 s->pts_num = pts_num; | |
| 1254 s->pts_den = pts_den; | |
| 1255 } | |
| 1256 | |
| 1257 /* fraction handling */ | |
| 1258 | |
| 1259 /** | |
| 1260 * f = val + (num / den) + 0.5. 'num' is normalized so that it is such | |
| 1261 * as 0 <= num < den. | |
| 1262 * | |
| 1263 * @param f fractional number | |
| 1264 * @param val integer value | |
| 1265 * @param num must be >= 0 | |
| 1266 * @param den must be >= 1 | |
| 1267 */ | |
| 1268 void av_frac_init(AVFrac *f, INT64 val, INT64 num, INT64 den) | |
| 1269 { | |
| 1270 num += (den >> 1); | |
| 1271 if (num >= den) { | |
| 1272 val += num / den; | |
| 1273 num = num % den; | |
| 1274 } | |
| 1275 f->val = val; | |
| 1276 f->num = num; | |
| 1277 f->den = den; | |
| 1278 } | |
| 1279 | |
| 1280 /* set f to (val + 0.5) */ | |
| 1281 void av_frac_set(AVFrac *f, INT64 val) | |
| 1282 { | |
| 1283 f->val = val; | |
| 1284 f->num = f->den >> 1; | |
| 1285 } | |
| 1286 | |
| 1287 /** | |
| 1288 * Fractionnal addition to f: f = f + (incr / f->den) | |
| 1289 * | |
| 1290 * @param f fractional number | |
| 1291 * @param incr increment, can be positive or negative | |
| 1292 */ | |
| 1293 void av_frac_add(AVFrac *f, INT64 incr) | |
| 1294 { | |
| 1295 INT64 num, den; | |
| 1296 | |
| 1297 num = f->num + incr; | |
| 1298 den = f->den; | |
| 1299 if (num < 0) { | |
| 1300 f->val += num / den; | |
| 1301 num = num % den; | |
| 1302 if (num < 0) { | |
| 1303 num += den; | |
| 1304 f->val--; | |
| 1305 } | |
| 1306 } else if (num >= den) { | |
| 1307 f->val += num / den; | |
| 1308 num = num % den; | |
| 1309 } | |
| 1310 f->num = num; | |
| 1311 } | |
| 20 | 1312 |
| 1313 /** | |
| 1314 * register a new image format | |
| 1315 * @param img_fmt Image format descriptor | |
| 1316 */ | |
| 1317 void av_register_image_format(AVImageFormat *img_fmt) | |
| 1318 { | |
| 1319 AVImageFormat **p; | |
| 1320 | |
| 1321 p = &first_image_format; | |
| 1322 while (*p != NULL) p = &(*p)->next; | |
| 1323 *p = img_fmt; | |
| 1324 img_fmt->next = NULL; | |
| 1325 } | |
| 1326 | |
| 1327 /* guess image format */ | |
| 1328 AVImageFormat *av_probe_image_format(AVProbeData *pd) | |
| 1329 { | |
| 1330 AVImageFormat *fmt1, *fmt; | |
| 1331 int score, score_max; | |
| 1332 | |
| 1333 fmt = NULL; | |
| 1334 score_max = 0; | |
| 1335 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
| 1336 if (fmt1->img_probe) { | |
| 1337 score = fmt1->img_probe(pd); | |
| 1338 if (score > score_max) { | |
| 1339 score_max = score; | |
| 1340 fmt = fmt1; | |
| 1341 } | |
| 1342 } | |
| 1343 } | |
| 1344 return fmt; | |
| 1345 } | |
| 1346 | |
| 1347 AVImageFormat *guess_image_format(const char *filename) | |
| 1348 { | |
| 1349 AVImageFormat *fmt1; | |
| 1350 | |
| 1351 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
| 1352 if (fmt1->extensions && match_ext(filename, fmt1->extensions)) | |
| 1353 return fmt1; | |
| 1354 } | |
| 1355 return NULL; | |
| 1356 } | |
| 1357 | |
| 1358 /** | |
| 1359 * Read an image from a stream. | |
| 1360 * @param gb byte stream containing the image | |
| 1361 * @param fmt image format, NULL if probing is required | |
| 1362 */ | |
| 1363 int av_read_image(ByteIOContext *pb, const char *filename, | |
| 1364 AVImageFormat *fmt, | |
| 1365 int (*alloc_cb)(void *, AVImageInfo *info), void *opaque) | |
| 1366 { | |
| 1367 char buf[PROBE_BUF_SIZE]; | |
| 1368 AVProbeData probe_data, *pd = &probe_data; | |
| 1369 offset_t pos; | |
| 1370 int ret; | |
| 1371 | |
| 1372 if (!fmt) { | |
| 1373 pd->filename = (char *)filename; | |
| 1374 pd->buf = buf; | |
| 1375 pos = url_ftell(pb); | |
| 1376 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE); | |
| 1377 url_fseek(pb, pos, SEEK_SET); | |
| 1378 fmt = av_probe_image_format(pd); | |
| 1379 } | |
| 1380 if (!fmt) | |
| 1381 return AVERROR_NOFMT; | |
| 1382 ret = fmt->img_read(pb, alloc_cb, opaque); | |
| 1383 return ret; | |
| 1384 } | |
| 1385 | |
| 1386 /** | |
| 1387 * Write an image to a stream. | |
| 1388 * @param pb byte stream for the image output | |
| 1389 * @param fmt image format | |
| 1390 * @param img image data and informations | |
| 1391 */ | |
| 1392 int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img) | |
| 1393 { | |
| 1394 return fmt->img_write(pb, img); | |
| 1395 } | |
| 1396 |
