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.

706 lines
15KB

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