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.

109 lines
3.6KB

  1. /*
  2. * MJPEG A dump header bitstream filter
  3. * Copyright (c) 2006 Baptiste Coudurier
  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. * MJPEG A dump header bitstream filter
  24. * modifies bitstream to be decoded by quicktime
  25. */
  26. #include "avcodec.h"
  27. #include "bsf.h"
  28. #include "bytestream.h"
  29. #include "mjpeg.h"
  30. static int mjpega_dump_header(AVBSFContext *ctx, AVPacket *out)
  31. {
  32. AVPacket *in;
  33. uint8_t *out_buf;
  34. unsigned dqt = 0, dht = 0, sof0 = 0;
  35. int ret = 0, i;
  36. ret = ff_bsf_get_packet(ctx, &in);
  37. if (ret < 0)
  38. return ret;
  39. ret = av_new_packet(out, in->size + 44);
  40. if (ret < 0)
  41. goto fail;
  42. ret = av_packet_copy_props(out, in);
  43. if (ret < 0)
  44. goto fail;
  45. out_buf = out->data;
  46. bytestream_put_byte(&out_buf, 0xff);
  47. bytestream_put_byte(&out_buf, SOI);
  48. bytestream_put_byte(&out_buf, 0xff);
  49. bytestream_put_byte(&out_buf, APP1);
  50. bytestream_put_be16(&out_buf, 42); /* size */
  51. bytestream_put_be32(&out_buf, 0);
  52. bytestream_put_buffer(&out_buf, "mjpg", 4);
  53. bytestream_put_be32(&out_buf, in->size + 44); /* field size */
  54. bytestream_put_be32(&out_buf, in->size + 44); /* pad field size */
  55. bytestream_put_be32(&out_buf, 0); /* next ptr */
  56. for (i = 0; i < in->size - 1; i++) {
  57. if (in->data[i] == 0xff) {
  58. switch (in->data[i + 1]) {
  59. case DQT: dqt = i + 46; break;
  60. case DHT: dht = i + 46; break;
  61. case SOF0: sof0 = i + 46; break;
  62. case SOS:
  63. bytestream_put_be32(&out_buf, dqt); /* quant off */
  64. bytestream_put_be32(&out_buf, dht); /* huff off */
  65. bytestream_put_be32(&out_buf, sof0); /* image off */
  66. bytestream_put_be32(&out_buf, i + 46); /* scan off */
  67. bytestream_put_be32(&out_buf, i + 46 + AV_RB16(in->data + i + 2)); /* data off */
  68. bytestream_put_buffer(&out_buf, in->data + 2, in->size - 2); /* skip already written SOI */
  69. out->size = out_buf - out->data;
  70. av_packet_free(&in);
  71. return 0;
  72. case APP1:
  73. if (i + 8 < in->size && AV_RL32(in->data + i + 8) == AV_RL32("mjpg")) {
  74. av_log(ctx, AV_LOG_ERROR, "bitstream already formatted\n");
  75. av_packet_unref(out);
  76. av_packet_move_ref(out, in);
  77. av_packet_free(&in);
  78. return 0;
  79. }
  80. }
  81. }
  82. }
  83. av_log(ctx, AV_LOG_ERROR, "could not find SOS marker in bitstream\n");
  84. fail:
  85. av_packet_unref(out);
  86. av_packet_free(&in);
  87. return AVERROR_INVALIDDATA;
  88. }
  89. static const enum AVCodecID codec_ids[] = {
  90. AV_CODEC_ID_MJPEG, AV_CODEC_ID_NONE,
  91. };
  92. const AVBitStreamFilter ff_mjpega_dump_header_bsf = {
  93. .name = "mjpegadump",
  94. .filter = mjpega_dump_header,
  95. .codec_ids = codec_ids,
  96. };