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.

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