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.

336 lines
9.9KB

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