qpeg.c 9.4 KB

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