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.

132 lines
4.1KB

  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 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. * Adapted from mjpeg2jpeg.c, with original copyright:
  23. * Paris 2010 Adrian Daerr, public domain
  24. */
  25. #include <string.h>
  26. #include "libavutil/error.h"
  27. #include "libavutil/mem.h"
  28. #include "avcodec.h"
  29. #include "bsf.h"
  30. #include "jpegtables.h"
  31. static const uint8_t jpeg_header[] = {
  32. 0xff, 0xd8, // SOI
  33. 0xff, 0xe0, // APP0
  34. 0x00, 0x10, // APP0 header size (including
  35. // this field, but excluding preceding)
  36. 0x4a, 0x46, 0x49, 0x46, 0x00, // ID string 'JFIF\0'
  37. 0x01, 0x01, // version
  38. 0x00, // bits per type
  39. 0x00, 0x00, // X density
  40. 0x00, 0x00, // Y density
  41. 0x00, // X thumbnail size
  42. 0x00, // Y thumbnail size
  43. };
  44. static const int dht_segment_size = 420;
  45. static const uint8_t dht_segment_head[] = { 0xFF, 0xC4, 0x01, 0xA2, 0x00 };
  46. static const uint8_t dht_segment_frag[] = {
  47. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
  48. 0x0a, 0x0b, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
  49. 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. };
  51. static uint8_t *append(uint8_t *buf, const uint8_t *src, int size)
  52. {
  53. memcpy(buf, src, size);
  54. return buf + size;
  55. }
  56. static uint8_t *append_dht_segment(uint8_t *buf)
  57. {
  58. buf = append(buf, dht_segment_head, sizeof(dht_segment_head));
  59. buf = append(buf, avpriv_mjpeg_bits_dc_luminance + 1, 16);
  60. buf = append(buf, dht_segment_frag, sizeof(dht_segment_frag));
  61. buf = append(buf, avpriv_mjpeg_val_dc, 12);
  62. *(buf++) = 0x10;
  63. buf = append(buf, avpriv_mjpeg_bits_ac_luminance + 1, 16);
  64. buf = append(buf, avpriv_mjpeg_val_ac_luminance, 162);
  65. *(buf++) = 0x11;
  66. buf = append(buf, avpriv_mjpeg_bits_ac_chrominance + 1, 16);
  67. buf = append(buf, avpriv_mjpeg_val_ac_chrominance, 162);
  68. return buf;
  69. }
  70. static int mjpeg2jpeg_filter(AVBSFContext *ctx, AVPacket *out)
  71. {
  72. AVPacket *in;
  73. int ret = 0;
  74. int input_skip, output_size;
  75. uint8_t *output;
  76. ret = ff_bsf_get_packet(ctx, &in);
  77. if (in->size < 12) {
  78. av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
  79. ret = AVERROR_INVALIDDATA;
  80. goto fail;
  81. }
  82. if (memcmp("AVI1", in->data + 6, 4)) {
  83. av_log(ctx, AV_LOG_ERROR, "input is not MJPEG/AVI1\n");
  84. ret = AVERROR_INVALIDDATA;
  85. goto fail;
  86. }
  87. input_skip = (in->data[4] << 8) + in->data[5] + 4;
  88. if (in->size < input_skip) {
  89. av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
  90. ret = AVERROR_INVALIDDATA;
  91. goto fail;
  92. }
  93. output_size = in->size - input_skip +
  94. sizeof(jpeg_header) + dht_segment_size;
  95. ret = av_new_packet(out, output_size);
  96. if (ret < 0)
  97. goto fail;
  98. output = out->data;
  99. output = append(output, jpeg_header, sizeof(jpeg_header));
  100. output = append_dht_segment(output);
  101. output = append(output, in->data + input_skip, in->size - input_skip);
  102. ret = av_packet_copy_props(out, in);
  103. if (ret < 0)
  104. goto fail;
  105. fail:
  106. if (ret < 0)
  107. av_packet_unref(out);
  108. av_packet_free(&in);
  109. return ret;
  110. }
  111. const AVBitStreamFilter ff_mjpeg2jpeg_bsf = {
  112. .name = "mjpeg2jpeg",
  113. .filter = mjpeg2jpeg_filter,
  114. };