sp5xdec.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. 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. i = ff_mjpeg_decode_frame(avctx, data, data_size, recoded, j);
  77. av_free(recoded);
  78. #else
  79. /* SOF */
  80. s->bits = 8;
  81. s->width = avctx->coded_width;
  82. s->height = avctx->coded_height;
  83. s->nb_components = 3;
  84. s->component_id[0] = 0;
  85. s->h_count[0] = 2;
  86. s->v_count[0] = 2;
  87. s->quant_index[0] = 0;
  88. s->component_id[1] = 1;
  89. s->h_count[1] = 1;
  90. s->v_count[1] = 1;
  91. s->quant_index[1] = 1;
  92. s->component_id[2] = 2;
  93. s->h_count[2] = 1;
  94. s->v_count[2] = 1;
  95. s->quant_index[2] = 1;
  96. s->h_max = 2;
  97. s->v_max = 2;
  98. s->qscale_table = av_mallocz((s->width+15)/16);
  99. avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420;
  100. s->interlaced = 0;
  101. s->picture.reference = 0;
  102. if (avctx->get_buffer(avctx, &s->picture) < 0)
  103. {
  104. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  105. return -1;
  106. }
  107. s->picture.pict_type = FF_I_TYPE;
  108. s->picture.key_frame = 1;
  109. for (i = 0; i < 3; i++)
  110. s->linesize[i] = s->picture.linesize[i] << s->interlaced;
  111. /* DQT */
  112. for (i = 0; i < 64; i++)
  113. {
  114. j = s->scantable.permutated[i];
  115. s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i];
  116. }
  117. s->qscale[0] = FFMAX(
  118. s->quant_matrixes[0][s->scantable.permutated[1]],
  119. s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1;
  120. for (i = 0; i < 64; i++)
  121. {
  122. j = s->scantable.permutated[i];
  123. s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i];
  124. }
  125. s->qscale[1] = FFMAX(
  126. s->quant_matrixes[1][s->scantable.permutated[1]],
  127. s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1;
  128. /* DHT */
  129. /* SOS */
  130. s->comp_index[0] = 0;
  131. s->nb_blocks[0] = s->h_count[0] * s->v_count[0];
  132. s->h_scount[0] = s->h_count[0];
  133. s->v_scount[0] = s->v_count[0];
  134. s->dc_index[0] = 0;
  135. s->ac_index[0] = 0;
  136. s->comp_index[1] = 1;
  137. s->nb_blocks[1] = s->h_count[1] * s->v_count[1];
  138. s->h_scount[1] = s->h_count[1];
  139. s->v_scount[1] = s->v_count[1];
  140. s->dc_index[1] = 1;
  141. s->ac_index[1] = 1;
  142. s->comp_index[2] = 2;
  143. s->nb_blocks[2] = s->h_count[2] * s->v_count[2];
  144. s->h_scount[2] = s->h_count[2];
  145. s->v_scount[2] = s->v_count[2];
  146. s->dc_index[2] = 1;
  147. s->ac_index[2] = 1;
  148. for (i = 0; i < 3; i++)
  149. s->last_dc[i] = 1024;
  150. s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8);
  151. s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8);
  152. init_get_bits(&s->gb, buf+14, (buf_size-14)*8);
  153. return mjpeg_decode_scan(s);
  154. #endif
  155. return i;
  156. }
  157. AVCodec sp5x_decoder = {
  158. "sp5x",
  159. CODEC_TYPE_VIDEO,
  160. CODEC_ID_SP5X,
  161. sizeof(MJpegDecodeContext),
  162. ff_mjpeg_decode_init,
  163. NULL,
  164. ff_mjpeg_decode_end,
  165. sp5x_decode_frame,
  166. CODEC_CAP_DR1,
  167. NULL,
  168. .long_name = NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"),
  169. };
  170. AVCodec amv_decoder = {
  171. "amv",
  172. CODEC_TYPE_VIDEO,
  173. CODEC_ID_AMV,
  174. sizeof(MJpegDecodeContext),
  175. ff_mjpeg_decode_init,
  176. NULL,
  177. ff_mjpeg_decode_end,
  178. sp5x_decode_frame,
  179. .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
  180. };