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.

651 lines
15KB

  1. /*
  2. * Various utilities for ffmpeg system
  3. * Copyright (c) 2000,2001 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. AVFormat *first_format;
  32. void register_avformat(AVFormat *format)
  33. {
  34. AVFormat **p;
  35. p = &first_format;
  36. while (*p != NULL) p = &(*p)->next;
  37. *p = format;
  38. format->next = NULL;
  39. }
  40. int match_ext(const char *filename, const char *extensions)
  41. {
  42. const char *ext, *p;
  43. char ext1[32], *q;
  44. ext = strrchr(filename, '.');
  45. if (ext) {
  46. ext++;
  47. p = extensions;
  48. for(;;) {
  49. q = ext1;
  50. while (*p != '\0' && *p != ',')
  51. *q++ = *p++;
  52. *q = '\0';
  53. if (!strcasecmp(ext1, ext))
  54. return 1;
  55. if (*p == '\0')
  56. break;
  57. p++;
  58. }
  59. }
  60. return 0;
  61. }
  62. AVFormat *guess_format(const char *short_name, const char *filename, const char *mime_type)
  63. {
  64. AVFormat *fmt, *fmt_found;
  65. int score_max, score;
  66. /* find the proper file type */
  67. fmt_found = NULL;
  68. score_max = 0;
  69. fmt = first_format;
  70. while (fmt != NULL) {
  71. score = 0;
  72. if (fmt->name && short_name && !strcmp(fmt->name, short_name))
  73. score += 100;
  74. if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
  75. score += 10;
  76. if (filename && fmt->extensions &&
  77. match_ext(filename, fmt->extensions)) {
  78. score += 5;
  79. }
  80. if (score > score_max) {
  81. score_max = score;
  82. fmt_found = fmt;
  83. }
  84. fmt = fmt->next;
  85. }
  86. return fmt_found;
  87. }
  88. /* return TRUE if val is a prefix of str. If it returns TRUE, ptr is
  89. set to the next character in 'str' after the prefix */
  90. int strstart(const char *str, const char *val, const char **ptr)
  91. {
  92. const char *p, *q;
  93. p = str;
  94. q = val;
  95. while (*q != '\0') {
  96. if (*p != *q)
  97. return 0;
  98. p++;
  99. q++;
  100. }
  101. if (ptr)
  102. *ptr = p;
  103. return 1;
  104. }
  105. void nstrcpy(char *buf, int buf_size, const char *str)
  106. {
  107. int c;
  108. char *q = buf;
  109. for(;;) {
  110. c = *str++;
  111. if (c == 0 || q >= buf + buf_size - 1)
  112. break;
  113. *q++ = c;
  114. }
  115. *q = '\0';
  116. }
  117. void register_all(void)
  118. {
  119. avcodec_init();
  120. avcodec_register_all();
  121. register_avformat(&mp2_format);
  122. register_avformat(&ac3_format);
  123. register_avformat(&mpeg_mux_format);
  124. register_avformat(&mpeg1video_format);
  125. register_avformat(&mjpeg_format);
  126. register_avformat(&h263_format);
  127. register_avformat(&rm_format);
  128. register_avformat(&asf_format);
  129. register_avformat(&avi_format);
  130. register_avformat(&mov_format);
  131. register_avformat(&mp4_format);
  132. register_avformat(&mpjpeg_format);
  133. register_avformat(&jpeg_format);
  134. register_avformat(&single_jpeg_format);
  135. register_avformat(&swf_format);
  136. register_avformat(&gif_format);
  137. register_avformat(&au_format);
  138. register_avformat(&wav_format);
  139. register_avformat(&pcm_s16le_format);
  140. register_avformat(&pcm_s16be_format);
  141. register_avformat(&pcm_u16le_format);
  142. register_avformat(&pcm_u16be_format);
  143. register_avformat(&pcm_s8_format);
  144. register_avformat(&pcm_u8_format);
  145. register_avformat(&pcm_mulaw_format);
  146. register_avformat(&pcm_alaw_format);
  147. register_avformat(&rawvideo_format);
  148. #ifndef CONFIG_WIN32
  149. register_avformat(&ffm_format);
  150. #endif
  151. register_avformat(&pgm_format);
  152. register_avformat(&ppm_format);
  153. register_avformat(&pgmyuv_format);
  154. register_avformat(&imgyuv_format);
  155. register_avformat(&pgmpipe_format);
  156. register_avformat(&pgmyuvpipe_format);
  157. register_avformat(&ppmpipe_format);
  158. #ifdef CONFIG_GRAB
  159. register_avformat(&video_grab_device_format);
  160. register_avformat(&audio_device_format);
  161. #endif
  162. /* file protocols */
  163. register_protocol(&file_protocol);
  164. register_protocol(&pipe_protocol);
  165. #ifndef CONFIG_WIN32
  166. register_protocol(&udp_protocol);
  167. register_protocol(&http_protocol);
  168. #endif
  169. }
  170. /* memory handling */
  171. int av_new_packet(AVPacket *pkt, int size)
  172. {
  173. pkt->data = malloc(size);
  174. if (!pkt->data)
  175. return -ENOMEM;
  176. pkt->size = size;
  177. /* sane state */
  178. pkt->pts = 0;
  179. pkt->stream_index = 0;
  180. pkt->flags = 0;
  181. return 0;
  182. }
  183. void av_free_packet(AVPacket *pkt)
  184. {
  185. free(pkt->data);
  186. /* fail safe */
  187. pkt->data = NULL;
  188. pkt->size = 0;
  189. }
  190. /* fifo handling */
  191. int fifo_init(FifoBuffer *f, int size)
  192. {
  193. f->buffer = malloc(size);
  194. if (!f->buffer)
  195. return -1;
  196. f->end = f->buffer + size;
  197. f->wptr = f->rptr = f->buffer;
  198. return 0;
  199. }
  200. void fifo_free(FifoBuffer *f)
  201. {
  202. free(f->buffer);
  203. }
  204. int fifo_size(FifoBuffer *f, UINT8 *rptr)
  205. {
  206. int size;
  207. if (f->wptr >= rptr) {
  208. size = f->wptr - rptr;
  209. } else {
  210. size = (f->end - rptr) + (f->wptr - f->buffer);
  211. }
  212. return size;
  213. }
  214. /* get data from the fifo (return -1 if not enough data) */
  215. int fifo_read(FifoBuffer *f, UINT8 *buf, int buf_size, UINT8 **rptr_ptr)
  216. {
  217. UINT8 *rptr = *rptr_ptr;
  218. int size, len;
  219. if (f->wptr >= rptr) {
  220. size = f->wptr - rptr;
  221. } else {
  222. size = (f->end - rptr) + (f->wptr - f->buffer);
  223. }
  224. if (size < buf_size)
  225. return -1;
  226. while (buf_size > 0) {
  227. len = f->end - rptr;
  228. if (len > buf_size)
  229. len = buf_size;
  230. memcpy(buf, rptr, len);
  231. buf += len;
  232. rptr += len;
  233. if (rptr >= f->end)
  234. rptr = f->buffer;
  235. buf_size -= len;
  236. }
  237. *rptr_ptr = rptr;
  238. return 0;
  239. }
  240. void fifo_write(FifoBuffer *f, UINT8 *buf, int size, UINT8 **wptr_ptr)
  241. {
  242. int len;
  243. UINT8 *wptr;
  244. wptr = *wptr_ptr;
  245. while (size > 0) {
  246. len = f->end - wptr;
  247. if (len > size)
  248. len = size;
  249. memcpy(wptr, buf, len);
  250. wptr += len;
  251. if (wptr >= f->end)
  252. wptr = f->buffer;
  253. buf += len;
  254. size -= len;
  255. }
  256. *wptr_ptr = wptr;
  257. }
  258. /* media file handling.
  259. 'filename' is the filename to open.
  260. 'format_name' is used to force the file format (NULL if auto guess).
  261. 'buf_size' is the optional buffer size (zero if default is OK).
  262. 'ap' are additionnal parameters needed when opening the file (NULL if default).
  263. */
  264. AVFormatContext *av_open_input_file(const char *filename,
  265. const char *format_name,
  266. int buf_size,
  267. AVFormatParameters *ap)
  268. {
  269. AVFormat *fmt;
  270. AVFormatContext *ic = NULL;
  271. int err;
  272. ic = av_mallocz(sizeof(AVFormatContext));
  273. if (!ic)
  274. goto fail;
  275. /* find format */
  276. if (format_name != NULL) {
  277. fmt = guess_format(format_name, NULL, NULL);
  278. } else {
  279. fmt = guess_format(NULL, filename, NULL);
  280. }
  281. if (!fmt || !fmt->read_header) {
  282. return NULL;
  283. }
  284. ic->format = fmt;
  285. /* if no file needed do not try to open one */
  286. if (!(fmt->flags & AVFMT_NOFILE)) {
  287. if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0)
  288. goto fail;
  289. if (buf_size > 0) {
  290. url_setbufsize(&ic->pb, buf_size);
  291. }
  292. }
  293. err = ic->format->read_header(ic, ap);
  294. if (err < 0) {
  295. if (!(fmt->flags & AVFMT_NOFILE)) {
  296. url_fclose(&ic->pb);
  297. }
  298. goto fail;
  299. }
  300. return ic;
  301. fail:
  302. if (ic)
  303. free(ic);
  304. return NULL;
  305. }
  306. int av_read_packet(AVFormatContext *s, AVPacket *pkt)
  307. {
  308. AVPacketList *pktl;
  309. pktl = s->packet_buffer;
  310. if (pktl) {
  311. /* read packet from packet buffer, if there is data */
  312. *pkt = pktl->pkt;
  313. s->packet_buffer = pktl->next;
  314. free(pktl);
  315. return 0;
  316. } else {
  317. return s->format->read_packet(s, pkt);
  318. }
  319. }
  320. void av_close_input_file(AVFormatContext *s)
  321. {
  322. int i;
  323. if (s->format->read_close)
  324. s->format->read_close(s);
  325. for(i=0;i<s->nb_streams;i++) {
  326. free(s->streams[i]);
  327. }
  328. if (s->packet_buffer) {
  329. AVPacketList *p, *p1;
  330. p = s->packet_buffer;
  331. while (p != NULL) {
  332. p1 = p->next;
  333. av_free_packet(&p->pkt);
  334. free(p);
  335. p = p1;
  336. }
  337. s->packet_buffer = NULL;
  338. }
  339. if (!(s->format->flags & AVFMT_NOFILE)) {
  340. url_fclose(&s->pb);
  341. }
  342. free(s);
  343. }
  344. int av_write_packet(AVFormatContext *s, AVPacket *pkt, int force_pts)
  345. {
  346. /* XXX: currently, an emulation because internal API must change */
  347. return s->format->write_packet(s, pkt->stream_index, pkt->data, pkt->size, force_pts);
  348. }
  349. /* "user interface" functions */
  350. void dump_format(AVFormatContext *ic,
  351. int index,
  352. const char *url,
  353. int is_output)
  354. {
  355. int i;
  356. char buf[256];
  357. fprintf(stderr, "%s #%d, %s, %s '%s':\n",
  358. is_output ? "Output" : "Input",
  359. index, ic->format->name,
  360. is_output ? "to" : "from", url);
  361. for(i=0;i<ic->nb_streams;i++) {
  362. AVStream *st = ic->streams[i];
  363. avcodec_string(buf, sizeof(buf), &st->codec, is_output);
  364. fprintf(stderr, " Stream #%d.%d: %s\n", index, i, buf);
  365. }
  366. }
  367. typedef struct {
  368. const char *str;
  369. int width, height;
  370. } SizeEntry;
  371. static SizeEntry sizes[] = {
  372. { "sqcif", 128, 96 },
  373. { "qcif", 176, 144 },
  374. { "cif", 352, 288 },
  375. { "4cif", 704, 576 },
  376. };
  377. int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
  378. {
  379. int i;
  380. int n = sizeof(sizes) / sizeof(SizeEntry);
  381. const char *p;
  382. int frame_width = 0, frame_height = 0;
  383. for(i=0;i<n;i++) {
  384. if (!strcmp(sizes[i].str, str)) {
  385. frame_width = sizes[i].width;
  386. frame_height = sizes[i].height;
  387. break;
  388. }
  389. }
  390. if (i == n) {
  391. p = str;
  392. frame_width = strtol(p, (char **)&p, 10);
  393. if (*p)
  394. p++;
  395. frame_height = strtol(p, (char **)&p, 10);
  396. }
  397. if (frame_width <= 0 || frame_height <= 0)
  398. return -1;
  399. *width_ptr = frame_width;
  400. *height_ptr = frame_height;
  401. return 0;
  402. }
  403. INT64 gettime(void)
  404. {
  405. #ifdef CONFIG_WIN32
  406. struct _timeb tb;
  407. _ftime(&tb);
  408. return ((INT64)tb.time * INT64_C(1000) + (INT64)tb.millitm) * INT64_C(1000);
  409. #else
  410. struct timeval tv;
  411. gettimeofday(&tv,NULL);
  412. return (INT64)tv.tv_sec * 1000000 + tv.tv_usec;
  413. #endif
  414. }
  415. /* syntax: [YYYY-MM-DD ][[HH:]MM:]SS[.m...] . Return the date in micro seconds since 1970 */
  416. INT64 parse_date(const char *datestr, int duration)
  417. {
  418. const char *p;
  419. INT64 t;
  420. int sec;
  421. p = datestr;
  422. if (!duration) {
  423. static const UINT8 months[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  424. int year, month, day, i;
  425. if (strlen(p) >= 5 && p[4] == '-') {
  426. year = strtol(p, (char **)&p, 10);
  427. if (*p)
  428. p++;
  429. month = strtol(p, (char **)&p, 10) - 1;
  430. if (*p)
  431. p++;
  432. day = strtol(p, (char **)&p, 10) - 1;
  433. if (*p)
  434. p++;
  435. day += (year - 1970) * 365;
  436. /* if >= March, take February of current year into account too */
  437. if (month >= 2)
  438. year++;
  439. for(i=1970;i<year;i++) {
  440. if ((i % 100) == 0) {
  441. if ((i % 400) == 0) day++;
  442. } else if ((i % 4) == 0) {
  443. day++;
  444. }
  445. }
  446. for(i=0;i<month;i++)
  447. day += months[i];
  448. } else {
  449. day = (time(NULL) / (3600 * 24));
  450. }
  451. t = day * (3600 * 24);
  452. } else {
  453. t = 0;
  454. }
  455. sec = 0;
  456. for(;;) {
  457. int val;
  458. val = strtol(p, (char **)&p, 10);
  459. sec = sec * 60 + val;
  460. if (*p != ':')
  461. break;
  462. p++;
  463. }
  464. t = (t + sec) * 1000000;
  465. if (*p == '.') {
  466. int val, n;
  467. p++;
  468. n = strlen(p);
  469. if (n > 6)
  470. n = 6;
  471. val = strtol(p, NULL, 10);
  472. while (n < 6) {
  473. val = val * 10;
  474. n++;
  475. }
  476. t += val;
  477. }
  478. return t;
  479. }
  480. /* syntax: '?tag1=val1&tag2=val2...'. No URL decoding is done. Return
  481. 1 if found */
  482. int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
  483. {
  484. const char *p;
  485. char tag[128], *q;
  486. p = info;
  487. if (*p == '?')
  488. p++;
  489. for(;;) {
  490. q = tag;
  491. while (*p != '\0' && *p != '=' && *p != '&') {
  492. if ((q - tag) < sizeof(tag) - 1)
  493. *q++ = *p;
  494. p++;
  495. }
  496. *q = '\0';
  497. q = arg;
  498. if (*p == '=') {
  499. p++;
  500. while (*p != '&' && *p != '\0') {
  501. if ((q - arg) < arg_size - 1)
  502. *q++ = *p;
  503. p++;
  504. }
  505. *q = '\0';
  506. }
  507. if (!strcmp(tag, tag1))
  508. return 1;
  509. if (*p != '&')
  510. break;
  511. }
  512. return 0;
  513. }
  514. /* Return in 'buf' the path with '%d' replaced by number. Also handles
  515. the '%0nd' format where 'n' is the total number of digits and
  516. '%%'. Return 0 if OK, and -1 if format error */
  517. int get_frame_filename(char *buf, int buf_size,
  518. const char *path, int number)
  519. {
  520. const char *p;
  521. char *q, buf1[20];
  522. int nd, len, c, percentd_found;
  523. q = buf;
  524. p = path;
  525. percentd_found = 0;
  526. for(;;) {
  527. c = *p++;
  528. if (c == '\0')
  529. break;
  530. if (c == '%') {
  531. nd = 0;
  532. while (*p >= '0' && *p <= '9') {
  533. nd = nd * 10 + *p++ - '0';
  534. }
  535. c = *p++;
  536. switch(c) {
  537. case '%':
  538. goto addchar;
  539. case 'd':
  540. if (percentd_found)
  541. goto fail;
  542. percentd_found = 1;
  543. snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
  544. len = strlen(buf1);
  545. if ((q - buf + len) > buf_size - 1)
  546. goto fail;
  547. memcpy(q, buf1, len);
  548. q += len;
  549. break;
  550. default:
  551. goto fail;
  552. }
  553. } else {
  554. addchar:
  555. if ((q - buf) < buf_size - 1)
  556. *q++ = c;
  557. }
  558. }
  559. if (!percentd_found)
  560. goto fail;
  561. *q = '\0';
  562. return 0;
  563. fail:
  564. *q = '\0';
  565. return -1;
  566. }
  567. static int gcd(INT64 a, INT64 b)
  568. {
  569. INT64 c;
  570. while (1) {
  571. c = a % b;
  572. if (c == 0)
  573. return b;
  574. a = b;
  575. b = c;
  576. }
  577. }
  578. void ticker_init(Ticker *tick, INT64 inrate, INT64 outrate)
  579. {
  580. int g;
  581. g = gcd(inrate, outrate);
  582. inrate /= g;
  583. outrate /= g;
  584. tick->value = -outrate/2;
  585. tick->inrate = inrate;
  586. tick->outrate = outrate;
  587. tick->div = tick->outrate / tick->inrate;
  588. tick->mod = tick->outrate % tick->inrate;
  589. }