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.

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