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.

355 lines
11KB

  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. #include "bytestream.h"
  27. #include "internal.h"
  28. typedef struct QpegContext{
  29. AVCodecContext *avctx;
  30. AVFrame pic, ref;
  31. uint32_t pal[256];
  32. GetByteContext buffer;
  33. } QpegContext;
  34. static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
  35. int stride, int width, int height)
  36. {
  37. int i;
  38. int code;
  39. int c0, c1;
  40. int run, copy;
  41. int filled = 0;
  42. int rows_to_go;
  43. rows_to_go = height;
  44. height--;
  45. dst = dst + height * stride;
  46. while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
  47. code = bytestream2_get_byte(&qctx->buffer);
  48. run = copy = 0;
  49. if(code == 0xFC) /* end-of-picture code */
  50. break;
  51. if(code >= 0xF8) { /* very long run */
  52. c0 = bytestream2_get_byte(&qctx->buffer);
  53. c1 = bytestream2_get_byte(&qctx->buffer);
  54. run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
  55. } else if (code >= 0xF0) { /* long run */
  56. c0 = bytestream2_get_byte(&qctx->buffer);
  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 = bytestream2_get_byte(&qctx->buffer);
  62. c1 = bytestream2_get_byte(&qctx->buffer);
  63. copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
  64. } else if (code >= 0x80) { /* long copy */
  65. c0 = bytestream2_get_byte(&qctx->buffer);
  66. copy = ((code & 0x7F) << 8) + c0 + 1;
  67. } else { /* short copy */
  68. copy = code + 1;
  69. }
  70. /* perform actual run or copy */
  71. if(run) {
  72. int p;
  73. p = bytestream2_get_byte(&qctx->buffer);
  74. for(i = 0; i < run; i++) {
  75. dst[filled++] = p;
  76. if (filled >= width) {
  77. filled = 0;
  78. dst -= stride;
  79. rows_to_go--;
  80. if(rows_to_go <= 0)
  81. break;
  82. }
  83. }
  84. } else {
  85. for(i = 0; i < copy; i++) {
  86. dst[filled++] = bytestream2_get_byte(&qctx->buffer);
  87. if (filled >= width) {
  88. filled = 0;
  89. dst -= stride;
  90. rows_to_go--;
  91. if(rows_to_go <= 0)
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. static const int qpeg_table_h[16] =
  99. { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
  100. static const int qpeg_table_w[16] =
  101. { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
  102. /* Decodes delta frames */
  103. static void qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
  104. int stride, int width, int height,
  105. int delta, const uint8_t *ctable,
  106. uint8_t *refdata)
  107. {
  108. int i, j;
  109. int code;
  110. int filled = 0;
  111. int orig_height;
  112. if(!refdata)
  113. refdata= dst;
  114. /* copy prev frame */
  115. for(i = 0; i < height; i++)
  116. memcpy(dst + (i * stride), refdata + (i * stride), width);
  117. orig_height = height;
  118. height--;
  119. dst = dst + height * stride;
  120. while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
  121. code = bytestream2_get_byte(&qctx->buffer);
  122. if(delta) {
  123. /* motion compensation */
  124. while(bytestream2_get_bytes_left(&qctx->buffer) > 0 && (code & 0xF0) == 0xF0) {
  125. if(delta == 1) {
  126. int me_idx;
  127. int me_w, me_h, me_x, me_y;
  128. uint8_t *me_plane;
  129. int corr, val;
  130. /* get block size by index */
  131. me_idx = code & 0xF;
  132. me_w = qpeg_table_w[me_idx];
  133. me_h = qpeg_table_h[me_idx];
  134. /* extract motion vector */
  135. corr = bytestream2_get_byte(&qctx->buffer);
  136. val = corr >> 4;
  137. if(val > 7)
  138. val -= 16;
  139. me_x = val;
  140. val = corr & 0xF;
  141. if(val > 7)
  142. val -= 16;
  143. me_y = val;
  144. /* check motion vector */
  145. if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
  146. (height - me_y - me_h < 0) || (height - me_y > orig_height) ||
  147. (filled + me_w > width) || (height - me_h < 0))
  148. av_log(NULL, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
  149. me_x, me_y, me_w, me_h, filled, height);
  150. else {
  151. /* do motion compensation */
  152. me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
  153. for(j = 0; j < me_h; j++) {
  154. for(i = 0; i < me_w; i++)
  155. dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
  156. }
  157. }
  158. }
  159. code = bytestream2_get_byte(&qctx->buffer);
  160. }
  161. }
  162. if(code == 0xE0) /* end-of-picture code */
  163. break;
  164. if(code > 0xE0) { /* run code: 0xE1..0xFF */
  165. int p;
  166. code &= 0x1F;
  167. p = bytestream2_get_byte(&qctx->buffer);
  168. for(i = 0; i <= code; i++) {
  169. dst[filled++] = p;
  170. if(filled >= width) {
  171. filled = 0;
  172. dst -= stride;
  173. height--;
  174. if(height < 0)
  175. break;
  176. }
  177. }
  178. } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
  179. code &= 0x1F;
  180. if(code + 1 > bytestream2_get_bytes_left(&qctx->buffer))
  181. break;
  182. for(i = 0; i <= code; i++) {
  183. dst[filled++] = bytestream2_get_byte(&qctx->buffer);
  184. if(filled >= width) {
  185. filled = 0;
  186. dst -= stride;
  187. height--;
  188. if(height < 0)
  189. break;
  190. }
  191. }
  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 = bytestream2_get_byte(&qctx->buffer) + 64;
  199. else if(code == 1)
  200. skip = bytestream2_get_byte(&qctx->buffer) + 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. }
  216. else
  217. filled++;
  218. if(filled >= width) {
  219. filled = 0;
  220. dst -= stride;
  221. height--;
  222. }
  223. }
  224. }
  225. }
  226. static int decode_frame(AVCodecContext *avctx,
  227. void *data, int *got_frame,
  228. AVPacket *avpkt)
  229. {
  230. uint8_t ctable[128];
  231. QpegContext * const a = avctx->priv_data;
  232. AVFrame * p = &a->pic;
  233. AVFrame * ref= &a->ref;
  234. uint8_t* outdata;
  235. int delta, ret;
  236. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  237. if (avpkt->size < 0x86) {
  238. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  239. return AVERROR_INVALIDDATA;
  240. }
  241. bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
  242. if(ref->data[0])
  243. avctx->release_buffer(avctx, ref);
  244. FFSWAP(AVFrame, *ref, *p);
  245. p->reference= 3;
  246. if ((ret = ff_get_buffer(avctx, p)) < 0) {
  247. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  248. return ret;
  249. }
  250. outdata = a->pic.data[0];
  251. bytestream2_skip(&a->buffer, 4);
  252. bytestream2_get_buffer(&a->buffer, ctable, 128);
  253. bytestream2_skip(&a->buffer, 1);
  254. delta = bytestream2_get_byte(&a->buffer);
  255. if(delta == 0x10) {
  256. qpeg_decode_intra(a, outdata, a->pic.linesize[0], avctx->width, avctx->height);
  257. } else {
  258. qpeg_decode_inter(a, outdata, a->pic.linesize[0], avctx->width, avctx->height, delta, ctable, a->ref.data[0]);
  259. }
  260. /* make the palette available on the way out */
  261. if (pal) {
  262. a->pic.palette_has_changed = 1;
  263. memcpy(a->pal, pal, AVPALETTE_SIZE);
  264. }
  265. memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
  266. *got_frame = 1;
  267. *(AVFrame*)data = a->pic;
  268. return avpkt->size;
  269. }
  270. static void decode_flush(AVCodecContext *avctx){
  271. QpegContext * const a = avctx->priv_data;
  272. int i, pal_size;
  273. const uint8_t *pal_src;
  274. pal_size = FFMIN(1024U, avctx->extradata_size);
  275. pal_src = avctx->extradata + avctx->extradata_size - pal_size;
  276. for (i=0; i<pal_size/4; i++)
  277. a->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
  278. }
  279. static av_cold int decode_init(AVCodecContext *avctx){
  280. QpegContext * const a = avctx->priv_data;
  281. avcodec_get_frame_defaults(&a->pic);
  282. avcodec_get_frame_defaults(&a->ref);
  283. a->avctx = avctx;
  284. avctx->pix_fmt= AV_PIX_FMT_PAL8;
  285. decode_flush(avctx);
  286. return 0;
  287. }
  288. static av_cold int decode_end(AVCodecContext *avctx){
  289. QpegContext * const a = avctx->priv_data;
  290. AVFrame * const p = &a->pic;
  291. AVFrame * const ref= &a->ref;
  292. if(p->data[0])
  293. avctx->release_buffer(avctx, p);
  294. if(ref->data[0])
  295. avctx->release_buffer(avctx, ref);
  296. return 0;
  297. }
  298. AVCodec ff_qpeg_decoder = {
  299. .name = "qpeg",
  300. .type = AVMEDIA_TYPE_VIDEO,
  301. .id = AV_CODEC_ID_QPEG,
  302. .priv_data_size = sizeof(QpegContext),
  303. .init = decode_init,
  304. .close = decode_end,
  305. .decode = decode_frame,
  306. .flush = decode_flush,
  307. .capabilities = CODEC_CAP_DR1,
  308. .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
  309. };