00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030
00031 #include "avcodec.h"
00032 #include "dsputil.h"
00033
00034 typedef struct AascContext {
00035 AVCodecContext *avctx;
00036 AVFrame frame;
00037 } AascContext;
00038
00039 #define FETCH_NEXT_STREAM_BYTE() \
00040 if (stream_ptr >= buf_size) \
00041 { \
00042 av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (fetch)\n"); \
00043 break; \
00044 } \
00045 stream_byte = buf[stream_ptr++];
00046
00047 static int aasc_decode_init(AVCodecContext *avctx)
00048 {
00049 AascContext *s = avctx->priv_data;
00050
00051 s->avctx = avctx;
00052
00053 avctx->pix_fmt = PIX_FMT_BGR24;
00054 s->frame.data[0] = NULL;
00055
00056 return 0;
00057 }
00058
00059 static int aasc_decode_frame(AVCodecContext *avctx,
00060 void *data, int *data_size,
00061 const uint8_t *buf, int buf_size)
00062 {
00063 AascContext *s = avctx->priv_data;
00064 int stream_ptr = 4;
00065 unsigned char rle_code;
00066 unsigned char stream_byte;
00067 int pixel_ptr = 0;
00068 int row_dec, row_ptr;
00069 int frame_size;
00070 int i;
00071
00072 s->frame.reference = 1;
00073 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00074 if (avctx->reget_buffer(avctx, &s->frame)) {
00075 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00076 return -1;
00077 }
00078
00079 row_dec = s->frame.linesize[0];
00080 row_ptr = (s->avctx->height - 1) * row_dec;
00081 frame_size = row_dec * s->avctx->height;
00082
00083 while (row_ptr >= 0) {
00084 FETCH_NEXT_STREAM_BYTE();
00085 rle_code = stream_byte;
00086 if (rle_code == 0) {
00087
00088 FETCH_NEXT_STREAM_BYTE();
00089 if (stream_byte == 0) {
00090
00091 row_ptr -= row_dec;
00092 pixel_ptr = 0;
00093 } else if (stream_byte == 1) {
00094
00095 break;
00096 } else if (stream_byte == 2) {
00097
00098 FETCH_NEXT_STREAM_BYTE();
00099 pixel_ptr += stream_byte;
00100 FETCH_NEXT_STREAM_BYTE();
00101 row_ptr -= stream_byte * row_dec;
00102 } else {
00103
00104 if ((pixel_ptr + stream_byte > avctx->width * 3) ||
00105 (row_ptr < 0)) {
00106 av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (copy1)\n");
00107 break;
00108 }
00109
00110 rle_code = stream_byte;
00111 if (stream_ptr + rle_code > buf_size) {
00112 av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (copy2)\n");
00113 break;
00114 }
00115
00116 for (i = 0; i < rle_code; i++) {
00117 FETCH_NEXT_STREAM_BYTE();
00118 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
00119 pixel_ptr++;
00120 }
00121 if (rle_code & 1)
00122 stream_ptr++;
00123 }
00124 } else {
00125
00126 if ((pixel_ptr + rle_code > avctx->width * 3) ||
00127 (row_ptr < 0)) {
00128 av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (run1)\n");
00129 break;
00130 }
00131
00132 FETCH_NEXT_STREAM_BYTE();
00133
00134 while(rle_code--) {
00135 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
00136 pixel_ptr++;
00137 }
00138 }
00139 }
00140
00141
00142 if (stream_ptr < buf_size)
00143 av_log(s->avctx, AV_LOG_ERROR, " AASC: ended frame decode with bytes left over (%d < %d)\n",
00144 stream_ptr, buf_size);
00145
00146 *data_size = sizeof(AVFrame);
00147 *(AVFrame*)data = s->frame;
00148
00149
00150 return buf_size;
00151 }
00152
00153 static int aasc_decode_end(AVCodecContext *avctx)
00154 {
00155 AascContext *s = avctx->priv_data;
00156
00157
00158 if (s->frame.data[0])
00159 avctx->release_buffer(avctx, &s->frame);
00160
00161 return 0;
00162 }
00163
00164 AVCodec aasc_decoder = {
00165 "aasc",
00166 CODEC_TYPE_VIDEO,
00167 CODEC_ID_AASC,
00168 sizeof(AascContext),
00169 aasc_decode_init,
00170 NULL,
00171 aasc_decode_end,
00172 aasc_decode_frame,
00173 CODEC_CAP_DR1,
00174 };