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.

177 lines
5.2KB

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