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.

372 lines
12KB

  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 *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. int step = FFMIN(run - i, width - filled);
  76. memset(dst+filled, p, step);
  77. filled += step;
  78. i += step - 1;
  79. if (filled >= width) {
  80. filled = 0;
  81. dst -= stride;
  82. rows_to_go--;
  83. while (run - i > width && rows_to_go > 0) {
  84. memset(dst, p, width);
  85. dst -= stride;
  86. rows_to_go--;
  87. i += width;
  88. }
  89. if(rows_to_go <= 0)
  90. break;
  91. }
  92. }
  93. } else {
  94. if (bytestream2_get_bytes_left(&qctx->buffer) < copy)
  95. copy = bytestream2_get_bytes_left(&qctx->buffer);
  96. for(i = 0; i < copy; i++) {
  97. dst[filled++] = bytestream2_get_byte(&qctx->buffer);
  98. if (filled >= width) {
  99. filled = 0;
  100. dst -= stride;
  101. rows_to_go--;
  102. if(rows_to_go <= 0)
  103. break;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. static const int qpeg_table_h[16] =
  110. { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
  111. static const int qpeg_table_w[16] =
  112. { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
  113. /* Decodes delta frames */
  114. static void av_noinline qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
  115. int stride, int width, int height,
  116. int delta, const uint8_t *ctable,
  117. uint8_t *refdata)
  118. {
  119. int i, j;
  120. int code;
  121. int filled = 0;
  122. int orig_height;
  123. if (refdata) {
  124. /* copy prev frame */
  125. for (i = 0; i < height; i++)
  126. memcpy(dst + (i * stride), refdata + (i * stride), width);
  127. } else {
  128. refdata = dst;
  129. }
  130. orig_height = height;
  131. height--;
  132. dst = dst + height * stride;
  133. while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
  134. code = bytestream2_get_byte(&qctx->buffer);
  135. if(delta) {
  136. /* motion compensation */
  137. while(bytestream2_get_bytes_left(&qctx->buffer) > 0 && (code & 0xF0) == 0xF0) {
  138. if(delta == 1) {
  139. int me_idx;
  140. int me_w, me_h, me_x, me_y;
  141. uint8_t *me_plane;
  142. int corr, val;
  143. /* get block size by index */
  144. me_idx = code & 0xF;
  145. me_w = qpeg_table_w[me_idx];
  146. me_h = qpeg_table_h[me_idx];
  147. /* extract motion vector */
  148. corr = bytestream2_get_byte(&qctx->buffer);
  149. val = corr >> 4;
  150. if(val > 7)
  151. val -= 16;
  152. me_x = val;
  153. val = corr & 0xF;
  154. if(val > 7)
  155. val -= 16;
  156. me_y = val;
  157. /* check motion vector */
  158. if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
  159. (height - me_y - me_h < 0) || (height - me_y >= orig_height) ||
  160. (filled + me_w > width) || (height - me_h < 0))
  161. av_log(qctx->avctx, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
  162. me_x, me_y, me_w, me_h, filled, height);
  163. else {
  164. /* do motion compensation */
  165. me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
  166. for(j = 0; j < me_h; j++) {
  167. for(i = 0; i < me_w; i++)
  168. dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
  169. }
  170. }
  171. }
  172. code = bytestream2_get_byte(&qctx->buffer);
  173. }
  174. }
  175. if(code == 0xE0) /* end-of-picture code */
  176. break;
  177. if(code > 0xE0) { /* run code: 0xE1..0xFF */
  178. int p;
  179. code &= 0x1F;
  180. p = bytestream2_get_byte(&qctx->buffer);
  181. for(i = 0; i <= code; i++) {
  182. dst[filled++] = p;
  183. if(filled >= width) {
  184. filled = 0;
  185. dst -= stride;
  186. height--;
  187. if (height < 0)
  188. break;
  189. }
  190. }
  191. } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
  192. code &= 0x1F;
  193. if(code + 1 > bytestream2_get_bytes_left(&qctx->buffer))
  194. break;
  195. for(i = 0; i <= code; i++) {
  196. dst[filled++] = bytestream2_get_byte(&qctx->buffer);
  197. if(filled >= width) {
  198. filled = 0;
  199. dst -= stride;
  200. height--;
  201. if (height < 0)
  202. break;
  203. }
  204. }
  205. } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
  206. int skip;
  207. code &= 0x3F;
  208. /* codes 0x80 and 0x81 are actually escape codes,
  209. skip value minus constant is in the next byte */
  210. if(!code)
  211. skip = bytestream2_get_byte(&qctx->buffer) + 64;
  212. else if(code == 1)
  213. skip = bytestream2_get_byte(&qctx->buffer) + 320;
  214. else
  215. skip = code;
  216. filled += skip;
  217. while( filled >= width) {
  218. filled -= width;
  219. dst -= stride;
  220. height--;
  221. if(height < 0)
  222. break;
  223. }
  224. } else {
  225. /* zero code treated as one-pixel skip */
  226. if(code) {
  227. dst[filled++] = ctable[code & 0x7F];
  228. }
  229. else
  230. filled++;
  231. if(filled >= width) {
  232. filled = 0;
  233. dst -= stride;
  234. height--;
  235. }
  236. }
  237. }
  238. }
  239. static int decode_frame(AVCodecContext *avctx,
  240. void *data, int *got_frame,
  241. AVPacket *avpkt)
  242. {
  243. uint8_t ctable[128];
  244. QpegContext * const a = avctx->priv_data;
  245. AVFrame * const p = data;
  246. AVFrame * const ref = a->ref;
  247. uint8_t* outdata;
  248. int delta, intra, ret;
  249. int pal_size;
  250. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
  251. if (avpkt->size < 0x86) {
  252. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  253. return AVERROR_INVALIDDATA;
  254. }
  255. bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
  256. if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0)
  257. return ret;
  258. outdata = p->data[0];
  259. bytestream2_skip(&a->buffer, 4);
  260. bytestream2_get_buffer(&a->buffer, ctable, 128);
  261. bytestream2_skip(&a->buffer, 1);
  262. delta = bytestream2_get_byte(&a->buffer);
  263. intra = delta == 0x10;
  264. if (intra) {
  265. qpeg_decode_intra(a, outdata, p->linesize[0], avctx->width, avctx->height);
  266. } else {
  267. qpeg_decode_inter(a, outdata, p->linesize[0], avctx->width, avctx->height, delta, ctable, ref->data[0]);
  268. }
  269. /* make the palette available on the way out */
  270. if (pal && pal_size == AVPALETTE_SIZE) {
  271. p->palette_has_changed = 1;
  272. memcpy(a->pal, pal, AVPALETTE_SIZE);
  273. } else if (pal) {
  274. av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", pal_size);
  275. }
  276. memcpy(p->data[1], a->pal, AVPALETTE_SIZE);
  277. av_frame_unref(ref);
  278. if ((ret = av_frame_ref(ref, p)) < 0)
  279. return ret;
  280. p->key_frame = intra;
  281. p->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  282. *got_frame = 1;
  283. return avpkt->size;
  284. }
  285. static void decode_flush(AVCodecContext *avctx){
  286. QpegContext * const a = avctx->priv_data;
  287. int i, pal_size;
  288. const uint8_t *pal_src;
  289. av_frame_unref(a->ref);
  290. pal_size = FFMIN(1024U, avctx->extradata_size);
  291. pal_src = avctx->extradata + avctx->extradata_size - pal_size;
  292. for (i=0; i<pal_size/4; i++)
  293. a->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
  294. }
  295. static av_cold int decode_end(AVCodecContext *avctx)
  296. {
  297. QpegContext * const a = avctx->priv_data;
  298. av_frame_free(&a->ref);
  299. return 0;
  300. }
  301. static av_cold int decode_init(AVCodecContext *avctx){
  302. QpegContext * const a = avctx->priv_data;
  303. a->avctx = avctx;
  304. avctx->pix_fmt= AV_PIX_FMT_PAL8;
  305. a->ref = av_frame_alloc();
  306. if (!a->ref)
  307. return AVERROR(ENOMEM);
  308. decode_flush(avctx);
  309. return 0;
  310. }
  311. AVCodec ff_qpeg_decoder = {
  312. .name = "qpeg",
  313. .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
  314. .type = AVMEDIA_TYPE_VIDEO,
  315. .id = AV_CODEC_ID_QPEG,
  316. .priv_data_size = sizeof(QpegContext),
  317. .init = decode_init,
  318. .close = decode_end,
  319. .decode = decode_frame,
  320. .flush = decode_flush,
  321. .capabilities = AV_CODEC_CAP_DR1,
  322. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  323. FF_CODEC_CAP_INIT_CLEANUP,
  324. };