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.

332 lines
10KB

  1. /*
  2. * QPEG codec
  3. * Copyright (c) 2004 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #include "internal.h"
  28. typedef struct QpegContext{
  29. AVCodecContext *avctx;
  30. AVFrame *pic;
  31. uint8_t *refdata;
  32. uint32_t pal[256];
  33. GetByteContext buffer;
  34. } QpegContext;
  35. static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
  36. int stride, int width, int height)
  37. {
  38. int i;
  39. int code;
  40. int c0, c1;
  41. int run, copy;
  42. int filled = 0;
  43. int rows_to_go;
  44. rows_to_go = height;
  45. height--;
  46. dst = dst + height * stride;
  47. while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
  48. code = bytestream2_get_byte(&qctx->buffer);
  49. run = copy = 0;
  50. if(code == 0xFC) /* end-of-picture code */
  51. break;
  52. if(code >= 0xF8) { /* very long run */
  53. c0 = bytestream2_get_byte(&qctx->buffer);
  54. c1 = bytestream2_get_byte(&qctx->buffer);
  55. run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
  56. } else if (code >= 0xF0) { /* long run */
  57. c0 = bytestream2_get_byte(&qctx->buffer);
  58. run = ((code & 0xF) << 8) + c0 + 2;
  59. } else if (code >= 0xE0) { /* short run */
  60. run = (code & 0x1F) + 2;
  61. } else if (code >= 0xC0) { /* very long copy */
  62. c0 = bytestream2_get_byte(&qctx->buffer);
  63. c1 = bytestream2_get_byte(&qctx->buffer);
  64. copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
  65. } else if (code >= 0x80) { /* long copy */
  66. c0 = bytestream2_get_byte(&qctx->buffer);
  67. copy = ((code & 0x7F) << 8) + c0 + 1;
  68. } else { /* short copy */
  69. copy = code + 1;
  70. }
  71. /* perform actual run or copy */
  72. if(run) {
  73. int p;
  74. p = bytestream2_get_byte(&qctx->buffer);
  75. for(i = 0; i < run; i++) {
  76. dst[filled++] = p;
  77. if (filled >= width) {
  78. filled = 0;
  79. dst -= stride;
  80. rows_to_go--;
  81. if(rows_to_go <= 0)
  82. break;
  83. }
  84. }
  85. } else {
  86. for(i = 0; i < copy; i++) {
  87. dst[filled++] = bytestream2_get_byte(&qctx->buffer);
  88. if (filled >= width) {
  89. filled = 0;
  90. dst -= stride;
  91. rows_to_go--;
  92. if(rows_to_go <= 0)
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. static const int qpeg_table_h[16] =
  100. { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
  101. static const int qpeg_table_w[16] =
  102. { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
  103. /* Decodes delta frames */
  104. static void qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
  105. int stride, int width, int height,
  106. int delta, const uint8_t *ctable,
  107. uint8_t *refdata)
  108. {
  109. int i, j;
  110. int code;
  111. int filled = 0;
  112. int orig_height;
  113. /* copy prev frame */
  114. for(i = 0; i < height; i++)
  115. memcpy(refdata + (i * width), dst + (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((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(qctx->avctx, 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) * width;
  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 * width)];
  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. for(i = 0; i <= code; i++) {
  180. dst[filled++] = bytestream2_get_byte(&qctx->buffer);
  181. if(filled >= width) {
  182. filled = 0;
  183. dst -= stride;
  184. height--;
  185. if (height < 0)
  186. break;
  187. }
  188. }
  189. } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
  190. int skip;
  191. code &= 0x3F;
  192. /* codes 0x80 and 0x81 are actually escape codes,
  193. skip value minus constant is in the next byte */
  194. if(!code)
  195. skip = bytestream2_get_byte(&qctx->buffer) + 64;
  196. else if(code == 1)
  197. skip = bytestream2_get_byte(&qctx->buffer) + 320;
  198. else
  199. skip = code;
  200. filled += skip;
  201. while( filled >= width) {
  202. filled -= width;
  203. dst -= stride;
  204. height--;
  205. if(height < 0)
  206. break;
  207. }
  208. } else {
  209. /* zero code treated as one-pixel skip */
  210. if(code) {
  211. dst[filled++] = ctable[code & 0x7F];
  212. }
  213. else
  214. filled++;
  215. if(filled >= width) {
  216. filled = 0;
  217. dst -= stride;
  218. height--;
  219. }
  220. }
  221. }
  222. }
  223. static int decode_frame(AVCodecContext *avctx,
  224. void *data, int *got_frame,
  225. AVPacket *avpkt)
  226. {
  227. uint8_t ctable[128];
  228. QpegContext * const a = avctx->priv_data;
  229. AVFrame * const p = a->pic;
  230. uint8_t* outdata;
  231. int delta, ret;
  232. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  233. if (avpkt->size < 0x86) {
  234. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  235. return AVERROR_INVALIDDATA;
  236. }
  237. bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
  238. if ((ret = ff_reget_buffer(avctx, p)) < 0) {
  239. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  240. return ret;
  241. }
  242. outdata = p->data[0];
  243. bytestream2_skip(&a->buffer, 4);
  244. bytestream2_get_buffer(&a->buffer, ctable, 128);
  245. bytestream2_skip(&a->buffer, 1);
  246. delta = bytestream2_get_byte(&a->buffer);
  247. if(delta == 0x10) {
  248. qpeg_decode_intra(a, outdata, p->linesize[0], avctx->width, avctx->height);
  249. } else {
  250. qpeg_decode_inter(a, outdata, p->linesize[0], avctx->width, avctx->height, delta, ctable, a->refdata);
  251. }
  252. /* make the palette available on the way out */
  253. if (pal) {
  254. p->palette_has_changed = 1;
  255. memcpy(a->pal, pal, AVPALETTE_SIZE);
  256. }
  257. memcpy(p->data[1], a->pal, AVPALETTE_SIZE);
  258. if ((ret = av_frame_ref(data, p)) < 0)
  259. return ret;
  260. *got_frame = 1;
  261. return avpkt->size;
  262. }
  263. static av_cold int decode_end(AVCodecContext *avctx)
  264. {
  265. QpegContext * const a = avctx->priv_data;
  266. av_frame_free(&a->pic);
  267. av_free(a->refdata);
  268. return 0;
  269. }
  270. static av_cold int decode_init(AVCodecContext *avctx){
  271. QpegContext * const a = avctx->priv_data;
  272. a->avctx = avctx;
  273. avctx->pix_fmt= AV_PIX_FMT_PAL8;
  274. a->refdata = av_malloc(avctx->width * avctx->height);
  275. a->pic = av_frame_alloc();
  276. if (!a->pic) {
  277. decode_end(avctx);
  278. return AVERROR(ENOMEM);
  279. }
  280. return 0;
  281. }
  282. AVCodec ff_qpeg_decoder = {
  283. .name = "qpeg",
  284. .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
  285. .type = AVMEDIA_TYPE_VIDEO,
  286. .id = AV_CODEC_ID_QPEG,
  287. .priv_data_size = sizeof(QpegContext),
  288. .init = decode_init,
  289. .close = decode_end,
  290. .decode = decode_frame,
  291. .capabilities = AV_CODEC_CAP_DR1,
  292. };