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.

339 lines
11KB

  1. /*
  2. * QPEG codec
  3. * Copyright (c) 2004 Konstantin Shishkov
  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
  23. * QPEG codec.
  24. */
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. typedef struct QpegContext{
  28. AVCodecContext *avctx;
  29. AVFrame pic, ref;
  30. uint32_t pal[256];
  31. GetByteContext buffer;
  32. } QpegContext;
  33. static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
  34. int stride, int width, int height)
  35. {
  36. int i;
  37. int code;
  38. int c0, c1;
  39. int run, copy;
  40. int filled = 0;
  41. int rows_to_go;
  42. rows_to_go = height;
  43. height--;
  44. dst = dst + height * stride;
  45. while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
  46. code = bytestream2_get_byte(&qctx->buffer);
  47. run = copy = 0;
  48. if(code == 0xFC) /* end-of-picture code */
  49. break;
  50. if(code >= 0xF8) { /* very long run */
  51. c0 = bytestream2_get_byte(&qctx->buffer);
  52. c1 = bytestream2_get_byte(&qctx->buffer);
  53. run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
  54. } else if (code >= 0xF0) { /* long run */
  55. c0 = bytestream2_get_byte(&qctx->buffer);
  56. run = ((code & 0xF) << 8) + c0 + 2;
  57. } else if (code >= 0xE0) { /* short run */
  58. run = (code & 0x1F) + 2;
  59. } else if (code >= 0xC0) { /* very long copy */
  60. c0 = bytestream2_get_byte(&qctx->buffer);
  61. c1 = bytestream2_get_byte(&qctx->buffer);
  62. copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
  63. } else if (code >= 0x80) { /* long copy */
  64. c0 = bytestream2_get_byte(&qctx->buffer);
  65. copy = ((code & 0x7F) << 8) + c0 + 1;
  66. } else { /* short copy */
  67. copy = code + 1;
  68. }
  69. /* perform actual run or copy */
  70. if(run) {
  71. int p;
  72. p = bytestream2_get_byte(&qctx->buffer);
  73. for(i = 0; i < run; i++) {
  74. dst[filled++] = p;
  75. if (filled >= width) {
  76. filled = 0;
  77. dst -= stride;
  78. rows_to_go--;
  79. if(rows_to_go <= 0)
  80. break;
  81. }
  82. }
  83. } else {
  84. for(i = 0; i < copy; i++) {
  85. dst[filled++] = bytestream2_get_byte(&qctx->buffer);
  86. if (filled >= width) {
  87. filled = 0;
  88. dst -= stride;
  89. rows_to_go--;
  90. if(rows_to_go <= 0)
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. static const int qpeg_table_h[16] =
  98. { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
  99. static const int qpeg_table_w[16] =
  100. { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
  101. /* Decodes delta frames */
  102. static void qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
  103. int stride, int width, int height,
  104. int delta, const uint8_t *ctable,
  105. uint8_t *refdata)
  106. {
  107. int i, j;
  108. int code;
  109. int filled = 0;
  110. int orig_height;
  111. if(!refdata)
  112. refdata= dst;
  113. /* copy prev frame */
  114. for(i = 0; i < height; i++)
  115. memcpy(dst + (i * stride), refdata + (i * stride), width);
  116. orig_height = height;
  117. height--;
  118. dst = dst + height * stride;
  119. while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
  120. code = bytestream2_get_byte(&qctx->buffer);
  121. if(delta) {
  122. /* motion compensation */
  123. while(bytestream2_get_bytes_left(&qctx->buffer) > 0 && (code & 0xF0) == 0xF0) {
  124. if(delta == 1) {
  125. int me_idx;
  126. int me_w, me_h, me_x, me_y;
  127. uint8_t *me_plane;
  128. int corr, val;
  129. /* get block size by index */
  130. me_idx = code & 0xF;
  131. me_w = qpeg_table_w[me_idx];
  132. me_h = qpeg_table_h[me_idx];
  133. /* extract motion vector */
  134. corr = bytestream2_get_byte(&qctx->buffer);
  135. val = corr >> 4;
  136. if(val > 7)
  137. val -= 16;
  138. me_x = val;
  139. val = corr & 0xF;
  140. if(val > 7)
  141. val -= 16;
  142. me_y = val;
  143. /* check motion vector */
  144. if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
  145. (height - me_y - me_h < 0) || (height - me_y > orig_height) ||
  146. (filled + me_w > width) || (height - me_h < 0))
  147. av_log(NULL, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
  148. me_x, me_y, me_w, me_h, filled, height);
  149. else {
  150. /* do motion compensation */
  151. me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
  152. for(j = 0; j < me_h; j++) {
  153. for(i = 0; i < me_w; i++)
  154. dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
  155. }
  156. }
  157. }
  158. code = bytestream2_get_byte(&qctx->buffer);
  159. }
  160. }
  161. if(code == 0xE0) /* end-of-picture code */
  162. break;
  163. if(code > 0xE0) { /* run code: 0xE1..0xFF */
  164. int p;
  165. code &= 0x1F;
  166. p = bytestream2_get_byte(&qctx->buffer);
  167. for(i = 0; i <= code; i++) {
  168. dst[filled++] = p;
  169. if(filled >= width) {
  170. filled = 0;
  171. dst -= stride;
  172. height--;
  173. if(height < 0)
  174. break;
  175. }
  176. }
  177. } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
  178. code &= 0x1F;
  179. if(code + 1 > bytestream2_get_bytes_left(&qctx->buffer))
  180. break;
  181. for(i = 0; i <= code; i++) {
  182. dst[filled++] = bytestream2_get_byte(&qctx->buffer);
  183. if(filled >= width) {
  184. filled = 0;
  185. dst -= stride;
  186. height--;
  187. if(height < 0)
  188. break;
  189. }
  190. }
  191. } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
  192. int skip;
  193. code &= 0x3F;
  194. /* codes 0x80 and 0x81 are actually escape codes,
  195. skip value minus constant is in the next byte */
  196. if(!code)
  197. skip = bytestream2_get_byte(&qctx->buffer) + 64;
  198. else if(code == 1)
  199. skip = bytestream2_get_byte(&qctx->buffer) + 320;
  200. else
  201. skip = code;
  202. filled += skip;
  203. while( filled >= width) {
  204. filled -= width;
  205. dst -= stride;
  206. height--;
  207. if(height < 0)
  208. break;
  209. }
  210. } else {
  211. /* zero code treated as one-pixel skip */
  212. if(code) {
  213. dst[filled++] = ctable[code & 0x7F];
  214. }
  215. else
  216. filled++;
  217. if(filled >= width) {
  218. filled = 0;
  219. dst -= stride;
  220. height--;
  221. }
  222. }
  223. }
  224. }
  225. static int decode_frame(AVCodecContext *avctx,
  226. void *data, int *data_size,
  227. AVPacket *avpkt)
  228. {
  229. uint8_t ctable[128];
  230. QpegContext * const a = avctx->priv_data;
  231. AVFrame * p = &a->pic;
  232. AVFrame * ref= &a->ref;
  233. uint8_t* outdata;
  234. int delta;
  235. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  236. if (avpkt->size < 0x86) {
  237. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  238. return AVERROR_INVALIDDATA;
  239. }
  240. bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
  241. if(ref->data[0])
  242. avctx->release_buffer(avctx, ref);
  243. FFSWAP(AVFrame, *ref, *p);
  244. p->reference= 3;
  245. if(avctx->get_buffer(avctx, p) < 0){
  246. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  247. return -1;
  248. }
  249. outdata = a->pic.data[0];
  250. bytestream2_skip(&a->buffer, 4);
  251. bytestream2_get_buffer(&a->buffer, ctable, 128);
  252. bytestream2_skip(&a->buffer, 1);
  253. delta = bytestream2_get_byte(&a->buffer);
  254. if(delta == 0x10) {
  255. qpeg_decode_intra(a, outdata, a->pic.linesize[0], avctx->width, avctx->height);
  256. } else {
  257. qpeg_decode_inter(a, outdata, a->pic.linesize[0], avctx->width, avctx->height, delta, ctable, a->ref.data[0]);
  258. }
  259. /* make the palette available on the way out */
  260. if (pal) {
  261. a->pic.palette_has_changed = 1;
  262. memcpy(a->pal, pal, AVPALETTE_SIZE);
  263. }
  264. memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
  265. *data_size = sizeof(AVFrame);
  266. *(AVFrame*)data = a->pic;
  267. return avpkt->size;
  268. }
  269. static av_cold int decode_init(AVCodecContext *avctx){
  270. QpegContext * const a = avctx->priv_data;
  271. avcodec_get_frame_defaults(&a->pic);
  272. avcodec_get_frame_defaults(&a->ref);
  273. a->avctx = avctx;
  274. avctx->pix_fmt= PIX_FMT_PAL8;
  275. return 0;
  276. }
  277. static av_cold int decode_end(AVCodecContext *avctx){
  278. QpegContext * const a = avctx->priv_data;
  279. AVFrame * const p = &a->pic;
  280. AVFrame * const ref= &a->ref;
  281. if(p->data[0])
  282. avctx->release_buffer(avctx, p);
  283. if(ref->data[0])
  284. avctx->release_buffer(avctx, ref);
  285. return 0;
  286. }
  287. AVCodec ff_qpeg_decoder = {
  288. .name = "qpeg",
  289. .type = AVMEDIA_TYPE_VIDEO,
  290. .id = AV_CODEC_ID_QPEG,
  291. .priv_data_size = sizeof(QpegContext),
  292. .init = decode_init,
  293. .close = decode_end,
  294. .decode = decode_frame,
  295. .capabilities = CODEC_CAP_DR1,
  296. .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
  297. };