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.

126 lines
3.2KB

  1. /*
  2. * Sunplus JPEG decoder (SP5X)
  3. * Copyright (c) 2003 Alex Beregszaszi
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. * 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. const int qscale = 5;
  37. const uint8_t *buf_ptr;
  38. uint8_t *recoded;
  39. int i = 0, j = 0;
  40. if (!avctx->width || !avctx->height)
  41. return -1;
  42. buf_ptr = buf;
  43. recoded = av_mallocz(buf_size + 1024);
  44. if (!recoded)
  45. return -1;
  46. /* SOI */
  47. recoded[j++] = 0xFF;
  48. recoded[j++] = 0xD8;
  49. memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
  50. memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64);
  51. memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64);
  52. j += sizeof(sp5x_data_dqt);
  53. memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
  54. j += sizeof(sp5x_data_dht);
  55. memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
  56. AV_WB16(recoded+j+5, avctx->coded_height);
  57. AV_WB16(recoded+j+7, avctx->coded_width);
  58. j += sizeof(sp5x_data_sof);
  59. memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
  60. j += sizeof(sp5x_data_sos);
  61. if(avctx->codec_id==CODEC_ID_AMV)
  62. for (i = 2; i < buf_size-2 && j < buf_size+1024-2; i++)
  63. recoded[j++] = buf[i];
  64. else
  65. for (i = 14; i < buf_size && j < buf_size+1024-2; i++)
  66. {
  67. recoded[j++] = buf[i];
  68. if (buf[i] == 0xff)
  69. recoded[j++] = 0;
  70. }
  71. /* EOI */
  72. recoded[j++] = 0xFF;
  73. recoded[j++] = 0xD9;
  74. av_init_packet(&avpkt_recoded);
  75. avpkt_recoded.data = recoded;
  76. avpkt_recoded.size = j;
  77. i = ff_mjpeg_decode_frame(avctx, data, data_size, &avpkt_recoded);
  78. av_free(recoded);
  79. return i;
  80. }
  81. AVCodec ff_sp5x_decoder = {
  82. "sp5x",
  83. AVMEDIA_TYPE_VIDEO,
  84. CODEC_ID_SP5X,
  85. sizeof(MJpegDecodeContext),
  86. ff_mjpeg_decode_init,
  87. NULL,
  88. ff_mjpeg_decode_end,
  89. sp5x_decode_frame,
  90. CODEC_CAP_DR1,
  91. NULL,
  92. .max_lowres = 5,
  93. .long_name = NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"),
  94. };
  95. AVCodec ff_amv_decoder = {
  96. "amv",
  97. AVMEDIA_TYPE_VIDEO,
  98. CODEC_ID_AMV,
  99. sizeof(MJpegDecodeContext),
  100. ff_mjpeg_decode_init,
  101. NULL,
  102. ff_mjpeg_decode_end,
  103. sp5x_decode_frame,
  104. 0,
  105. .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
  106. };