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.

222 lines
5.8KB

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