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.

122 lines
3.6KB

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