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.

325 lines
9.4KB

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