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.

1099 lines
28KB

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