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.

1095 lines
28KB

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