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.

324 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(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) * 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. }
  174. }
  175. } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
  176. code &= 0x1F;
  177. for(i = 0; i <= code; i++) {
  178. dst[filled++] = bytestream2_get_byte(&qctx->buffer);
  179. if(filled >= width) {
  180. filled = 0;
  181. dst -= stride;
  182. height--;
  183. }
  184. }
  185. } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
  186. int skip;
  187. code &= 0x3F;
  188. /* codes 0x80 and 0x81 are actually escape codes,
  189. skip value minus constant is in the next byte */
  190. if(!code)
  191. skip = bytestream2_get_byte(&qctx->buffer) + 64;
  192. else if(code == 1)
  193. skip = bytestream2_get_byte(&qctx->buffer) + 320;
  194. else
  195. skip = code;
  196. filled += skip;
  197. while( filled >= width) {
  198. filled -= width;
  199. dst -= stride;
  200. height--;
  201. if(height < 0)
  202. break;
  203. }
  204. } else {
  205. /* zero code treated as one-pixel skip */
  206. if(code) {
  207. dst[filled++] = ctable[code & 0x7F];
  208. }
  209. else
  210. filled++;
  211. if(filled >= width) {
  212. filled = 0;
  213. dst -= stride;
  214. height--;
  215. }
  216. }
  217. }
  218. }
  219. static int decode_frame(AVCodecContext *avctx,
  220. void *data, int *got_frame,
  221. AVPacket *avpkt)
  222. {
  223. uint8_t ctable[128];
  224. QpegContext * const a = avctx->priv_data;
  225. AVFrame * const p = &a->pic;
  226. uint8_t* outdata;
  227. int delta, ret;
  228. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  229. if (avpkt->size < 0x86) {
  230. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  231. return AVERROR_INVALIDDATA;
  232. }
  233. bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
  234. if ((ret = ff_reget_buffer(avctx, p)) < 0) {
  235. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  236. return ret;
  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. if ((ret = av_frame_ref(data, &a->pic)) < 0)
  255. return ret;
  256. *got_frame = 1;
  257. return avpkt->size;
  258. }
  259. static av_cold int decode_init(AVCodecContext *avctx){
  260. QpegContext * const a = avctx->priv_data;
  261. a->avctx = avctx;
  262. avctx->pix_fmt= AV_PIX_FMT_PAL8;
  263. a->refdata = av_malloc(avctx->width * avctx->height);
  264. avcodec_get_frame_defaults(&a->pic);
  265. return 0;
  266. }
  267. static av_cold int decode_end(AVCodecContext *avctx){
  268. QpegContext * const a = avctx->priv_data;
  269. AVFrame * const p = &a->pic;
  270. av_frame_unref(p);
  271. av_free(a->refdata);
  272. return 0;
  273. }
  274. AVCodec ff_qpeg_decoder = {
  275. .name = "qpeg",
  276. .type = AVMEDIA_TYPE_VIDEO,
  277. .id = AV_CODEC_ID_QPEG,
  278. .priv_data_size = sizeof(QpegContext),
  279. .init = decode_init,
  280. .close = decode_end,
  281. .decode = decode_frame,
  282. .capabilities = CODEC_CAP_DR1,
  283. .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
  284. };