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.

245 lines
7.0KB

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