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.

720 lines
16KB

  1. /*
  2. * Image format
  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. #define IMGFMT_YUV 1
  21. #define IMGFMT_PGMYUV 2
  22. #define IMGFMT_PGM 3
  23. #define IMGFMT_PPM 4
  24. typedef struct {
  25. int width;
  26. int height;
  27. int img_number;
  28. int img_size;
  29. int img_fmt;
  30. int is_pipe;
  31. char path[1024];
  32. } VideoData;
  33. static inline int pnm_space(int c)
  34. {
  35. return (c==' ' || c=='\n' || c=='\r' || c=='\t');
  36. }
  37. static void pnm_get(ByteIOContext *f, char *str, int buf_size)
  38. {
  39. char *s;
  40. int c;
  41. do {
  42. c=get_byte(f);
  43. if (c=='#') {
  44. do {
  45. c=get_byte(f);
  46. } while (c!='\n');
  47. c=get_byte(f);
  48. }
  49. } while (pnm_space(c));
  50. s=str;
  51. do {
  52. if (url_feof(f))
  53. break;
  54. if ((s - str) < buf_size - 1)
  55. *s++=c;
  56. c=get_byte(f);
  57. } while (!pnm_space(c));
  58. *s = '\0';
  59. }
  60. static int pgm_read(VideoData *s, ByteIOContext *f, UINT8 *buf, int size, int is_yuv)
  61. {
  62. int width, height, i;
  63. char buf1[32];
  64. UINT8 *picture[3];
  65. width = s->width;
  66. height = s->height;
  67. pnm_get(f, buf1, sizeof(buf1));
  68. if (strcmp(buf1, "P5")) {
  69. return -EIO;
  70. }
  71. pnm_get(f, buf1, sizeof(buf1));
  72. pnm_get(f, buf1, sizeof(buf1));
  73. pnm_get(f, buf1, sizeof(buf1));
  74. picture[0] = buf;
  75. picture[1] = buf + width * height;
  76. picture[2] = buf + width * height + (width * height / 4);
  77. get_buffer(f, picture[0], width * height);
  78. height>>=1;
  79. width>>=1;
  80. if (is_yuv) {
  81. for(i=0;i<height;i++) {
  82. get_buffer(f, picture[1] + i * width, width);
  83. get_buffer(f, picture[2] + i * width, width);
  84. }
  85. } else {
  86. for(i=0;i<height;i++) {
  87. memset(picture[1] + i * width, 128, width);
  88. memset(picture[2] + i * width, 128, width);
  89. }
  90. }
  91. return 0;
  92. }
  93. static int ppm_read(VideoData *s, ByteIOContext *f, UINT8 *buf, int size)
  94. {
  95. int width, height;
  96. char buf1[32];
  97. UINT8 *picture[3];
  98. width = s->width;
  99. height = s->height;
  100. pnm_get(f, buf1, sizeof(buf1));
  101. if (strcmp(buf1, "P6")) {
  102. return -EIO;
  103. }
  104. pnm_get(f, buf1, sizeof(buf1));
  105. pnm_get(f, buf1, sizeof(buf1));
  106. pnm_get(f, buf1, sizeof(buf1));
  107. picture[0] = buf;
  108. get_buffer(f, picture[0], width * height*3);
  109. return 0;
  110. }
  111. static int yuv_read(VideoData *s, const char *filename, UINT8 *buf, int size1)
  112. {
  113. ByteIOContext pb1, *pb = &pb1;
  114. char fname[1024], *p;
  115. int size;
  116. size = s->width * s->height;
  117. strcpy(fname, filename);
  118. p = strrchr(fname, '.');
  119. if (!p || p[1] != 'Y')
  120. return -EIO;
  121. if (url_fopen(pb, fname, URL_RDONLY) < 0)
  122. return -EIO;
  123. get_buffer(pb, buf, size);
  124. url_fclose(pb);
  125. p[1] = 'U';
  126. if (url_fopen(pb, fname, URL_RDONLY) < 0)
  127. return -EIO;
  128. get_buffer(pb, buf + size, size / 4);
  129. url_fclose(pb);
  130. p[1] = 'V';
  131. if (url_fopen(pb, fname, URL_RDONLY) < 0)
  132. return -EIO;
  133. get_buffer(pb, buf + size + (size / 4), size / 4);
  134. url_fclose(pb);
  135. return 0;
  136. }
  137. static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  138. {
  139. VideoData *s = s1->priv_data;
  140. char filename[1024];
  141. int ret;
  142. ByteIOContext f1, *f;
  143. /*
  144. This if-statement destroys pipes - I do not see why it is necessary
  145. if (get_frame_filename(filename, sizeof(filename),
  146. s->path, s->img_number) < 0)
  147. return -EIO;
  148. */
  149. get_frame_filename(filename, sizeof(filename),
  150. s->path, s->img_number);
  151. if (!s->is_pipe) {
  152. f = &f1;
  153. if (url_fopen(f, filename, URL_RDONLY) < 0)
  154. return -EIO;
  155. } else {
  156. f = &s1->pb;
  157. if (url_feof(f))
  158. return -EIO;
  159. }
  160. av_new_packet(pkt, s->img_size);
  161. pkt->stream_index = 0;
  162. switch(s->img_fmt) {
  163. case IMGFMT_PGMYUV:
  164. ret = pgm_read(s, f, pkt->data, pkt->size, 1);
  165. break;
  166. case IMGFMT_PGM:
  167. ret = pgm_read(s, f, pkt->data, pkt->size, 0);
  168. break;
  169. case IMGFMT_YUV:
  170. ret = yuv_read(s, filename, pkt->data, pkt->size);
  171. break;
  172. case IMGFMT_PPM:
  173. ret = ppm_read(s, f, pkt->data, pkt->size);
  174. break;
  175. default:
  176. return -EIO;
  177. }
  178. if (!s->is_pipe) {
  179. url_fclose(f);
  180. }
  181. if (ret < 0) {
  182. av_free_packet(pkt);
  183. return -EIO; /* signal EOF */
  184. } else {
  185. s->img_number++;
  186. return 0;
  187. }
  188. }
  189. static int sizes[][2] = {
  190. { 640, 480 },
  191. { 720, 480 },
  192. { 720, 576 },
  193. { 352, 288 },
  194. { 352, 240 },
  195. { 160, 128 },
  196. { 512, 384 },
  197. { 640, 352 },
  198. { 640, 240 },
  199. };
  200. static int infer_size(int *width_ptr, int *height_ptr, int size)
  201. {
  202. int i;
  203. for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
  204. if ((sizes[i][0] * sizes[i][1]) == size) {
  205. *width_ptr = sizes[i][0];
  206. *height_ptr = sizes[i][1];
  207. return 0;
  208. }
  209. }
  210. return -1;
  211. }
  212. static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  213. {
  214. VideoData *s;
  215. int i, h;
  216. char buf[1024];
  217. char buf1[32];
  218. ByteIOContext pb1, *f = &pb1;
  219. AVStream *st;
  220. s = av_malloc(sizeof(VideoData));
  221. if (!s)
  222. return -ENOMEM;
  223. s1->priv_data = s;
  224. s1->nb_streams = 1;
  225. st = av_mallocz(sizeof(AVStream));
  226. if (!st) {
  227. av_free(s);
  228. return -ENOMEM;
  229. }
  230. s1->streams[0] = st;
  231. strcpy(s->path, s1->filename);
  232. s->img_number = 0;
  233. /* find format */
  234. if (s1->format->flags & AVFMT_NOFILE)
  235. s->is_pipe = 0;
  236. else
  237. s->is_pipe = 1;
  238. if (s1->format == &pgmyuvpipe_format ||
  239. s1->format == &pgmyuv_format)
  240. s->img_fmt = IMGFMT_PGMYUV;
  241. else if (s1->format == &pgmpipe_format ||
  242. s1->format == &pgm_format)
  243. s->img_fmt = IMGFMT_PGM;
  244. else if (s1->format == &imgyuv_format)
  245. s->img_fmt = IMGFMT_YUV;
  246. else if (s1->format == &ppmpipe_format ||
  247. s1->format == &ppm_format)
  248. s->img_fmt = IMGFMT_PPM;
  249. else
  250. goto fail;
  251. if (!s->is_pipe) {
  252. /* try to find the first image */
  253. for(i=0;i<5;i++) {
  254. if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
  255. goto fail;
  256. if (url_fopen(f, buf, URL_RDONLY) >= 0)
  257. break;
  258. s->img_number++;
  259. }
  260. if (i == 5)
  261. goto fail;
  262. } else {
  263. f = &s1->pb;
  264. }
  265. /* find the image size */
  266. /* XXX: use generic file format guessing, as mpeg */
  267. switch(s->img_fmt) {
  268. case IMGFMT_PGM:
  269. case IMGFMT_PGMYUV:
  270. case IMGFMT_PPM:
  271. pnm_get(f, buf1, sizeof(buf1));
  272. pnm_get(f, buf1, sizeof(buf1));
  273. s->width = atoi(buf1);
  274. pnm_get(f, buf1, sizeof(buf1));
  275. h = atoi(buf1);
  276. if (s->img_fmt == IMGFMT_PGMYUV)
  277. h = (h * 2) / 3;
  278. s->height = h;
  279. if (s->width <= 0 ||
  280. s->height <= 0 ||
  281. (s->width % 2) != 0 ||
  282. (s->height % 2) != 0) {
  283. goto fail1;
  284. }
  285. break;
  286. case IMGFMT_YUV:
  287. /* infer size by using the file size. */
  288. {
  289. int img_size;
  290. URLContext *h;
  291. /* XXX: hack hack */
  292. h = url_fileno(f);
  293. img_size = url_seek(h, 0, SEEK_END);
  294. if (infer_size(&s->width, &s->height, img_size) < 0) {
  295. goto fail1;
  296. }
  297. }
  298. break;
  299. }
  300. if (!s->is_pipe) {
  301. url_fclose(f);
  302. } else {
  303. url_fseek(f, 0, SEEK_SET);
  304. }
  305. st->codec.codec_type = CODEC_TYPE_VIDEO;
  306. st->codec.codec_id = CODEC_ID_RAWVIDEO;
  307. st->codec.width = s->width;
  308. st->codec.height = s->height;
  309. if (s->img_fmt == IMGFMT_PPM) {
  310. st->codec.pix_fmt = PIX_FMT_RGB24;
  311. s->img_size = (s->width * s->height * 3);
  312. } else {
  313. st->codec.pix_fmt = PIX_FMT_YUV420P;
  314. s->img_size = (s->width * s->height * 3) / 2;
  315. }
  316. if (!ap || !ap->frame_rate)
  317. st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  318. else
  319. st->codec.frame_rate = ap->frame_rate;
  320. return 0;
  321. fail1:
  322. if (!s->is_pipe)
  323. url_fclose(f);
  324. fail:
  325. av_free(s);
  326. return -EIO;
  327. }
  328. static int img_read_close(AVFormatContext *s1)
  329. {
  330. VideoData *s = s1->priv_data;
  331. av_free(s);
  332. return 0;
  333. }
  334. /******************************************************/
  335. /* image output */
  336. static int pgm_save(AVPicture *picture, int width, int height, ByteIOContext *pb, int is_yuv)
  337. {
  338. int i, h;
  339. char buf[100];
  340. UINT8 *ptr, *ptr1, *ptr2;
  341. h = height;
  342. if (is_yuv)
  343. h = (height * 3) / 2;
  344. snprintf(buf, sizeof(buf),
  345. "P5\n%d %d\n%d\n",
  346. width, h, 255);
  347. put_buffer(pb, buf, strlen(buf));
  348. ptr = picture->data[0];
  349. for(i=0;i<height;i++) {
  350. put_buffer(pb, ptr, width);
  351. ptr += picture->linesize[0];
  352. }
  353. if (is_yuv) {
  354. height >>= 1;
  355. width >>= 1;
  356. ptr1 = picture->data[1];
  357. ptr2 = picture->data[2];
  358. for(i=0;i<height;i++) {
  359. put_buffer(pb, ptr1, width);
  360. put_buffer(pb, ptr2, width);
  361. ptr1 += picture->linesize[1];
  362. ptr2 += picture->linesize[2];
  363. }
  364. }
  365. put_flush_packet(pb);
  366. return 0;
  367. }
  368. static int ppm_save(AVPicture *picture, int width, int height, ByteIOContext *pb)
  369. {
  370. int i;
  371. char buf[100];
  372. UINT8 *ptr;
  373. snprintf(buf, sizeof(buf),
  374. "P6\n%d %d\n%d\n",
  375. width, height, 255);
  376. put_buffer(pb, buf, strlen(buf));
  377. ptr = picture->data[0];
  378. for(i=0;i<height;i++) {
  379. put_buffer(pb, ptr, width * 3);
  380. ptr += picture->linesize[0];
  381. }
  382. put_flush_packet(pb);
  383. return 0;
  384. }
  385. static int yuv_save(AVPicture *picture, int width, int height, const char *filename)
  386. {
  387. ByteIOContext pb1, *pb = &pb1;
  388. char fname[1024], *p;
  389. int i, j;
  390. UINT8 *ptr;
  391. static char *ext = "YUV";
  392. strcpy(fname, filename);
  393. p = strrchr(fname, '.');
  394. if (!p || p[1] != 'Y')
  395. return -EIO;
  396. for(i=0;i<3;i++) {
  397. if (i == 1) {
  398. width >>= 1;
  399. height >>= 1;
  400. }
  401. p[1] = ext[i];
  402. if (url_fopen(pb, fname, URL_WRONLY) < 0)
  403. return -EIO;
  404. ptr = picture->data[i];
  405. for(j=0;j<height;j++) {
  406. put_buffer(pb, ptr, width);
  407. ptr += picture->linesize[i];
  408. }
  409. put_flush_packet(pb);
  410. url_fclose(pb);
  411. }
  412. return 0;
  413. }
  414. static int img_write_header(AVFormatContext *s)
  415. {
  416. VideoData *img;
  417. img = av_mallocz(sizeof(VideoData));
  418. if (!img)
  419. return -1;
  420. s->priv_data = img;
  421. img->img_number = 1;
  422. strcpy(img->path, s->filename);
  423. /* find format */
  424. if (s->format->flags & AVFMT_NOFILE)
  425. img->is_pipe = 0;
  426. else
  427. img->is_pipe = 1;
  428. if (s->format == &pgmyuvpipe_format ||
  429. s->format == &pgmyuv_format) {
  430. img->img_fmt = IMGFMT_PGMYUV;
  431. } else if (s->format == &pgmpipe_format ||
  432. s->format == &pgm_format) {
  433. img->img_fmt = IMGFMT_PGM;
  434. } else if (s->format == &imgyuv_format) {
  435. img->img_fmt = IMGFMT_YUV;
  436. } else if (s->format == &ppmpipe_format ||
  437. s->format == &ppm_format) {
  438. img->img_fmt = IMGFMT_PPM;
  439. } else {
  440. goto fail;
  441. }
  442. return 0;
  443. fail:
  444. av_free(img);
  445. return -EIO;
  446. }
  447. static int img_write_packet(AVFormatContext *s, int stream_index,
  448. UINT8 *buf, int size, int force_pts)
  449. {
  450. VideoData *img = s->priv_data;
  451. AVStream *st = s->streams[stream_index];
  452. ByteIOContext pb1, *pb;
  453. AVPicture picture;
  454. int width, height, ret, size1;
  455. char filename[1024];
  456. width = st->codec.width;
  457. height = st->codec.height;
  458. switch(st->codec.pix_fmt) {
  459. case PIX_FMT_YUV420P:
  460. size1 = (width * height * 3) / 2;
  461. if (size != size1)
  462. return -EIO;
  463. picture.data[0] = buf;
  464. picture.data[1] = picture.data[0] + width * height;
  465. picture.data[2] = picture.data[1] + (width * height) / 4;
  466. picture.linesize[0] = width;
  467. picture.linesize[1] = width >> 1;
  468. picture.linesize[2] = width >> 1;
  469. break;
  470. case PIX_FMT_RGB24:
  471. size1 = (width * height * 3);
  472. if (size != size1)
  473. return -EIO;
  474. picture.data[0] = buf;
  475. picture.linesize[0] = width * 3;
  476. break;
  477. default:
  478. return -EIO;
  479. }
  480. /*
  481. This if-statement destroys pipes - I do not see why it is necessary
  482. if (get_frame_filename(filename, sizeof(filename),
  483. img->path, img->img_number) < 0)
  484. return -EIO;
  485. */
  486. get_frame_filename(filename, sizeof(filename),
  487. img->path, img->img_number);
  488. if (!img->is_pipe) {
  489. pb = &pb1;
  490. if (url_fopen(pb, filename, URL_WRONLY) < 0)
  491. return -EIO;
  492. } else {
  493. pb = &s->pb;
  494. }
  495. switch(img->img_fmt) {
  496. case IMGFMT_PGMYUV:
  497. ret = pgm_save(&picture, width, height, pb, 1);
  498. break;
  499. case IMGFMT_PGM:
  500. ret = pgm_save(&picture, width, height, pb, 0);
  501. break;
  502. case IMGFMT_YUV:
  503. ret = yuv_save(&picture, width, height, filename);
  504. break;
  505. case IMGFMT_PPM:
  506. ret = ppm_save(&picture, width, height, pb);
  507. break;
  508. }
  509. if (!img->is_pipe) {
  510. url_fclose(pb);
  511. }
  512. img->img_number++;
  513. return 0;
  514. }
  515. static int img_write_trailer(AVFormatContext *s)
  516. {
  517. VideoData *img = s->priv_data;
  518. av_free(img);
  519. return 0;
  520. }
  521. AVFormat pgm_format = {
  522. "pgm",
  523. "pgm image format",
  524. "",
  525. "pgm",
  526. CODEC_ID_NONE,
  527. CODEC_ID_RAWVIDEO,
  528. img_write_header,
  529. img_write_packet,
  530. img_write_trailer,
  531. img_read_header,
  532. img_read_packet,
  533. img_read_close,
  534. NULL,
  535. AVFMT_NOFILE | AVFMT_NEEDNUMBER,
  536. };
  537. AVFormat pgmyuv_format = {
  538. "pgmyuv",
  539. "pgm with YUV content image format",
  540. "",
  541. "pgm",
  542. CODEC_ID_NONE,
  543. CODEC_ID_RAWVIDEO,
  544. img_write_header,
  545. img_write_packet,
  546. img_write_trailer,
  547. img_read_header,
  548. img_read_packet,
  549. img_read_close,
  550. NULL,
  551. AVFMT_NOFILE | AVFMT_NEEDNUMBER,
  552. };
  553. AVFormat ppm_format = {
  554. "ppm",
  555. "ppm image format",
  556. "",
  557. "ppm",
  558. CODEC_ID_NONE,
  559. CODEC_ID_RAWVIDEO,
  560. img_write_header,
  561. img_write_packet,
  562. img_write_trailer,
  563. img_read_header,
  564. img_read_packet,
  565. img_read_close,
  566. NULL,
  567. AVFMT_NOFILE | AVFMT_NEEDNUMBER,
  568. };
  569. AVFormat imgyuv_format = {
  570. ".Y.U.V",
  571. ".Y.U.V format",
  572. "",
  573. "Y",
  574. CODEC_ID_NONE,
  575. CODEC_ID_RAWVIDEO,
  576. img_write_header,
  577. img_write_packet,
  578. img_write_trailer,
  579. img_read_header,
  580. img_read_packet,
  581. img_read_close,
  582. NULL,
  583. AVFMT_NOFILE | AVFMT_NEEDNUMBER,
  584. };
  585. AVFormat pgmpipe_format = {
  586. "pgmpipe",
  587. "PGM pipe format",
  588. "",
  589. "pgm",
  590. CODEC_ID_NONE,
  591. CODEC_ID_RAWVIDEO,
  592. img_write_header,
  593. img_write_packet,
  594. img_write_trailer,
  595. img_read_header,
  596. img_read_packet,
  597. img_read_close,
  598. NULL,
  599. };
  600. AVFormat pgmyuvpipe_format = {
  601. "pgmyuvpipe",
  602. "PGM YUV pipe format",
  603. "",
  604. "pgm",
  605. CODEC_ID_NONE,
  606. CODEC_ID_RAWVIDEO,
  607. img_write_header,
  608. img_write_packet,
  609. img_write_trailer,
  610. img_read_header,
  611. img_read_packet,
  612. img_read_close,
  613. NULL,
  614. };
  615. AVFormat ppmpipe_format = {
  616. "ppmpipe",
  617. "PPM pipe format",
  618. "",
  619. "ppm",
  620. CODEC_ID_NONE,
  621. CODEC_ID_RAWVIDEO,
  622. img_write_header,
  623. img_write_packet,
  624. img_write_trailer,
  625. img_read_header,
  626. img_read_packet,
  627. img_read_close,
  628. NULL,
  629. };