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.

113 lines
4.0KB

  1. /*
  2. * MJPEG/AVI1 to JPEG/JFIF bitstream format filter
  3. * Copyright (c) 2010 Adrian Daerr and Nicolas George
  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. * Adapted from mjpeg2jpeg.c, with original copyright:
  23. * Paris 2010 Adrian Daerr, public domain
  24. */
  25. #include <string.h>
  26. #include "avcodec.h"
  27. #include "mjpeg.h"
  28. static const uint8_t jpeg_header[] = {
  29. 0xff, 0xd8, // SOI
  30. 0xff, 0xe0, // APP0
  31. 0x00, 0x10, // APP0 header size (including
  32. // this field, but excluding preceding)
  33. 0x4a, 0x46, 0x49, 0x46, 0x00, // ID string 'JFIF\0'
  34. 0x01, 0x01, // version
  35. 0x00, // bits per type
  36. 0x00, 0x00, // X density
  37. 0x00, 0x00, // Y density
  38. 0x00, // X thumbnail size
  39. 0x00, // Y thumbnail size
  40. };
  41. static const int dht_segment_size = 420;
  42. static const uint8_t dht_segment_head[] = { 0xFF, 0xC4, 0x01, 0xA2, 0x00 };
  43. static const uint8_t dht_segment_frag[] = {
  44. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
  45. 0x0a, 0x0b, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
  46. 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. };
  48. static uint8_t *append(uint8_t *buf, const uint8_t *src, int size)
  49. {
  50. memcpy(buf, src, size);
  51. return buf + size;
  52. }
  53. static uint8_t *append_dht_segment(uint8_t *buf)
  54. {
  55. buf = append(buf, dht_segment_head, sizeof(dht_segment_head));
  56. buf = append(buf, ff_mjpeg_bits_dc_luminance + 1, 16);
  57. buf = append(buf, dht_segment_frag, sizeof(dht_segment_frag));
  58. buf = append(buf, ff_mjpeg_val_dc, 12);
  59. *(buf++) = 0x10;
  60. buf = append(buf, ff_mjpeg_bits_ac_luminance + 1, 16);
  61. buf = append(buf, ff_mjpeg_val_ac_luminance, 162);
  62. *(buf++) = 0x11;
  63. buf = append(buf, ff_mjpeg_bits_ac_chrominance + 1, 16);
  64. buf = append(buf, ff_mjpeg_val_ac_chrominance, 162);
  65. return buf;
  66. }
  67. static int mjpeg2jpeg_filter(AVBitStreamFilterContext *bsfc,
  68. AVCodecContext *avctx, const char *args,
  69. uint8_t **poutbuf, int *poutbuf_size,
  70. const uint8_t *buf, int buf_size,
  71. int keyframe)
  72. {
  73. int input_skip, output_size;
  74. uint8_t *output, *out;
  75. if (buf_size < 12) {
  76. av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
  77. return AVERROR_INVALIDDATA;
  78. }
  79. if (memcmp("AVI1", buf + 6, 4)) {
  80. av_log(avctx, AV_LOG_ERROR, "input is not MJPEG/AVI1\n");
  81. return AVERROR_INVALIDDATA;
  82. }
  83. input_skip = (buf[4] << 8) + buf[5] + 4;
  84. if (buf_size < input_skip) {
  85. av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
  86. return AVERROR_INVALIDDATA;
  87. }
  88. output_size = buf_size - input_skip +
  89. sizeof(jpeg_header) + dht_segment_size;
  90. output = out = av_malloc(output_size);
  91. if (!output)
  92. return AVERROR(ENOMEM);
  93. out = append(out, jpeg_header, sizeof(jpeg_header));
  94. out = append_dht_segment(out);
  95. out = append(out, buf + input_skip, buf_size - input_skip);
  96. *poutbuf = output;
  97. *poutbuf_size = output_size;
  98. return 1;
  99. }
  100. AVBitStreamFilter ff_mjpeg2jpeg_bsf = {
  101. .name = "mjpeg2jpeg",
  102. .filter = mjpeg2jpeg_filter,
  103. };