rtpdec_latm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * RTP Depacketization of MP4A-LATM, RFC 3016
  3. * Copyright (c) 2010 Martin Storsjo
  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. #include "rtpdec_formats.h"
  22. #include "internal.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavcodec/get_bits.h"
  25. struct PayloadContext {
  26. AVIOContext *dyn_buf;
  27. uint8_t *buf;
  28. int pos, len;
  29. uint32_t timestamp;
  30. };
  31. static PayloadContext *latm_new_context(void)
  32. {
  33. return av_mallocz(sizeof(PayloadContext));
  34. }
  35. static void latm_free_context(PayloadContext *data)
  36. {
  37. if (!data)
  38. return;
  39. if (data->dyn_buf) {
  40. uint8_t *p;
  41. avio_close_dyn_buf(data->dyn_buf, &p);
  42. av_free(p);
  43. }
  44. av_free(data->buf);
  45. av_free(data);
  46. }
  47. static int latm_parse_packet(AVFormatContext *ctx, PayloadContext *data,
  48. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  49. const uint8_t *buf, int len, int flags)
  50. {
  51. int ret, cur_len;
  52. if (buf) {
  53. if (!data->dyn_buf || data->timestamp != *timestamp) {
  54. av_freep(&data->buf);
  55. if (data->dyn_buf)
  56. avio_close_dyn_buf(data->dyn_buf, &data->buf);
  57. data->dyn_buf = NULL;
  58. av_freep(&data->buf);
  59. data->timestamp = *timestamp;
  60. if ((ret = avio_open_dyn_buf(&data->dyn_buf)) < 0)
  61. return ret;
  62. }
  63. avio_write(data->dyn_buf, buf, len);
  64. if (!(flags & RTP_FLAG_MARKER))
  65. return AVERROR(EAGAIN);
  66. av_free(data->buf);
  67. data->len = avio_close_dyn_buf(data->dyn_buf, &data->buf);
  68. data->dyn_buf = NULL;
  69. data->pos = 0;
  70. }
  71. if (!data->buf) {
  72. av_log(ctx, AV_LOG_ERROR, "No data available yet\n");
  73. return AVERROR(EIO);
  74. }
  75. cur_len = 0;
  76. while (data->pos < data->len) {
  77. uint8_t val = data->buf[data->pos++];
  78. cur_len += val;
  79. if (val != 0xff)
  80. break;
  81. }
  82. if (data->pos + cur_len > data->len) {
  83. av_log(ctx, AV_LOG_ERROR, "Malformed LATM packet\n");
  84. return AVERROR(EIO);
  85. }
  86. if ((ret = av_new_packet(pkt, cur_len)) < 0)
  87. return ret;
  88. memcpy(pkt->data, data->buf + data->pos, cur_len);
  89. data->pos += cur_len;
  90. pkt->stream_index = st->index;
  91. return data->pos < data->len;
  92. }
  93. static int parse_fmtp_config(AVStream *st, char *value)
  94. {
  95. int len = ff_hex_to_data(NULL, value), i, ret = 0;
  96. GetBitContext gb;
  97. uint8_t *config;
  98. int audio_mux_version, same_time_framing, num_programs, num_layers;
  99. /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */
  100. config = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  101. if (!config)
  102. return AVERROR(ENOMEM);
  103. ff_hex_to_data(config, value);
  104. init_get_bits(&gb, config, len*8);
  105. audio_mux_version = get_bits(&gb, 1);
  106. same_time_framing = get_bits(&gb, 1);
  107. skip_bits(&gb, 6); /* num_sub_frames */
  108. num_programs = get_bits(&gb, 4);
  109. num_layers = get_bits(&gb, 3);
  110. if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 ||
  111. num_layers != 0) {
  112. av_log(NULL, AV_LOG_WARNING, "Unsupported LATM config (%d,%d,%d,%d)\n",
  113. audio_mux_version, same_time_framing,
  114. num_programs, num_layers);
  115. ret = AVERROR_PATCHWELCOME;
  116. goto end;
  117. }
  118. av_freep(&st->codec->extradata);
  119. st->codec->extradata_size = (get_bits_left(&gb) + 7)/8;
  120. st->codec->extradata = av_mallocz(st->codec->extradata_size +
  121. FF_INPUT_BUFFER_PADDING_SIZE);
  122. if (!st->codec->extradata) {
  123. ret = AVERROR(ENOMEM);
  124. goto end;
  125. }
  126. for (i = 0; i < st->codec->extradata_size; i++)
  127. st->codec->extradata[i] = get_bits(&gb, 8);
  128. end:
  129. av_free(config);
  130. return ret;
  131. }
  132. static int parse_fmtp(AVStream *stream, PayloadContext *data,
  133. char *attr, char *value)
  134. {
  135. int res;
  136. if (!strcmp(attr, "config")) {
  137. res = parse_fmtp_config(stream, value);
  138. if (res < 0)
  139. return res;
  140. } else if (!strcmp(attr, "cpresent")) {
  141. int cpresent = atoi(value);
  142. if (cpresent != 0)
  143. av_log_missing_feature(NULL, "RTP MP4A-LATM with in-band "
  144. "configuration", 1);
  145. }
  146. return 0;
  147. }
  148. static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
  149. PayloadContext *data, const char *line)
  150. {
  151. const char *p;
  152. if (st_index < 0)
  153. return 0;
  154. if (av_strstart(line, "fmtp:", &p))
  155. return ff_parse_fmtp(s->streams[st_index], data, p, parse_fmtp);
  156. return 0;
  157. }
  158. RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler = {
  159. .enc_name = "MP4A-LATM",
  160. .codec_type = AVMEDIA_TYPE_AUDIO,
  161. .codec_id = AV_CODEC_ID_AAC,
  162. .parse_sdp_a_line = latm_parse_sdp_line,
  163. .alloc = latm_new_context,
  164. .free = latm_free_context,
  165. .parse_packet = latm_parse_packet
  166. };