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.

216 lines
5.6KB

  1. /*
  2. * Sunplus JPEG decoder (SP5X)
  3. * Copyright (c) 2003 Alex Beregszaszi
  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 sp5xdec.c
  23. * Sunplus JPEG decoder (SP5X).
  24. */
  25. #include "avcodec.h"
  26. #include "mjpeg.h"
  27. #include "mjpegdec.h"
  28. #include "sp5x.h"
  29. static int sp5x_decode_frame(AVCodecContext *avctx,
  30. void *data, int *data_size,
  31. const uint8_t *buf, int buf_size)
  32. {
  33. #if 0
  34. MJpegDecodeContext *s = avctx->priv_data;
  35. #endif
  36. const int qscale = 5;
  37. const uint8_t *buf_ptr, *buf_end;
  38. uint8_t *recoded;
  39. int i = 0, j = 0;
  40. if (!avctx->width || !avctx->height)
  41. return -1;
  42. buf_ptr = buf;
  43. buf_end = buf + buf_size;
  44. #if 1
  45. recoded = av_mallocz(buf_size + 1024);
  46. if (!recoded)
  47. return -1;
  48. /* SOI */
  49. recoded[j++] = 0xFF;
  50. recoded[j++] = 0xD8;
  51. memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
  52. memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64);
  53. memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64);
  54. j += sizeof(sp5x_data_dqt);
  55. memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
  56. j += sizeof(sp5x_data_dht);
  57. memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
  58. AV_WB16(recoded+j+5, avctx->coded_height);
  59. AV_WB16(recoded+j+7, avctx->coded_width);
  60. j += sizeof(sp5x_data_sof);
  61. memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
  62. j += sizeof(sp5x_data_sos);
  63. if(avctx->codec_id==CODEC_ID_AMV)
  64. for (i = 2; i < buf_size-2 && j < buf_size+1024-2; i++)
  65. recoded[j++] = buf[i];
  66. else
  67. for (i = 14; i < buf_size && j < buf_size+1024-2; i++)
  68. {
  69. recoded[j++] = buf[i];
  70. if (buf[i] == 0xff)
  71. recoded[j++] = 0;
  72. }
  73. /* EOI */
  74. recoded[j++] = 0xFF;
  75. recoded[j++] = 0xD9;
  76. avctx->flags &= ~CODEC_FLAG_EMU_EDGE;
  77. i = ff_mjpeg_decode_frame(avctx, data, data_size, recoded, j);
  78. av_free(recoded);
  79. #else
  80. /* SOF */
  81. s->bits = 8;
  82. s->width = avctx->coded_width;
  83. s->height = avctx->coded_height;
  84. s->nb_components = 3;
  85. s->component_id[0] = 0;
  86. s->h_count[0] = 2;
  87. s->v_count[0] = 2;
  88. s->quant_index[0] = 0;
  89. s->component_id[1] = 1;
  90. s->h_count[1] = 1;
  91. s->v_count[1] = 1;
  92. s->quant_index[1] = 1;
  93. s->component_id[2] = 2;
  94. s->h_count[2] = 1;
  95. s->v_count[2] = 1;
  96. s->quant_index[2] = 1;
  97. s->h_max = 2;
  98. s->v_max = 2;
  99. s->qscale_table = av_mallocz((s->width+15)/16);
  100. avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420;
  101. s->interlaced = 0;
  102. s->picture.reference = 0;
  103. if (avctx->get_buffer(avctx, &s->picture) < 0)
  104. {
  105. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  106. return -1;
  107. }
  108. s->picture.pict_type = FF_I_TYPE;
  109. s->picture.key_frame = 1;
  110. for (i = 0; i < 3; i++)
  111. s->linesize[i] = s->picture.linesize[i] << s->interlaced;
  112. /* DQT */
  113. for (i = 0; i < 64; i++)
  114. {
  115. j = s->scantable.permutated[i];
  116. s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i];
  117. }
  118. s->qscale[0] = FFMAX(
  119. s->quant_matrixes[0][s->scantable.permutated[1]],
  120. s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1;
  121. for (i = 0; i < 64; i++)
  122. {
  123. j = s->scantable.permutated[i];
  124. s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i];
  125. }
  126. s->qscale[1] = FFMAX(
  127. s->quant_matrixes[1][s->scantable.permutated[1]],
  128. s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1;
  129. /* DHT */
  130. /* SOS */
  131. s->comp_index[0] = 0;
  132. s->nb_blocks[0] = s->h_count[0] * s->v_count[0];
  133. s->h_scount[0] = s->h_count[0];
  134. s->v_scount[0] = s->v_count[0];
  135. s->dc_index[0] = 0;
  136. s->ac_index[0] = 0;
  137. s->comp_index[1] = 1;
  138. s->nb_blocks[1] = s->h_count[1] * s->v_count[1];
  139. s->h_scount[1] = s->h_count[1];
  140. s->v_scount[1] = s->v_count[1];
  141. s->dc_index[1] = 1;
  142. s->ac_index[1] = 1;
  143. s->comp_index[2] = 2;
  144. s->nb_blocks[2] = s->h_count[2] * s->v_count[2];
  145. s->h_scount[2] = s->h_count[2];
  146. s->v_scount[2] = s->v_count[2];
  147. s->dc_index[2] = 1;
  148. s->ac_index[2] = 1;
  149. for (i = 0; i < 3; i++)
  150. s->last_dc[i] = 1024;
  151. s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8);
  152. s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8);
  153. init_get_bits(&s->gb, buf+14, (buf_size-14)*8);
  154. return mjpeg_decode_scan(s);
  155. #endif
  156. return i;
  157. }
  158. AVCodec sp5x_decoder = {
  159. "sp5x",
  160. CODEC_TYPE_VIDEO,
  161. CODEC_ID_SP5X,
  162. sizeof(MJpegDecodeContext),
  163. ff_mjpeg_decode_init,
  164. NULL,
  165. ff_mjpeg_decode_end,
  166. sp5x_decode_frame,
  167. CODEC_CAP_DR1,
  168. NULL,
  169. .long_name = NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"),
  170. };
  171. AVCodec amv_decoder = {
  172. "amv",
  173. CODEC_TYPE_VIDEO,
  174. CODEC_ID_AMV,
  175. sizeof(MJpegDecodeContext),
  176. ff_mjpeg_decode_init,
  177. NULL,
  178. ff_mjpeg_decode_end,
  179. sp5x_decode_frame,
  180. .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
  181. };