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.

118 lines
3.4KB

  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 *got_frame,
  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. uint8_t *recoded;
  38. int i = 0, j = 0;
  39. if (!avctx->width || !avctx->height)
  40. return -1;
  41. recoded = av_mallocz(buf_size + 1024);
  42. if (!recoded)
  43. return -1;
  44. /* SOI */
  45. recoded[j++] = 0xFF;
  46. recoded[j++] = 0xD8;
  47. memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
  48. memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64);
  49. memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64);
  50. j += sizeof(sp5x_data_dqt);
  51. memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
  52. j += sizeof(sp5x_data_dht);
  53. memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
  54. AV_WB16(recoded+j+5, avctx->coded_height);
  55. AV_WB16(recoded+j+7, avctx->coded_width);
  56. j += sizeof(sp5x_data_sof);
  57. memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
  58. j += sizeof(sp5x_data_sos);
  59. if(avctx->codec_id==AV_CODEC_ID_AMV)
  60. for (i = 2; i < buf_size-2 && j < buf_size+1024-2; i++)
  61. recoded[j++] = buf[i];
  62. else
  63. for (i = 14; i < buf_size && j < buf_size+1024-2; i++)
  64. {
  65. recoded[j++] = buf[i];
  66. if (buf[i] == 0xff)
  67. recoded[j++] = 0;
  68. }
  69. /* EOI */
  70. recoded[j++] = 0xFF;
  71. recoded[j++] = 0xD9;
  72. av_init_packet(&avpkt_recoded);
  73. avpkt_recoded.data = recoded;
  74. avpkt_recoded.size = j;
  75. i = ff_mjpeg_decode_frame(avctx, data, got_frame, &avpkt_recoded);
  76. av_free(recoded);
  77. return i;
  78. }
  79. AVCodec ff_sp5x_decoder = {
  80. .name = "sp5x",
  81. .long_name = NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"),
  82. .type = AVMEDIA_TYPE_VIDEO,
  83. .id = AV_CODEC_ID_SP5X,
  84. .priv_data_size = sizeof(MJpegDecodeContext),
  85. .init = ff_mjpeg_decode_init,
  86. .close = ff_mjpeg_decode_end,
  87. .decode = sp5x_decode_frame,
  88. .capabilities = CODEC_CAP_DR1,
  89. };
  90. AVCodec ff_amv_decoder = {
  91. .name = "amv",
  92. .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
  93. .type = AVMEDIA_TYPE_VIDEO,
  94. .id = AV_CODEC_ID_AMV,
  95. .priv_data_size = sizeof(MJpegDecodeContext),
  96. .init = ff_mjpeg_decode_init,
  97. .close = ff_mjpeg_decode_end,
  98. .decode = sp5x_decode_frame,
  99. };