pnm.c 16 KB

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