lmlm4.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "libavutil/intreadwrite.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #define LMLM4_I_FRAME 0x00
  28. #define LMLM4_P_FRAME 0x01
  29. #define LMLM4_B_FRAME 0x02
  30. #define LMLM4_INVALID 0x03
  31. #define LMLM4_MPEG1L2 0x04
  32. #define LMLM4_MAX_PACKET_SIZE 1024 * 1024
  33. static int lmlm4_probe(AVProbeData * pd) {
  34. unsigned char *buf = pd->buf;
  35. unsigned int frame_type, packet_size;
  36. frame_type = AV_RB16(buf+2);
  37. packet_size = AV_RB32(buf+4);
  38. if (!AV_RB16(buf) && frame_type <= LMLM4_MPEG1L2 && packet_size &&
  39. frame_type != LMLM4_INVALID && packet_size <= LMLM4_MAX_PACKET_SIZE) {
  40. if (frame_type == LMLM4_MPEG1L2) {
  41. if ((AV_RB16(buf+8) & 0xfffe) != 0xfffc)
  42. return 0;
  43. /* I could calculate the audio framesize and compare with
  44. * packet_size-8, but that seems overkill */
  45. return AVPROBE_SCORE_MAX / 3;
  46. } else if (AV_RB24(buf+8) == 0x000001) { /* PES Signal */
  47. return AVPROBE_SCORE_MAX / 5;
  48. }
  49. }
  50. return 0;
  51. }
  52. static int lmlm4_read_header(AVFormatContext *s) {
  53. AVStream *st;
  54. if (!(st = avformat_new_stream(s, NULL)))
  55. return AVERROR(ENOMEM);
  56. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  57. st->codec->codec_id = AV_CODEC_ID_MPEG4;
  58. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  59. avpriv_set_pts_info(st, 64, 1001, 30000);
  60. if (!(st = avformat_new_stream(s, NULL)))
  61. return AVERROR(ENOMEM);
  62. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  63. st->codec->codec_id = AV_CODEC_ID_MP2;
  64. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  65. /* the parameters will be extracted from the compressed bitstream */
  66. return 0;
  67. }
  68. static int lmlm4_read_packet(AVFormatContext *s, AVPacket *pkt) {
  69. AVIOContext *pb = s->pb;
  70. int ret;
  71. unsigned int frame_type, packet_size, padding, frame_size;
  72. avio_rb16(pb); /* channel number */
  73. frame_type = avio_rb16(pb);
  74. packet_size = avio_rb32(pb);
  75. padding = -packet_size & 511;
  76. frame_size = packet_size - 8;
  77. if (frame_type > LMLM4_MPEG1L2 || frame_type == LMLM4_INVALID) {
  78. av_log(s, AV_LOG_ERROR, "invalid or unsupported frame_type\n");
  79. return AVERROR(EIO);
  80. }
  81. if (packet_size > LMLM4_MAX_PACKET_SIZE) {
  82. av_log(s, AV_LOG_ERROR, "packet size exceeds maximum\n");
  83. return AVERROR(EIO);
  84. }
  85. if ((ret = av_get_packet(pb, pkt, frame_size)) <= 0)
  86. return AVERROR(EIO);
  87. avio_skip(pb, padding);
  88. switch (frame_type) {
  89. case LMLM4_I_FRAME:
  90. pkt->flags = AV_PKT_FLAG_KEY;
  91. case LMLM4_P_FRAME:
  92. case LMLM4_B_FRAME:
  93. pkt->stream_index = 0;
  94. break;
  95. case LMLM4_MPEG1L2:
  96. pkt->stream_index = 1;
  97. break;
  98. }
  99. return ret;
  100. }
  101. AVInputFormat ff_lmlm4_demuxer = {
  102. .name = "lmlm4",
  103. .long_name = NULL_IF_CONFIG_SMALL("raw lmlm4"),
  104. .read_probe = lmlm4_probe,
  105. .read_header = lmlm4_read_header,
  106. .read_packet = lmlm4_read_packet,
  107. };