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.

330 lines
9.8KB

  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
  23. * QPEG codec.
  24. */
  25. #include "avcodec.h"
  26. typedef struct QpegContext{
  27. AVCodecContext *avctx;
  28. AVFrame pic, ref;
  29. uint32_t pal[256];
  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. if(!refdata)
  116. refdata= dst;
  117. /* copy prev frame */
  118. for(i = 0; i < height; i++)
  119. memcpy(dst + (i * stride), refdata + (i * stride), width);
  120. orig_height = height;
  121. height--;
  122. dst = dst + height * stride;
  123. while((size > 0) && (height >= 0)) {
  124. code = *src++;
  125. size--;
  126. if(delta) {
  127. /* motion compensation */
  128. while((code & 0xF0) == 0xF0) {
  129. if(delta == 1) {
  130. int me_idx;
  131. int me_w, me_h, me_x, me_y;
  132. uint8_t *me_plane;
  133. int corr, val;
  134. /* get block size by index */
  135. me_idx = code & 0xF;
  136. me_w = qpeg_table_w[me_idx];
  137. me_h = qpeg_table_h[me_idx];
  138. /* extract motion vector */
  139. corr = *src++;
  140. size--;
  141. val = corr >> 4;
  142. if(val > 7)
  143. val -= 16;
  144. me_x = val;
  145. val = corr & 0xF;
  146. if(val > 7)
  147. val -= 16;
  148. me_y = val;
  149. /* check motion vector */
  150. if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
  151. (height - me_y - me_h < 0) || (height - me_y > orig_height) ||
  152. (filled + me_w > width) || (height - me_h < 0))
  153. av_log(NULL, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
  154. me_x, me_y, me_w, me_h, filled, height);
  155. else {
  156. /* do motion compensation */
  157. me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
  158. for(j = 0; j < me_h; j++) {
  159. for(i = 0; i < me_w; i++)
  160. dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
  161. }
  162. }
  163. }
  164. code = *src++;
  165. size--;
  166. }
  167. }
  168. if(code == 0xE0) /* end-of-picture code */
  169. break;
  170. if(code > 0xE0) { /* run code: 0xE1..0xFF */
  171. int p;
  172. code &= 0x1F;
  173. p = *src++;
  174. size--;
  175. for(i = 0; i <= code; i++) {
  176. dst[filled++] = p;
  177. if(filled >= width) {
  178. filled = 0;
  179. dst -= stride;
  180. height--;
  181. }
  182. }
  183. } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
  184. code &= 0x1F;
  185. for(i = 0; i <= code; i++) {
  186. dst[filled++] = *src++;
  187. if(filled >= width) {
  188. filled = 0;
  189. dst -= stride;
  190. height--;
  191. }
  192. }
  193. size -= code + 1;
  194. } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
  195. int skip;
  196. code &= 0x3F;
  197. /* codes 0x80 and 0x81 are actually escape codes,
  198. skip value minus constant is in the next byte */
  199. if(!code)
  200. skip = (*src++) + 64;
  201. else if(code == 1)
  202. skip = (*src++) + 320;
  203. else
  204. skip = code;
  205. filled += skip;
  206. while( filled >= width) {
  207. filled -= width;
  208. dst -= stride;
  209. height--;
  210. if(height < 0)
  211. break;
  212. }
  213. } else {
  214. /* zero code treated as one-pixel skip */
  215. if(code)
  216. dst[filled++] = ctable[code & 0x7F];
  217. else
  218. filled++;
  219. if(filled >= width) {
  220. filled = 0;
  221. dst -= stride;
  222. height--;
  223. }
  224. }
  225. }
  226. }
  227. static int decode_frame(AVCodecContext *avctx,
  228. void *data, int *data_size,
  229. AVPacket *avpkt)
  230. {
  231. const uint8_t *buf = avpkt->data;
  232. int buf_size = avpkt->size;
  233. QpegContext * const a = avctx->priv_data;
  234. AVFrame * p= (AVFrame*)&a->pic;
  235. AVFrame * ref= (AVFrame*)&a->ref;
  236. uint8_t* outdata;
  237. int delta;
  238. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  239. if(ref->data[0])
  240. avctx->release_buffer(avctx, ref);
  241. FFSWAP(AVFrame, *ref, *p);
  242. p->reference= 3;
  243. if(avctx->get_buffer(avctx, p) < 0){
  244. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  245. return -1;
  246. }
  247. outdata = a->pic.data[0];
  248. if(buf[0x85] == 0x10) {
  249. qpeg_decode_intra(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height);
  250. } else {
  251. delta = buf[0x85];
  252. qpeg_decode_inter(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height, delta, buf + 4, a->ref.data[0]);
  253. }
  254. /* make the palette available on the way out */
  255. if (pal) {
  256. a->pic.palette_has_changed = 1;
  257. memcpy(a->pal, pal, AVPALETTE_SIZE);
  258. }
  259. memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
  260. *data_size = sizeof(AVFrame);
  261. *(AVFrame*)data = a->pic;
  262. return buf_size;
  263. }
  264. static av_cold int decode_init(AVCodecContext *avctx){
  265. QpegContext * const a = avctx->priv_data;
  266. avcodec_get_frame_defaults(&a->pic);
  267. avcodec_get_frame_defaults(&a->ref);
  268. a->avctx = avctx;
  269. avctx->pix_fmt= PIX_FMT_PAL8;
  270. return 0;
  271. }
  272. static av_cold int decode_end(AVCodecContext *avctx){
  273. QpegContext * const a = avctx->priv_data;
  274. AVFrame * const p= (AVFrame*)&a->pic;
  275. AVFrame * const ref= (AVFrame*)&a->ref;
  276. if(p->data[0])
  277. avctx->release_buffer(avctx, p);
  278. if(ref->data[0])
  279. avctx->release_buffer(avctx, ref);
  280. return 0;
  281. }
  282. AVCodec ff_qpeg_decoder = {
  283. .name = "qpeg",
  284. .type = AVMEDIA_TYPE_VIDEO,
  285. .id = CODEC_ID_QPEG,
  286. .priv_data_size = sizeof(QpegContext),
  287. .init = decode_init,
  288. .close = decode_end,
  289. .decode = decode_frame,
  290. .capabilities = CODEC_CAP_DR1,
  291. .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
  292. };