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.

1733 lines
46KB

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