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.

197 lines
5.1KB

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