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.

175 lines
5.2KB

  1. /*
  2. * Autodesc RLE Decoder
  3. * Copyright (C) 2005 the ffmpeg project
  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. /**
  20. * @file aasc.c
  21. * Autodesc RLE Video Decoder by Konstantin Shishkov
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "common.h"
  27. #include "avcodec.h"
  28. #include "dsputil.h"
  29. typedef struct AascContext {
  30. AVCodecContext *avctx;
  31. AVFrame frame;
  32. } AascContext;
  33. #define FETCH_NEXT_STREAM_BYTE() \
  34. if (stream_ptr >= buf_size) \
  35. { \
  36. av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (fetch)\n"); \
  37. break; \
  38. } \
  39. stream_byte = buf[stream_ptr++];
  40. static int aasc_decode_init(AVCodecContext *avctx)
  41. {
  42. AascContext *s = (AascContext *)avctx->priv_data;
  43. s->avctx = avctx;
  44. avctx->pix_fmt = PIX_FMT_BGR24;
  45. avctx->has_b_frames = 0;
  46. s->frame.data[0] = NULL;
  47. return 0;
  48. }
  49. static int aasc_decode_frame(AVCodecContext *avctx,
  50. void *data, int *data_size,
  51. uint8_t *buf, int buf_size)
  52. {
  53. AascContext *s = (AascContext *)avctx->priv_data;
  54. int stream_ptr = 4;
  55. unsigned char rle_code;
  56. unsigned char stream_byte;
  57. int pixel_ptr = 0;
  58. int row_dec, row_ptr;
  59. int frame_size;
  60. int i;
  61. s->frame.reference = 1;
  62. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  63. if (avctx->reget_buffer(avctx, &s->frame)) {
  64. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  65. return -1;
  66. }
  67. row_dec = s->frame.linesize[0];
  68. row_ptr = (s->avctx->height - 1) * row_dec;
  69. frame_size = row_dec * s->avctx->height;
  70. while (row_ptr >= 0) {
  71. FETCH_NEXT_STREAM_BYTE();
  72. rle_code = stream_byte;
  73. if (rle_code == 0) {
  74. /* fetch the next byte to see how to handle escape code */
  75. FETCH_NEXT_STREAM_BYTE();
  76. if (stream_byte == 0) {
  77. /* line is done, goto the next one */
  78. row_ptr -= row_dec;
  79. pixel_ptr = 0;
  80. } else if (stream_byte == 1) {
  81. /* decode is done */
  82. break;
  83. } else if (stream_byte == 2) {
  84. /* reposition frame decode coordinates */
  85. FETCH_NEXT_STREAM_BYTE();
  86. pixel_ptr += stream_byte;
  87. FETCH_NEXT_STREAM_BYTE();
  88. row_ptr -= stream_byte * row_dec;
  89. } else {
  90. /* copy pixels from encoded stream */
  91. if ((pixel_ptr + stream_byte > avctx->width * 3) ||
  92. (row_ptr < 0)) {
  93. av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (copy1)\n");
  94. break;
  95. }
  96. rle_code = stream_byte;
  97. if (stream_ptr + rle_code > buf_size) {
  98. av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (copy2)\n");
  99. break;
  100. }
  101. for (i = 0; i < rle_code; i++) {
  102. FETCH_NEXT_STREAM_BYTE();
  103. s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
  104. pixel_ptr++;
  105. }
  106. if (rle_code & 1)
  107. stream_ptr++;
  108. }
  109. } else {
  110. /* decode a run of data */
  111. if ((pixel_ptr + rle_code > avctx->width * 3) ||
  112. (row_ptr < 0)) {
  113. av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (run1)\n");
  114. break;
  115. }
  116. FETCH_NEXT_STREAM_BYTE();
  117. while(rle_code--) {
  118. s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
  119. pixel_ptr++;
  120. }
  121. }
  122. }
  123. /* one last sanity check on the way out */
  124. if (stream_ptr < buf_size)
  125. av_log(s->avctx, AV_LOG_ERROR, " AASC: ended frame decode with bytes left over (%d < %d)\n",
  126. stream_ptr, buf_size);
  127. *data_size = sizeof(AVFrame);
  128. *(AVFrame*)data = s->frame;
  129. /* report that the buffer was completely consumed */
  130. return buf_size;
  131. }
  132. static int aasc_decode_end(AVCodecContext *avctx)
  133. {
  134. AascContext *s = (AascContext *)avctx->priv_data;
  135. /* release the last frame */
  136. if (s->frame.data[0])
  137. avctx->release_buffer(avctx, &s->frame);
  138. return 0;
  139. }
  140. AVCodec aasc_decoder = {
  141. "aasc",
  142. CODEC_TYPE_VIDEO,
  143. CODEC_ID_AASC,
  144. sizeof(AascContext),
  145. aasc_decode_init,
  146. NULL,
  147. aasc_decode_end,
  148. aasc_decode_frame,
  149. CODEC_CAP_DR1,
  150. };