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.8KB

  1. /*
  2. * Linux Media Labs MPEG-4 demuxer
  3. * Copyright (c) 2008 Ivo van Poorten
  4. *
  5. * Due to a lack of sample files, only files with one channel are supported.
  6. * u-law and ADPCM audio are unsupported for the same reason.
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "avformat.h"
  25. #define LMLM4_I_FRAME 0x00
  26. #define LMLM4_P_FRAME 0x01
  27. #define LMLM4_B_FRAME 0x02
  28. #define LMLM4_INVALID 0x03
  29. #define LMLM4_MPEG1L2 0x04
  30. #define LMLM4_MAX_PACKET_SIZE 1024 * 1024
  31. static int lmlm4_probe(AVProbeData * pd) {
  32. unsigned char *buf = pd->buf;
  33. unsigned int frame_type, packet_size;
  34. frame_type = AV_RB16(buf+2);
  35. packet_size = AV_RB32(buf+4);
  36. if (!AV_RB16(buf) && frame_type <= LMLM4_MPEG1L2 && packet_size &&
  37. frame_type != LMLM4_INVALID && packet_size <= LMLM4_MAX_PACKET_SIZE) {
  38. if (frame_type == LMLM4_MPEG1L2) {
  39. if ((AV_RB16(buf+8) & 0xfffe) != 0xfffc)
  40. return 0;
  41. /* I could calculate the audio framesize and compare with
  42. * packet_size-8, but that seems overkill */
  43. return AVPROBE_SCORE_MAX / 3;
  44. } else if (AV_RB24(buf+8) == 0x000001) { /* PES Signal */
  45. return AVPROBE_SCORE_MAX / 5;
  46. }
  47. }
  48. return 0;
  49. }
  50. static int lmlm4_read_header(AVFormatContext *s, AVFormatParameters *ap) {
  51. AVStream *st;
  52. if (!(st = av_new_stream(s, 0)))
  53. return AVERROR(ENOMEM);
  54. st->codec->codec_type = CODEC_TYPE_VIDEO;
  55. st->codec->codec_id = CODEC_ID_MPEG4;
  56. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  57. av_set_pts_info(st, 64, 1001, 30000);
  58. if (!(st = av_new_stream(s, 1)))
  59. return AVERROR(ENOMEM);
  60. st->codec->codec_type = CODEC_TYPE_AUDIO;
  61. st->codec->codec_id = CODEC_ID_MP2;
  62. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  63. /* the parameters will be extracted from the compressed bitstream */
  64. return 0;
  65. }
  66. static int lmlm4_read_packet(AVFormatContext *s, AVPacket *pkt) {
  67. ByteIOContext *pb = s->pb;
  68. int ret;
  69. unsigned int frame_type, packet_size, padding, frame_size;
  70. get_be16(pb); /* channel number */
  71. frame_type = get_be16(pb);
  72. packet_size = get_be32(pb);
  73. padding = -packet_size & 511;
  74. frame_size = packet_size - 8;
  75. if (frame_type > LMLM4_MPEG1L2 || frame_type == LMLM4_INVALID) {
  76. av_log(s, AV_LOG_ERROR, "invalid or unsupported frame_type\n");
  77. return AVERROR(EIO);
  78. }
  79. if (packet_size > LMLM4_MAX_PACKET_SIZE) {
  80. av_log(s, AV_LOG_ERROR, "packet size exceeds maximum\n");
  81. return AVERROR(EIO);
  82. }
  83. if ((ret = av_get_packet(pb, pkt, frame_size)) <= 0)
  84. return AVERROR(EIO);
  85. url_fskip(pb, padding);
  86. switch (frame_type) {
  87. case LMLM4_I_FRAME:
  88. pkt->flags = PKT_FLAG_KEY;
  89. case LMLM4_P_FRAME:
  90. case LMLM4_B_FRAME:
  91. pkt->stream_index = 0;
  92. break;
  93. case LMLM4_MPEG1L2:
  94. pkt->stream_index = 1;
  95. break;
  96. }
  97. return ret;
  98. }
  99. AVInputFormat lmlm4_demuxer = {
  100. "lmlm4",
  101. "lmlm4 raw format",
  102. 0,
  103. lmlm4_probe,
  104. lmlm4_read_header,
  105. lmlm4_read_packet,
  106. };