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.

127 lines
3.3KB

  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
  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. avctx->flags &= ~CODEC_FLAG_EMU_EDGE;
  75. av_init_packet(&avpkt_recoded);
  76. avpkt_recoded.data = recoded;
  77. avpkt_recoded.size = j;
  78. i = ff_mjpeg_decode_frame(avctx, data, data_size, &avpkt_recoded);
  79. av_free(recoded);
  80. return i;
  81. }
  82. AVCodec sp5x_decoder = {
  83. "sp5x",
  84. AVMEDIA_TYPE_VIDEO,
  85. CODEC_ID_SP5X,
  86. sizeof(MJpegDecodeContext),
  87. ff_mjpeg_decode_init,
  88. NULL,
  89. ff_mjpeg_decode_end,
  90. sp5x_decode_frame,
  91. CODEC_CAP_DR1,
  92. NULL,
  93. .max_lowres = 5,
  94. .long_name = NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"),
  95. };
  96. AVCodec amv_decoder = {
  97. "amv",
  98. AVMEDIA_TYPE_VIDEO,
  99. CODEC_ID_AMV,
  100. sizeof(MJpegDecodeContext),
  101. ff_mjpeg_decode_init,
  102. NULL,
  103. ff_mjpeg_decode_end,
  104. sp5x_decode_frame,
  105. CODEC_CAP_DR1,
  106. .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
  107. };