You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1057 lines
27KB

  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 "tick.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. AVInputFormat *first_iformat;
  32. AVOutputFormat *first_oformat;
  33. void av_register_input_format(AVInputFormat *format)
  34. {
  35. AVInputFormat **p;
  36. p = &first_iformat;
  37. while (*p != NULL) p = &(*p)->next;
  38. *p = format;
  39. format->next = NULL;
  40. }
  41. void av_register_output_format(AVOutputFormat *format)
  42. {
  43. AVOutputFormat **p;
  44. p = &first_oformat;
  45. while (*p != NULL) p = &(*p)->next;
  46. *p = format;
  47. format->next = NULL;
  48. }
  49. int match_ext(const char *filename, const char *extensions)
  50. {
  51. const char *ext, *p;
  52. char ext1[32], *q;
  53. ext = strrchr(filename, '.');
  54. if (ext) {
  55. ext++;
  56. p = extensions;
  57. for(;;) {
  58. q = ext1;
  59. while (*p != '\0' && *p != ',')
  60. *q++ = *p++;
  61. *q = '\0';
  62. if (!strcasecmp(ext1, ext))
  63. return 1;
  64. if (*p == '\0')
  65. break;
  66. p++;
  67. }
  68. }
  69. return 0;
  70. }
  71. AVOutputFormat *guess_format(const char *short_name, const char *filename,
  72. const char *mime_type)
  73. {
  74. AVOutputFormat *fmt, *fmt_found;
  75. int score_max, score;
  76. /* find the proper file type */
  77. fmt_found = NULL;
  78. score_max = 0;
  79. fmt = first_oformat;
  80. while (fmt != NULL) {
  81. score = 0;
  82. if (fmt->name && short_name && !strcmp(fmt->name, short_name))
  83. score += 100;
  84. if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
  85. score += 10;
  86. if (filename && fmt->extensions &&
  87. match_ext(filename, fmt->extensions)) {
  88. score += 5;
  89. }
  90. if (score > score_max) {
  91. score_max = score;
  92. fmt_found = fmt;
  93. }
  94. fmt = fmt->next;
  95. }
  96. return fmt_found;
  97. }
  98. AVInputFormat *av_find_input_format(const char *short_name)
  99. {
  100. AVInputFormat *fmt;
  101. for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) {
  102. if (!strcmp(fmt->name, short_name))
  103. return fmt;
  104. }
  105. return NULL;
  106. }
  107. /* memory handling */
  108. /**
  109. * Allocate the payload of a packet and intialized its fields to default values.
  110. *
  111. * @param pkt packet
  112. * @param size wanted payload size
  113. * @return 0 if OK. AVERROR_xxx otherwise.
  114. */
  115. int av_new_packet(AVPacket *pkt, int size)
  116. {
  117. pkt->data = av_malloc(size);
  118. if (!pkt->data)
  119. return AVERROR_NOMEM;
  120. pkt->size = size;
  121. /* sane state */
  122. pkt->pts = 0;
  123. pkt->stream_index = 0;
  124. pkt->flags = 0;
  125. return 0;
  126. }
  127. /**
  128. * Free a packet
  129. *
  130. * @param pkt packet to free
  131. */
  132. void av_free_packet(AVPacket *pkt)
  133. {
  134. av_freep(&pkt->data);
  135. /* fail safe */
  136. pkt->size = 0;
  137. }
  138. /* fifo handling */
  139. int fifo_init(FifoBuffer *f, int size)
  140. {
  141. f->buffer = av_malloc(size);
  142. if (!f->buffer)
  143. return -1;
  144. f->end = f->buffer + size;
  145. f->wptr = f->rptr = f->buffer;
  146. return 0;
  147. }
  148. void fifo_free(FifoBuffer *f)
  149. {
  150. av_free(f->buffer);
  151. }
  152. int fifo_size(FifoBuffer *f, UINT8 *rptr)
  153. {
  154. int size;
  155. if (f->wptr >= rptr) {
  156. size = f->wptr - rptr;
  157. } else {
  158. size = (f->end - rptr) + (f->wptr - f->buffer);
  159. }
  160. return size;
  161. }
  162. /* get data from the fifo (return -1 if not enough data) */
  163. int fifo_read(FifoBuffer *f, UINT8 *buf, int buf_size, UINT8 **rptr_ptr)
  164. {
  165. UINT8 *rptr = *rptr_ptr;
  166. int size, len;
  167. if (f->wptr >= rptr) {
  168. size = f->wptr - rptr;
  169. } else {
  170. size = (f->end - rptr) + (f->wptr - f->buffer);
  171. }
  172. if (size < buf_size)
  173. return -1;
  174. while (buf_size > 0) {
  175. len = f->end - rptr;
  176. if (len > buf_size)
  177. len = buf_size;
  178. memcpy(buf, rptr, len);
  179. buf += len;
  180. rptr += len;
  181. if (rptr >= f->end)
  182. rptr = f->buffer;
  183. buf_size -= len;
  184. }
  185. *rptr_ptr = rptr;
  186. return 0;
  187. }
  188. void fifo_write(FifoBuffer *f, UINT8 *buf, int size, UINT8 **wptr_ptr)
  189. {
  190. int len;
  191. UINT8 *wptr;
  192. wptr = *wptr_ptr;
  193. while (size > 0) {
  194. len = f->end - wptr;
  195. if (len > size)
  196. len = size;
  197. memcpy(wptr, buf, len);
  198. wptr += len;
  199. if (wptr >= f->end)
  200. wptr = f->buffer;
  201. buf += len;
  202. size -= len;
  203. }
  204. *wptr_ptr = wptr;
  205. }
  206. int filename_number_test(const char *filename)
  207. {
  208. char buf[1024];
  209. return get_frame_filename(buf, sizeof(buf), filename, 1);
  210. }
  211. /* guess file format */
  212. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
  213. {
  214. AVInputFormat *fmt1, *fmt;
  215. int score, score_max;
  216. fmt = NULL;
  217. score_max = 0;
  218. for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) {
  219. if (!is_opened && !(fmt1->flags & AVFMT_NOFILE))
  220. continue;
  221. score = 0;
  222. if (fmt1->read_probe) {
  223. score = fmt1->read_probe(pd);
  224. } else if (fmt1->extensions) {
  225. if (match_ext(pd->filename, fmt1->extensions)) {
  226. score = 50;
  227. }
  228. }
  229. if (score > score_max) {
  230. score_max = score;
  231. fmt = fmt1;
  232. }
  233. }
  234. return fmt;
  235. }
  236. /************************************************************/
  237. /* input media file */
  238. #define PROBE_BUF_SIZE 2048
  239. /**
  240. * Open a media file as input. The codec are not opened. Only the file
  241. * header (if present) is read.
  242. *
  243. * @param ic_ptr the opened media file handle is put here
  244. * @param filename filename to open.
  245. * @param fmt if non NULL, force the file format to use
  246. * @param buf_size optional buffer size (zero if default is OK)
  247. * @param ap additionnal parameters needed when opening the file (NULL if default)
  248. * @return 0 if OK. AVERROR_xxx otherwise.
  249. */
  250. int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
  251. AVInputFormat *fmt,
  252. int buf_size,
  253. AVFormatParameters *ap)
  254. {
  255. AVFormatContext *ic = NULL;
  256. int err;
  257. char buf[PROBE_BUF_SIZE];
  258. AVProbeData probe_data, *pd = &probe_data;
  259. ic = av_mallocz(sizeof(AVFormatContext));
  260. if (!ic) {
  261. err = AVERROR_NOMEM;
  262. goto fail;
  263. }
  264. pstrcpy(ic->filename, sizeof(ic->filename), filename);
  265. pd->filename = ic->filename;
  266. pd->buf = buf;
  267. pd->buf_size = 0;
  268. if (!fmt) {
  269. /* guess format if no file can be opened */
  270. fmt = av_probe_input_format(pd, 0);
  271. }
  272. /* if no file needed do not try to open one */
  273. if (!fmt || !(fmt->flags & AVFMT_NOFILE)) {
  274. if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
  275. err = AVERROR_IO;
  276. goto fail;
  277. }
  278. if (buf_size > 0) {
  279. url_setbufsize(&ic->pb, buf_size);
  280. }
  281. /* read probe data */
  282. pd->buf_size = get_buffer(&ic->pb, buf, PROBE_BUF_SIZE);
  283. url_fseek(&ic->pb, 0, SEEK_SET);
  284. }
  285. /* guess file format */
  286. if (!fmt) {
  287. fmt = av_probe_input_format(pd, 1);
  288. }
  289. /* if still no format found, error */
  290. if (!fmt) {
  291. err = AVERROR_NOFMT;
  292. goto fail;
  293. }
  294. ic->iformat = fmt;
  295. /* allocate private data */
  296. ic->priv_data = av_mallocz(fmt->priv_data_size);
  297. if (!ic->priv_data) {
  298. err = AVERROR_NOMEM;
  299. goto fail;
  300. }
  301. /* check filename in case of an image number is expected */
  302. if (ic->iformat->flags & AVFMT_NEEDNUMBER) {
  303. if (filename_number_test(ic->filename) < 0) {
  304. err = AVERROR_NUMEXPECTED;
  305. goto fail1;
  306. }
  307. }
  308. err = ic->iformat->read_header(ic, ap);
  309. if (err < 0)
  310. goto fail1;
  311. *ic_ptr = ic;
  312. return 0;
  313. fail1:
  314. if (!(fmt->flags & AVFMT_NOFILE)) {
  315. url_fclose(&ic->pb);
  316. }
  317. fail:
  318. if (ic) {
  319. av_freep(&ic->priv_data);
  320. }
  321. av_free(ic);
  322. *ic_ptr = NULL;
  323. return err;
  324. }
  325. /**
  326. * Read a packet from a media file
  327. * @param s media file handle
  328. * @param pkt is filled
  329. * @return 0 if OK. AVERROR_xxx if error.
  330. */
  331. int av_read_packet(AVFormatContext *s, AVPacket *pkt)
  332. {
  333. AVPacketList *pktl;
  334. pktl = s->packet_buffer;
  335. if (pktl) {
  336. /* read packet from packet buffer, if there is data */
  337. *pkt = pktl->pkt;
  338. s->packet_buffer = pktl->next;
  339. av_free(pktl);
  340. return 0;
  341. } else {
  342. return s->iformat->read_packet(s, pkt);
  343. }
  344. }
  345. /* state for codec information */
  346. #define CSTATE_NOTFOUND 0
  347. #define CSTATE_DECODING 1
  348. #define CSTATE_FOUND 2
  349. static int has_codec_parameters(AVCodecContext *enc)
  350. {
  351. int val;
  352. switch(enc->codec_type) {
  353. case CODEC_TYPE_AUDIO:
  354. val = enc->sample_rate;
  355. break;
  356. case CODEC_TYPE_VIDEO:
  357. val = enc->width;
  358. break;
  359. default:
  360. val = 1;
  361. break;
  362. }
  363. return (val != 0);
  364. }
  365. /**
  366. * Read the beginning of a media file to get stream information. This
  367. * is useful for file formats with no headers such as MPEG. This
  368. * function also compute the real frame rate in case of mpeg2 repeat
  369. * frame mode.
  370. *
  371. * @param ic media file handle
  372. * @return >=0 if OK. AVERROR_xxx if error.
  373. */
  374. int av_find_stream_info(AVFormatContext *ic)
  375. {
  376. int i, count, ret, got_picture, size, read_size;
  377. AVCodec *codec;
  378. AVStream *st;
  379. AVPacket *pkt;
  380. AVPicture picture;
  381. AVPacketList *pktl=NULL, **ppktl;
  382. short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
  383. UINT8 *ptr;
  384. int min_read_size, max_read_size;
  385. /* typical mpeg ts rate is 40 Mbits. DVD rate is about 10
  386. Mbits. We read at most 0.1 second of file to find all streams */
  387. /* XXX: base it on stream bitrate when possible */
  388. if (ic->iformat == &mpegts_demux) {
  389. /* maximum number of bytes we accept to read to find all the streams
  390. in a file */
  391. min_read_size = 3000000;
  392. } else {
  393. min_read_size = 125000;
  394. }
  395. /* max read size is 2 seconds of video max */
  396. max_read_size = min_read_size * 20;
  397. /* set initial codec state */
  398. for(i=0;i<ic->nb_streams;i++) {
  399. st = ic->streams[i];
  400. if (has_codec_parameters(&st->codec))
  401. st->codec_info_state = CSTATE_FOUND;
  402. else
  403. st->codec_info_state = CSTATE_NOTFOUND;
  404. st->codec_info_nb_repeat_frames = 0;
  405. st->codec_info_nb_real_frames = 0;
  406. }
  407. count = 0;
  408. read_size = 0;
  409. ppktl = &ic->packet_buffer;
  410. for(;;) {
  411. /* check if one codec still needs to be handled */
  412. for(i=0;i<ic->nb_streams;i++) {
  413. st = ic->streams[i];
  414. if (st->codec_info_state != CSTATE_FOUND)
  415. break;
  416. }
  417. if (i == ic->nb_streams) {
  418. /* NOTE: if the format has no header, then we need to read
  419. some packets to get most of the streams, so we cannot
  420. stop here */
  421. if (!(ic->iformat->flags & AVFMT_NOHEADER) ||
  422. read_size >= min_read_size) {
  423. /* if we found the info for all the codecs, we can stop */
  424. ret = count;
  425. break;
  426. }
  427. } else {
  428. /* we did not get all the codec info, but we read too much data */
  429. if (read_size >= max_read_size) {
  430. ret = count;
  431. break;
  432. }
  433. }
  434. pktl = av_mallocz(sizeof(AVPacketList));
  435. if (!pktl) {
  436. ret = AVERROR_NOMEM;
  437. break;
  438. }
  439. /* add the packet in the buffered packet list */
  440. *ppktl = pktl;
  441. ppktl = &pktl->next;
  442. /* NOTE: a new stream can be added there if no header in file
  443. (AVFMT_NOHEADER) */
  444. pkt = &pktl->pkt;
  445. if (ic->iformat->read_packet(ic, pkt) < 0) {
  446. /* EOF or error */
  447. ret = -1; /* we could not have all the codec parameters before EOF */
  448. if ((ic->iformat->flags & AVFMT_NOHEADER) &&
  449. i == ic->nb_streams)
  450. ret = 0;
  451. break;
  452. }
  453. read_size += pkt->size;
  454. /* open new codecs */
  455. for(i=0;i<ic->nb_streams;i++) {
  456. st = ic->streams[i];
  457. if (st->codec_info_state == CSTATE_NOTFOUND) {
  458. /* set to found in case of error */
  459. st->codec_info_state = CSTATE_FOUND;
  460. codec = avcodec_find_decoder(st->codec.codec_id);
  461. if (codec) {
  462. ret = avcodec_open(&st->codec, codec);
  463. if (ret >= 0)
  464. st->codec_info_state = CSTATE_DECODING;
  465. }
  466. }
  467. }
  468. st = ic->streams[pkt->stream_index];
  469. if (st->codec_info_state == CSTATE_DECODING) {
  470. /* decode the data and update codec parameters */
  471. ptr = pkt->data;
  472. size = pkt->size;
  473. while (size > 0) {
  474. switch(st->codec.codec_type) {
  475. case CODEC_TYPE_VIDEO:
  476. ret = avcodec_decode_video(&st->codec, &picture,
  477. &got_picture, ptr, size);
  478. break;
  479. case CODEC_TYPE_AUDIO:
  480. ret = avcodec_decode_audio(&st->codec, samples,
  481. &got_picture, ptr, size);
  482. break;
  483. default:
  484. ret = -1;
  485. break;
  486. }
  487. if (ret < 0) {
  488. /* if error, simply ignore because another packet
  489. may be OK */
  490. break;
  491. }
  492. if (got_picture) {
  493. /* we got the parameters - now we can stop
  494. examining this stream */
  495. /* XXX: add a codec info so that we can decide if
  496. the codec can repeat frames */
  497. if (st->codec.codec_id == CODEC_ID_MPEG1VIDEO &&
  498. ic->iformat != &mpegts_demux &&
  499. st->codec.sub_id == 2) {
  500. /* for mpeg2 video, we want to know the real
  501. frame rate, so we decode 40 frames. In mpeg
  502. TS case we do not do it because it would be
  503. too long */
  504. st->codec_info_nb_real_frames++;
  505. st->codec_info_nb_repeat_frames += st->codec.repeat_pict;
  506. #if 0
  507. /* XXX: testing */
  508. if ((st->codec_info_nb_real_frames % 24) == 23) {
  509. st->codec_info_nb_repeat_frames += 2;
  510. }
  511. #endif
  512. /* stop after 40 frames */
  513. if (st->codec_info_nb_real_frames >= 40) {
  514. st->r_frame_rate = (st->codec.frame_rate *
  515. st->codec_info_nb_real_frames) /
  516. (st->codec_info_nb_real_frames +
  517. (st->codec_info_nb_repeat_frames >> 1));
  518. goto close_codec;
  519. }
  520. } else {
  521. close_codec:
  522. st->codec_info_state = CSTATE_FOUND;
  523. avcodec_close(&st->codec);
  524. break;
  525. }
  526. }
  527. ptr += ret;
  528. size -= ret;
  529. }
  530. }
  531. count++;
  532. }
  533. /* close each codec if there are opened */
  534. for(i=0;i<ic->nb_streams;i++) {
  535. st = ic->streams[i];
  536. if (st->codec_info_state == CSTATE_DECODING)
  537. avcodec_close(&st->codec);
  538. }
  539. /* set real frame rate info */
  540. for(i=0;i<ic->nb_streams;i++) {
  541. st = ic->streams[i];
  542. if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
  543. if (!st->r_frame_rate)
  544. st->r_frame_rate = st->codec.frame_rate;
  545. }
  546. }
  547. return ret;
  548. }
  549. /**
  550. * Close a media file (but not its codecs)
  551. *
  552. * @param s media file handle
  553. */
  554. void av_close_input_file(AVFormatContext *s)
  555. {
  556. int i;
  557. if (s->iformat->read_close)
  558. s->iformat->read_close(s);
  559. for(i=0;i<s->nb_streams;i++) {
  560. av_free(s->streams[i]);
  561. }
  562. if (s->packet_buffer) {
  563. AVPacketList *p, *p1;
  564. p = s->packet_buffer;
  565. while (p != NULL) {
  566. p1 = p->next;
  567. av_free_packet(&p->pkt);
  568. av_free(p);
  569. p = p1;
  570. }
  571. s->packet_buffer = NULL;
  572. }
  573. if (!(s->iformat->flags & AVFMT_NOFILE)) {
  574. url_fclose(&s->pb);
  575. }
  576. av_freep(&s->priv_data);
  577. av_free(s);
  578. }
  579. /**
  580. * Add a new stream to a media file. Can only be called in the
  581. * read_header function. If the flag AVFMT_NOHEADER is in the format
  582. * description, then new streams can be added in read_packet too.
  583. *
  584. *
  585. * @param s media file handle
  586. * @param id file format dependent stream id
  587. */
  588. AVStream *av_new_stream(AVFormatContext *s, int id)
  589. {
  590. AVStream *st;
  591. if (s->nb_streams >= MAX_STREAMS)
  592. return NULL;
  593. st = av_mallocz(sizeof(AVStream));
  594. if (!st)
  595. return NULL;
  596. st->index = s->nb_streams;
  597. st->id = id;
  598. s->streams[s->nb_streams++] = st;
  599. return st;
  600. }
  601. /************************************************************/
  602. /* output media file */
  603. /**
  604. * allocate the stream private data and write the stream header to an
  605. * output media file
  606. *
  607. * @param s media file handle
  608. * @return 0 if OK. AVERROR_xxx if error.
  609. */
  610. int av_write_header(AVFormatContext *s)
  611. {
  612. s->priv_data = av_mallocz(s->oformat->priv_data_size);
  613. if (!s->priv_data)
  614. return AVERROR_NOMEM;
  615. return s->oformat->write_header(s);
  616. }
  617. /**
  618. * write a packet to an output media file
  619. *
  620. * @param s media file handle
  621. * @param pkt packet to write
  622. * @param force_pts XXX: need to suppress that
  623. */
  624. int av_write_packet(AVFormatContext *s, AVPacket *pkt, int force_pts)
  625. {
  626. /* XXX: currently, an emulation because internal API must change */
  627. return s->oformat->write_packet(s, pkt->stream_index, pkt->data, pkt->size, force_pts);
  628. }
  629. /**
  630. * write the stream trailer to an output media file and and free the
  631. * file private data.
  632. *
  633. * @param s media file handle
  634. * @return 0 if OK. AVERROR_xxx if error. */
  635. int av_write_trailer(AVFormatContext *s)
  636. {
  637. int ret;
  638. ret = s->oformat->write_trailer(s);
  639. av_freep(&s->priv_data);
  640. return ret;
  641. }
  642. /* "user interface" functions */
  643. void dump_format(AVFormatContext *ic,
  644. int index,
  645. const char *url,
  646. int is_output)
  647. {
  648. int i, flags;
  649. char buf[256];
  650. fprintf(stderr, "%s #%d, %s, %s '%s':\n",
  651. is_output ? "Output" : "Input",
  652. index,
  653. is_output ? ic->oformat->name : ic->iformat->name,
  654. is_output ? "to" : "from", url);
  655. for(i=0;i<ic->nb_streams;i++) {
  656. AVStream *st = ic->streams[i];
  657. avcodec_string(buf, sizeof(buf), &st->codec, is_output);
  658. fprintf(stderr, " Stream #%d.%d", index, i);
  659. /* the pid is an important information, so we display it */
  660. /* XXX: add a generic system */
  661. if (is_output)
  662. flags = ic->oformat->flags;
  663. else
  664. flags = ic->iformat->flags;
  665. if (flags & AVFMT_SHOW_IDS) {
  666. fprintf(stderr, "[0x%x]", st->id);
  667. }
  668. fprintf(stderr, ": %s\n", buf);
  669. }
  670. }
  671. typedef struct {
  672. const char *str;
  673. int width, height;
  674. } SizeEntry;
  675. static SizeEntry sizes[] = {
  676. { "sqcif", 128, 96 },
  677. { "qcif", 176, 144 },
  678. { "cif", 352, 288 },
  679. { "4cif", 704, 576 },
  680. };
  681. int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
  682. {
  683. int i;
  684. int n = sizeof(sizes) / sizeof(SizeEntry);
  685. const char *p;
  686. int frame_width = 0, frame_height = 0;
  687. for(i=0;i<n;i++) {
  688. if (!strcmp(sizes[i].str, str)) {
  689. frame_width = sizes[i].width;
  690. frame_height = sizes[i].height;
  691. break;
  692. }
  693. }
  694. if (i == n) {
  695. p = str;
  696. frame_width = strtol(p, (char **)&p, 10);
  697. if (*p)
  698. p++;
  699. frame_height = strtol(p, (char **)&p, 10);
  700. }
  701. if (frame_width <= 0 || frame_height <= 0)
  702. return -1;
  703. *width_ptr = frame_width;
  704. *height_ptr = frame_height;
  705. return 0;
  706. }
  707. INT64 av_gettime(void)
  708. {
  709. #ifdef CONFIG_WIN32
  710. struct _timeb tb;
  711. _ftime(&tb);
  712. return ((INT64)tb.time * INT64_C(1000) + (INT64)tb.millitm) * INT64_C(1000);
  713. #else
  714. struct timeval tv;
  715. gettimeofday(&tv,NULL);
  716. return (INT64)tv.tv_sec * 1000000 + tv.tv_usec;
  717. #endif
  718. }
  719. /* syntax: [YYYY-MM-DD ][[HH:]MM:]SS[.m...] . Return the date in micro seconds since 1970 */
  720. INT64 parse_date(const char *datestr, int duration)
  721. {
  722. const char *p;
  723. INT64 t;
  724. struct tm dt;
  725. memset(&dt, 0, sizeof(dt));
  726. p = datestr;
  727. if (!duration) {
  728. if (strlen(p) >= 5 && p[4] == '-') {
  729. dt.tm_year = strtol(p, (char **)&p, 10);
  730. if (*p)
  731. p++;
  732. dt.tm_mon = strtol(p, (char **)&p, 10) - 1;
  733. if (*p)
  734. p++;
  735. dt.tm_mday = strtol(p, (char **)&p, 10) - 1;
  736. if (*p)
  737. p++;
  738. } else {
  739. time_t now = time(0);
  740. dt = *localtime(&now);
  741. dt.tm_hour = 0;
  742. dt.tm_min = 0;
  743. dt.tm_sec = 0;
  744. }
  745. }
  746. dt.tm_hour = strtol(p, (char **)&p, 10);
  747. if (*p)
  748. p++;
  749. dt.tm_min = strtol(p, (char **)&p, 10);
  750. if (*p)
  751. p++;
  752. dt.tm_sec = strtol(p, (char **)&p, 10);
  753. if (duration) {
  754. t = (INT64) 1000000 * (dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec);
  755. } else {
  756. t = (INT64) 1000000 * mktime(&dt);
  757. }
  758. if (*p == '.') {
  759. int val, n;
  760. p++;
  761. n = strlen(p);
  762. if (n > 6)
  763. n = 6;
  764. val = strtol(p, NULL, 10);
  765. while (n < 6) {
  766. val = val * 10;
  767. n++;
  768. }
  769. t += val;
  770. }
  771. return t;
  772. }
  773. /* syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done. Return
  774. 1 if found */
  775. int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
  776. {
  777. const char *p;
  778. char tag[128], *q;
  779. p = info;
  780. if (*p == '?')
  781. p++;
  782. for(;;) {
  783. q = tag;
  784. while (*p != '\0' && *p != '=' && *p != '&') {
  785. if ((q - tag) < sizeof(tag) - 1)
  786. *q++ = *p;
  787. p++;
  788. }
  789. *q = '\0';
  790. q = arg;
  791. if (*p == '=') {
  792. p++;
  793. while (*p != '&' && *p != '\0') {
  794. if ((q - arg) < arg_size - 1) {
  795. if (*p == '+')
  796. *q++ = ' ';
  797. else
  798. *q++ = *p;
  799. }
  800. p++;
  801. }
  802. *q = '\0';
  803. }
  804. if (!strcmp(tag, tag1))
  805. return 1;
  806. if (*p != '&')
  807. break;
  808. p++;
  809. }
  810. return 0;
  811. }
  812. /* Return in 'buf' the path with '%d' replaced by number. Also handles
  813. the '%0nd' format where 'n' is the total number of digits and
  814. '%%'. Return 0 if OK, and -1 if format error */
  815. int get_frame_filename(char *buf, int buf_size,
  816. const char *path, int number)
  817. {
  818. const char *p;
  819. char *q, buf1[20];
  820. int nd, len, c, percentd_found;
  821. q = buf;
  822. p = path;
  823. percentd_found = 0;
  824. for(;;) {
  825. c = *p++;
  826. if (c == '\0')
  827. break;
  828. if (c == '%') {
  829. nd = 0;
  830. while (*p >= '0' && *p <= '9') {
  831. nd = nd * 10 + *p++ - '0';
  832. }
  833. c = *p++;
  834. switch(c) {
  835. case '%':
  836. goto addchar;
  837. case 'd':
  838. if (percentd_found)
  839. goto fail;
  840. percentd_found = 1;
  841. snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
  842. len = strlen(buf1);
  843. if ((q - buf + len) > buf_size - 1)
  844. goto fail;
  845. memcpy(q, buf1, len);
  846. q += len;
  847. break;
  848. default:
  849. goto fail;
  850. }
  851. } else {
  852. addchar:
  853. if ((q - buf) < buf_size - 1)
  854. *q++ = c;
  855. }
  856. }
  857. if (!percentd_found)
  858. goto fail;
  859. *q = '\0';
  860. return 0;
  861. fail:
  862. *q = '\0';
  863. return -1;
  864. }
  865. static int gcd(INT64 a, INT64 b)
  866. {
  867. INT64 c;
  868. while (1) {
  869. c = a % b;
  870. if (c == 0)
  871. return b;
  872. a = b;
  873. b = c;
  874. }
  875. }
  876. void ticker_init(Ticker *tick, INT64 inrate, INT64 outrate)
  877. {
  878. int g;
  879. g = gcd(inrate, outrate);
  880. inrate /= g;
  881. outrate /= g;
  882. tick->value = -outrate/2;
  883. tick->inrate = inrate;
  884. tick->outrate = outrate;
  885. tick->div = tick->outrate / tick->inrate;
  886. tick->mod = tick->outrate % tick->inrate;
  887. }
  888. /**
  889. *
  890. * Print on stdout a nice hexa dump of a buffer
  891. * @param buf buffer
  892. * @param size buffer size
  893. */
  894. void av_hex_dump(UINT8 *buf, int size)
  895. {
  896. int len, i, j, c;
  897. for(i=0;i<size;i+=16) {
  898. len = size - i;
  899. if (len > 16)
  900. len = 16;
  901. printf("%08x ", i);
  902. for(j=0;j<16;j++) {
  903. if (j < len)
  904. printf(" %02x", buf[i+j]);
  905. else
  906. printf(" ");
  907. }
  908. printf(" ");
  909. for(j=0;j<len;j++) {
  910. c = buf[i+j];
  911. if (c < ' ' || c > '~')
  912. c = '.';
  913. printf("%c", c);
  914. }
  915. printf("\n");
  916. }
  917. }
  918. void url_split(char *proto, int proto_size,
  919. char *hostname, int hostname_size,
  920. int *port_ptr,
  921. char *path, int path_size,
  922. const char *url)
  923. {
  924. const char *p;
  925. char *q;
  926. int port;
  927. port = -1;
  928. p = url;
  929. q = proto;
  930. while (*p != ':' && *p != '\0') {
  931. if ((q - proto) < proto_size - 1)
  932. *q++ = *p;
  933. p++;
  934. }
  935. if (proto_size > 0)
  936. *q = '\0';
  937. if (*p == '\0') {
  938. if (proto_size > 0)
  939. proto[0] = '\0';
  940. if (hostname_size > 0)
  941. hostname[0] = '\0';
  942. p = url;
  943. } else {
  944. p++;
  945. if (*p == '/')
  946. p++;
  947. if (*p == '/')
  948. p++;
  949. q = hostname;
  950. while (*p != ':' && *p != '/' && *p != '?' && *p != '\0') {
  951. if ((q - hostname) < hostname_size - 1)
  952. *q++ = *p;
  953. p++;
  954. }
  955. if (hostname_size > 0)
  956. *q = '\0';
  957. if (*p == ':') {
  958. p++;
  959. port = strtoul(p, (char **)&p, 10);
  960. }
  961. }
  962. if (port_ptr)
  963. *port_ptr = port;
  964. pstrcpy(path, path_size, p);
  965. }