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.

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