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.

584 lines
16KB

  1. /*
  2. * PNM image format
  3. * Copyright (c) 2002, 2003 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "mpegvideo.h" //only for ParseContext
  21. typedef struct PNMContext {
  22. uint8_t *bytestream;
  23. uint8_t *bytestream_start;
  24. uint8_t *bytestream_end;
  25. AVFrame picture;
  26. } PNMContext;
  27. static inline int pnm_space(int c)
  28. {
  29. return (c == ' ' || c == '\n' || c == '\r' || c == '\t');
  30. }
  31. static void pnm_get(PNMContext *sc, char *str, int buf_size)
  32. {
  33. char *s;
  34. int c;
  35. /* skip spaces and comments */
  36. for(;;) {
  37. c = *sc->bytestream++;
  38. if (c == '#') {
  39. do {
  40. c = *sc->bytestream++;
  41. } while (c != '\n' && sc->bytestream < sc->bytestream_end);
  42. } else if (!pnm_space(c)) {
  43. break;
  44. }
  45. }
  46. s = str;
  47. while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
  48. if ((s - str) < buf_size - 1)
  49. *s++ = c;
  50. c = *sc->bytestream++;
  51. }
  52. *s = '\0';
  53. }
  54. static int common_init(AVCodecContext *avctx){
  55. PNMContext *s = avctx->priv_data;
  56. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  57. avctx->coded_frame= (AVFrame*)&s->picture;
  58. return 0;
  59. }
  60. static int pnm_decode_header(AVCodecContext *avctx, PNMContext * const s){
  61. char buf1[32], tuple_type[32];
  62. int h, w, depth, maxval;;
  63. pnm_get(s, buf1, sizeof(buf1));
  64. if (!strcmp(buf1, "P4")) {
  65. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  66. } else if (!strcmp(buf1, "P5")) {
  67. if (avctx->codec_id == CODEC_ID_PGMYUV)
  68. avctx->pix_fmt = PIX_FMT_YUV420P;
  69. else
  70. avctx->pix_fmt = PIX_FMT_GRAY8;
  71. } else if (!strcmp(buf1, "P6")) {
  72. avctx->pix_fmt = PIX_FMT_RGB24;
  73. } else if (!strcmp(buf1, "P7")) {
  74. w = -1;
  75. h = -1;
  76. maxval = -1;
  77. depth = -1;
  78. tuple_type[0] = '\0';
  79. for(;;) {
  80. pnm_get(s, buf1, sizeof(buf1));
  81. if (!strcmp(buf1, "WIDTH")) {
  82. pnm_get(s, buf1, sizeof(buf1));
  83. w = strtol(buf1, NULL, 10);
  84. } else if (!strcmp(buf1, "HEIGHT")) {
  85. pnm_get(s, buf1, sizeof(buf1));
  86. h = strtol(buf1, NULL, 10);
  87. } else if (!strcmp(buf1, "DEPTH")) {
  88. pnm_get(s, buf1, sizeof(buf1));
  89. depth = strtol(buf1, NULL, 10);
  90. } else if (!strcmp(buf1, "MAXVAL")) {
  91. pnm_get(s, buf1, sizeof(buf1));
  92. maxval = strtol(buf1, NULL, 10);
  93. } else if (!strcmp(buf1, "TUPLETYPE")) {
  94. pnm_get(s, tuple_type, sizeof(tuple_type));
  95. } else if (!strcmp(buf1, "ENDHDR")) {
  96. break;
  97. } else {
  98. return -1;
  99. }
  100. }
  101. /* check that all tags are present */
  102. if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || avcodec_check_dimensions(avctx, w, h))
  103. return -1;
  104. avctx->width = w;
  105. avctx->height = h;
  106. if (depth == 1) {
  107. if (maxval == 1)
  108. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  109. else
  110. avctx->pix_fmt = PIX_FMT_GRAY8;
  111. } else if (depth == 3) {
  112. avctx->pix_fmt = PIX_FMT_RGB24;
  113. } else if (depth == 4) {
  114. avctx->pix_fmt = PIX_FMT_RGBA32;
  115. } else {
  116. return -1;
  117. }
  118. return 0;
  119. } else {
  120. return -1;
  121. }
  122. pnm_get(s, buf1, sizeof(buf1));
  123. avctx->width = atoi(buf1);
  124. if (avctx->width <= 0)
  125. return -1;
  126. pnm_get(s, buf1, sizeof(buf1));
  127. avctx->height = atoi(buf1);
  128. if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
  129. return -1;
  130. if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
  131. pnm_get(s, buf1, sizeof(buf1));
  132. }
  133. /* more check if YUV420 */
  134. if (avctx->pix_fmt == PIX_FMT_YUV420P) {
  135. if ((avctx->width & 1) != 0)
  136. return -1;
  137. h = (avctx->height * 2);
  138. if ((h % 3) != 0)
  139. return -1;
  140. h /= 3;
  141. avctx->height = h;
  142. }
  143. return 0;
  144. }
  145. static int pnm_decode_frame(AVCodecContext *avctx,
  146. void *data, int *data_size,
  147. uint8_t *buf, int buf_size)
  148. {
  149. PNMContext * const s = avctx->priv_data;
  150. AVFrame *picture = data;
  151. AVFrame * const p= (AVFrame*)&s->picture;
  152. int i, n, linesize, h;
  153. unsigned char *ptr;
  154. /* special case for last picture */
  155. if (buf_size == 0) {
  156. return 0;
  157. }
  158. s->bytestream_start=
  159. s->bytestream= buf;
  160. s->bytestream_end= buf + buf_size;
  161. if(pnm_decode_header(avctx, s) < 0)
  162. return -1;
  163. if(p->data[0])
  164. avctx->release_buffer(avctx, p);
  165. p->reference= 0;
  166. if(avctx->get_buffer(avctx, p) < 0){
  167. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  168. return -1;
  169. }
  170. p->pict_type= FF_I_TYPE;
  171. p->key_frame= 1;
  172. switch(avctx->pix_fmt) {
  173. default:
  174. return -1;
  175. case PIX_FMT_RGB24:
  176. n = avctx->width * 3;
  177. goto do_read;
  178. case PIX_FMT_GRAY8:
  179. n = avctx->width;
  180. goto do_read;
  181. case PIX_FMT_MONOWHITE:
  182. case PIX_FMT_MONOBLACK:
  183. n = (avctx->width + 7) >> 3;
  184. do_read:
  185. ptr = p->data[0];
  186. linesize = p->linesize[0];
  187. for(i = 0; i < avctx->height; i++) {
  188. memcpy(ptr, s->bytestream, n);
  189. s->bytestream += n;
  190. ptr += linesize;
  191. }
  192. break;
  193. case PIX_FMT_YUV420P:
  194. {
  195. unsigned char *ptr1, *ptr2;
  196. n = avctx->width;
  197. ptr = p->data[0];
  198. linesize = p->linesize[0];
  199. for(i = 0; i < avctx->height; i++) {
  200. memcpy(ptr, s->bytestream, n);
  201. s->bytestream += n;
  202. ptr += linesize;
  203. }
  204. ptr1 = p->data[1];
  205. ptr2 = p->data[2];
  206. n >>= 1;
  207. h = avctx->height >> 1;
  208. for(i = 0; i < h; i++) {
  209. memcpy(ptr1, s->bytestream, n);
  210. s->bytestream += n;
  211. memcpy(ptr2, s->bytestream, n);
  212. s->bytestream += n;
  213. ptr1 += p->linesize[1];
  214. ptr2 += p->linesize[2];
  215. }
  216. }
  217. break;
  218. case PIX_FMT_RGBA32:
  219. ptr = p->data[0];
  220. linesize = p->linesize[0];
  221. for(i = 0; i < avctx->height; i++) {
  222. int j, r, g, b, a;
  223. for(j = 0;j < avctx->width; j++) {
  224. r = *s->bytestream++;
  225. g = *s->bytestream++;
  226. b = *s->bytestream++;
  227. a = *s->bytestream++;
  228. ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
  229. }
  230. ptr += linesize;
  231. }
  232. break;
  233. }
  234. *picture= *(AVFrame*)&s->picture;
  235. *data_size = sizeof(AVPicture);
  236. return s->bytestream - s->bytestream_start;
  237. }
  238. static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
  239. PNMContext *s = avctx->priv_data;
  240. AVFrame *pict = data;
  241. AVFrame * const p= (AVFrame*)&s->picture;
  242. int i, h, h1, c, n, linesize;
  243. uint8_t *ptr, *ptr1, *ptr2;
  244. if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
  245. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  246. return -1;
  247. }
  248. *p = *pict;
  249. p->pict_type= FF_I_TYPE;
  250. p->key_frame= 1;
  251. s->bytestream_start=
  252. s->bytestream= outbuf;
  253. s->bytestream_end= outbuf+buf_size;
  254. h = avctx->height;
  255. h1 = h;
  256. switch(avctx->pix_fmt) {
  257. case PIX_FMT_MONOWHITE:
  258. c = '4';
  259. n = (avctx->width + 7) >> 3;
  260. break;
  261. case PIX_FMT_GRAY8:
  262. c = '5';
  263. n = avctx->width;
  264. break;
  265. case PIX_FMT_RGB24:
  266. c = '6';
  267. n = avctx->width * 3;
  268. break;
  269. case PIX_FMT_YUV420P:
  270. c = '5';
  271. n = avctx->width;
  272. h1 = (h * 3) / 2;
  273. break;
  274. default:
  275. return -1;
  276. }
  277. snprintf(s->bytestream, s->bytestream_end - s->bytestream,
  278. "P%c\n%d %d\n",
  279. c, avctx->width, h1);
  280. s->bytestream += strlen(s->bytestream);
  281. if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
  282. snprintf(s->bytestream, s->bytestream_end - s->bytestream,
  283. "%d\n", 255);
  284. s->bytestream += strlen(s->bytestream);
  285. }
  286. ptr = p->data[0];
  287. linesize = p->linesize[0];
  288. for(i=0;i<h;i++) {
  289. memcpy(s->bytestream, ptr, n);
  290. s->bytestream += n;
  291. ptr += linesize;
  292. }
  293. if (avctx->pix_fmt == PIX_FMT_YUV420P) {
  294. h >>= 1;
  295. n >>= 1;
  296. ptr1 = p->data[1];
  297. ptr2 = p->data[2];
  298. for(i=0;i<h;i++) {
  299. memcpy(s->bytestream, ptr1, n);
  300. s->bytestream += n;
  301. memcpy(s->bytestream, ptr2, n);
  302. s->bytestream += n;
  303. ptr1 += p->linesize[1];
  304. ptr2 += p->linesize[2];
  305. }
  306. }
  307. return s->bytestream - s->bytestream_start;
  308. }
  309. static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
  310. PNMContext *s = avctx->priv_data;
  311. AVFrame *pict = data;
  312. AVFrame * const p= (AVFrame*)&s->picture;
  313. int i, h, w, n, linesize, depth, maxval;
  314. const char *tuple_type;
  315. uint8_t *ptr;
  316. if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
  317. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  318. return -1;
  319. }
  320. *p = *pict;
  321. p->pict_type= FF_I_TYPE;
  322. p->key_frame= 1;
  323. s->bytestream_start=
  324. s->bytestream= outbuf;
  325. s->bytestream_end= outbuf+buf_size;
  326. h = avctx->height;
  327. w = avctx->width;
  328. switch(avctx->pix_fmt) {
  329. case PIX_FMT_MONOWHITE:
  330. n = (w + 7) >> 3;
  331. depth = 1;
  332. maxval = 1;
  333. tuple_type = "BLACKANDWHITE";
  334. break;
  335. case PIX_FMT_GRAY8:
  336. n = w;
  337. depth = 1;
  338. maxval = 255;
  339. tuple_type = "GRAYSCALE";
  340. break;
  341. case PIX_FMT_RGB24:
  342. n = w * 3;
  343. depth = 3;
  344. maxval = 255;
  345. tuple_type = "RGB";
  346. break;
  347. case PIX_FMT_RGBA32:
  348. n = w * 4;
  349. depth = 4;
  350. maxval = 255;
  351. tuple_type = "RGB_ALPHA";
  352. break;
  353. default:
  354. return -1;
  355. }
  356. snprintf(s->bytestream, s->bytestream_end - s->bytestream,
  357. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
  358. w, h, depth, maxval, tuple_type);
  359. s->bytestream += strlen(s->bytestream);
  360. ptr = p->data[0];
  361. linesize = p->linesize[0];
  362. if (avctx->pix_fmt == PIX_FMT_RGBA32) {
  363. int j;
  364. unsigned int v;
  365. for(i=0;i<h;i++) {
  366. for(j=0;j<w;j++) {
  367. v = ((uint32_t *)ptr)[j];
  368. *s->bytestream++ = v >> 16;
  369. *s->bytestream++ = v >> 8;
  370. *s->bytestream++ = v;
  371. *s->bytestream++ = v >> 24;
  372. }
  373. ptr += linesize;
  374. }
  375. } else {
  376. for(i=0;i<h;i++) {
  377. memcpy(s->bytestream, ptr, n);
  378. s->bytestream += n;
  379. ptr += linesize;
  380. }
  381. }
  382. return s->bytestream - s->bytestream_start;
  383. }
  384. #if 0
  385. static int pnm_probe(AVProbeData *pd)
  386. {
  387. const char *p = pd->buf;
  388. if (pd->buf_size >= 8 &&
  389. p[0] == 'P' &&
  390. p[1] >= '4' && p[1] <= '6' &&
  391. pnm_space(p[2]) )
  392. return AVPROBE_SCORE_MAX - 1; /* to permit pgmyuv probe */
  393. else
  394. return 0;
  395. }
  396. static int pgmyuv_probe(AVProbeData *pd)
  397. {
  398. if (match_ext(pd->filename, "pgmyuv"))
  399. return AVPROBE_SCORE_MAX;
  400. else
  401. return 0;
  402. }
  403. static int pam_probe(AVProbeData *pd)
  404. {
  405. const char *p = pd->buf;
  406. if (pd->buf_size >= 8 &&
  407. p[0] == 'P' &&
  408. p[1] == '7' &&
  409. p[2] == '\n')
  410. return AVPROBE_SCORE_MAX;
  411. else
  412. return 0;
  413. }
  414. #endif
  415. static int pnm_parse(AVCodecParserContext *s,
  416. AVCodecContext *avctx,
  417. uint8_t **poutbuf, int *poutbuf_size,
  418. const uint8_t *buf, int buf_size)
  419. {
  420. ParseContext *pc = s->priv_data;
  421. PNMContext pnmctx;
  422. int next;
  423. for(; pc->overread>0; pc->overread--){
  424. pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
  425. }
  426. retry:
  427. if(pc->index){
  428. pnmctx.bytestream_start=
  429. pnmctx.bytestream= pc->buffer;
  430. pnmctx.bytestream_end= pc->buffer + pc->index;
  431. }else{
  432. pnmctx.bytestream_start=
  433. pnmctx.bytestream= buf;
  434. pnmctx.bytestream_end= buf + buf_size;
  435. }
  436. if(pnm_decode_header(avctx, &pnmctx) < 0){
  437. if(pnmctx.bytestream < pnmctx.bytestream_end){
  438. if(pc->index){
  439. pc->index=0;
  440. }else{
  441. buf++;
  442. buf_size--;
  443. }
  444. goto retry;
  445. }
  446. #if 0
  447. if(pc->index && pc->index*2 + FF_INPUT_BUFFER_PADDING_SIZE < pc->buffer_size && buf_size > pc->index){
  448. memcpy(pc->buffer + pc->index, buf, pc->index);
  449. pc->index += pc->index;
  450. buf += pc->index;
  451. buf_size -= pc->index;
  452. goto retry;
  453. }
  454. #endif
  455. next= END_NOT_FOUND;
  456. }else{
  457. next= pnmctx.bytestream - pnmctx.bytestream_start
  458. + avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  459. if(pnmctx.bytestream_start!=buf)
  460. next-= pc->index;
  461. if(next > buf_size)
  462. next= END_NOT_FOUND;
  463. }
  464. if(ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size)<0){
  465. *poutbuf = NULL;
  466. *poutbuf_size = 0;
  467. return buf_size;
  468. }
  469. *poutbuf = (uint8_t *)buf;
  470. *poutbuf_size = buf_size;
  471. return next;
  472. }
  473. AVCodecParser pnm_parser = {
  474. { CODEC_ID_PGM, CODEC_ID_PGMYUV, CODEC_ID_PPM, CODEC_ID_PBM, CODEC_ID_PAM},
  475. sizeof(ParseContext),
  476. NULL,
  477. pnm_parse,
  478. ff_parse_close,
  479. };
  480. AVCodec pgm_encoder = {
  481. "pgm",
  482. CODEC_TYPE_VIDEO,
  483. CODEC_ID_PGM,
  484. sizeof(PNMContext),
  485. common_init,
  486. pnm_encode_frame,
  487. NULL, //encode_end,
  488. pnm_decode_frame,
  489. .pix_fmts= (enum PixelFormat[]){PIX_FMT_GRAY8, -1},
  490. };
  491. AVCodec pgmyuv_encoder = {
  492. "pgmyuv",
  493. CODEC_TYPE_VIDEO,
  494. CODEC_ID_PGMYUV,
  495. sizeof(PNMContext),
  496. common_init,
  497. pnm_encode_frame,
  498. NULL, //encode_end,
  499. pnm_decode_frame,
  500. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
  501. };
  502. AVCodec ppm_encoder = {
  503. "ppm",
  504. CODEC_TYPE_VIDEO,
  505. CODEC_ID_PPM,
  506. sizeof(PNMContext),
  507. common_init,
  508. pnm_encode_frame,
  509. NULL, //encode_end,
  510. pnm_decode_frame,
  511. .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, -1},
  512. };
  513. AVCodec pbm_encoder = {
  514. "pbm",
  515. CODEC_TYPE_VIDEO,
  516. CODEC_ID_PBM,
  517. sizeof(PNMContext),
  518. common_init,
  519. pnm_encode_frame,
  520. NULL, //encode_end,
  521. pnm_decode_frame,
  522. .pix_fmts= (enum PixelFormat[]){PIX_FMT_MONOWHITE, -1},
  523. };
  524. AVCodec pam_encoder = {
  525. "pam",
  526. CODEC_TYPE_VIDEO,
  527. CODEC_ID_PAM,
  528. sizeof(PNMContext),
  529. common_init,
  530. pam_encode_frame,
  531. NULL, //encode_end,
  532. pnm_decode_frame,
  533. .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, -1},
  534. };