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.

222 lines
6.5KB

  1. /*
  2. * BRender PIX (.pix) image decoder
  3. * Copyright (c) 2012 Aleksi Nurmi
  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. /*
  22. * Tested against samples from I-War / Independence War and Defiance.
  23. * If the PIX file does not contain a palette, the
  24. * palette_has_changed property of the AVFrame is set to 0.
  25. */
  26. #include "libavutil/imgutils.h"
  27. #include "avcodec.h"
  28. #include "bytestream.h"
  29. #include "internal.h"
  30. typedef struct BRPixHeader {
  31. int format;
  32. unsigned int width, height;
  33. } BRPixHeader;
  34. static int brpix_decode_header(BRPixHeader *out, GetByteContext *pgb)
  35. {
  36. unsigned int header_len = bytestream2_get_be32(pgb);
  37. out->format = bytestream2_get_byte(pgb);
  38. bytestream2_skip(pgb, 2);
  39. out->width = bytestream2_get_be16(pgb);
  40. out->height = bytestream2_get_be16(pgb);
  41. // the header is at least 11 bytes long; we read the first 7
  42. if (header_len < 11) {
  43. return 0;
  44. }
  45. // skip the rest of the header
  46. bytestream2_skip(pgb, header_len-7);
  47. return 1;
  48. }
  49. static int brpix_decode_frame(AVCodecContext *avctx,
  50. void *data, int *got_frame,
  51. AVPacket *avpkt)
  52. {
  53. AVFrame *frame = data;
  54. int ret;
  55. GetByteContext gb;
  56. unsigned int bytes_pp;
  57. unsigned int magic[4];
  58. unsigned int chunk_type;
  59. unsigned int data_len;
  60. BRPixHeader hdr;
  61. bytestream2_init(&gb, avpkt->data, avpkt->size);
  62. magic[0] = bytestream2_get_be32(&gb);
  63. magic[1] = bytestream2_get_be32(&gb);
  64. magic[2] = bytestream2_get_be32(&gb);
  65. magic[3] = bytestream2_get_be32(&gb);
  66. if (magic[0] != 0x12 ||
  67. magic[1] != 0x8 ||
  68. magic[2] != 0x2 ||
  69. magic[3] != 0x2) {
  70. av_log(avctx, AV_LOG_ERROR, "Not a BRender PIX file\n");
  71. return AVERROR_INVALIDDATA;
  72. }
  73. chunk_type = bytestream2_get_be32(&gb);
  74. if (chunk_type != 0x3 && chunk_type != 0x3d) {
  75. av_log(avctx, AV_LOG_ERROR, "Invalid chunk type %d\n", chunk_type);
  76. return AVERROR_INVALIDDATA;
  77. }
  78. ret = brpix_decode_header(&hdr, &gb);
  79. if (!ret) {
  80. av_log(avctx, AV_LOG_ERROR, "Invalid header length\n");
  81. return AVERROR_INVALIDDATA;
  82. }
  83. switch (hdr.format) {
  84. case 3:
  85. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  86. bytes_pp = 1;
  87. break;
  88. case 4:
  89. avctx->pix_fmt = AV_PIX_FMT_RGB555BE;
  90. bytes_pp = 2;
  91. break;
  92. case 5:
  93. avctx->pix_fmt = AV_PIX_FMT_RGB565BE;
  94. bytes_pp = 2;
  95. break;
  96. case 6:
  97. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  98. bytes_pp = 3;
  99. break;
  100. case 7:
  101. avctx->pix_fmt = AV_PIX_FMT_0RGB;
  102. bytes_pp = 4;
  103. break;
  104. case 18:
  105. avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
  106. bytes_pp = 2;
  107. break;
  108. default:
  109. av_log(avctx, AV_LOG_ERROR, "Format %d is not supported\n",
  110. hdr.format);
  111. return AVERROR_PATCHWELCOME;
  112. }
  113. if (av_image_check_size(hdr.width, hdr.height, 0, avctx) < 0)
  114. return AVERROR_INVALIDDATA;
  115. if (hdr.width != avctx->width || hdr.height != avctx->height)
  116. avcodec_set_dimensions(avctx, hdr.width, hdr.height);
  117. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  118. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  119. return ret;
  120. }
  121. chunk_type = bytestream2_get_be32(&gb);
  122. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
  123. (chunk_type == 0x3 || chunk_type == 0x3d)) {
  124. BRPixHeader palhdr;
  125. uint32_t *pal_out = (uint32_t *)frame->data[1];
  126. int i;
  127. ret = brpix_decode_header(&palhdr, &gb);
  128. if (!ret) {
  129. av_log(avctx, AV_LOG_ERROR, "Invalid palette header length\n");
  130. return AVERROR_INVALIDDATA;
  131. }
  132. if (palhdr.format != 7) {
  133. av_log(avctx, AV_LOG_ERROR, "Palette is not in 0RGB format\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. chunk_type = bytestream2_get_be32(&gb);
  137. data_len = bytestream2_get_be32(&gb);
  138. bytestream2_skip(&gb, 8);
  139. if (chunk_type != 0x21 || data_len != 1032 ||
  140. bytestream2_get_bytes_left(&gb) < 1032) {
  141. av_log(avctx, AV_LOG_ERROR, "Invalid palette data\n");
  142. return AVERROR_INVALIDDATA;
  143. }
  144. // convert 0RGB to machine endian format (ARGB32)
  145. for (i = 0; i < 256; ++i) {
  146. bytestream2_skipu(&gb, 1);
  147. *pal_out++ = (0xFFU << 24) | bytestream2_get_be24u(&gb);
  148. }
  149. bytestream2_skip(&gb, 8);
  150. frame->palette_has_changed = 1;
  151. chunk_type = bytestream2_get_be32(&gb);
  152. } else if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  153. uint32_t *pal_out = (uint32_t *)frame->data[1];
  154. int i;
  155. for (i = 0; i < 256; ++i) {
  156. *pal_out++ = (0xFFU << 24) | (i * 0x010101);
  157. }
  158. frame->palette_has_changed = 1;
  159. }
  160. data_len = bytestream2_get_be32(&gb);
  161. bytestream2_skip(&gb, 8);
  162. // read the image data to the buffer
  163. {
  164. unsigned int bytes_per_scanline = bytes_pp * hdr.width;
  165. unsigned int bytes_left = bytestream2_get_bytes_left(&gb);
  166. if (chunk_type != 0x21 || data_len != bytes_left ||
  167. bytes_left / bytes_per_scanline < hdr.height)
  168. {
  169. av_log(avctx, AV_LOG_ERROR, "Invalid image data\n");
  170. return AVERROR_INVALIDDATA;
  171. }
  172. av_image_copy_plane(frame->data[0], frame->linesize[0],
  173. avpkt->data + bytestream2_tell(&gb),
  174. bytes_per_scanline,
  175. bytes_per_scanline, hdr.height);
  176. }
  177. *got_frame = 1;
  178. return avpkt->size;
  179. }
  180. AVCodec ff_brender_pix_decoder = {
  181. .name = "brender_pix",
  182. .type = AVMEDIA_TYPE_VIDEO,
  183. .id = AV_CODEC_ID_BRENDER_PIX,
  184. .decode = brpix_decode_frame,
  185. .capabilities = CODEC_CAP_DR1,
  186. .long_name = NULL_IF_CONFIG_SMALL("BRender PIX image"),
  187. };