aasc.c 5.2 KB

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