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.

341 lines
10KB

  1. /*
  2. * Common code for the RTP depacketization of MPEG-4 formats.
  3. * Copyright (c) 2010 Fabrice Bellard
  4. * Romain Degez
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * @brief MPEG4 / RTP Code
  25. * @author Fabrice Bellard
  26. * @author Romain Degez
  27. */
  28. #include "rtpdec_formats.h"
  29. #include "internal.h"
  30. #include "libavutil/attributes.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavcodec/get_bits.h"
  33. #define MAX_AAC_HBR_FRAME_SIZE 8191
  34. /** Structure listing useful vars to parse RTP packet payload */
  35. struct PayloadContext {
  36. int sizelength;
  37. int indexlength;
  38. int indexdeltalength;
  39. int profile_level_id;
  40. int streamtype;
  41. int objecttype;
  42. char *mode;
  43. /** mpeg 4 AU headers */
  44. struct AUHeaders {
  45. int size;
  46. int index;
  47. int cts_flag;
  48. int cts;
  49. int dts_flag;
  50. int dts;
  51. int rap_flag;
  52. int streamstate;
  53. } *au_headers;
  54. int au_headers_allocated;
  55. int nb_au_headers;
  56. int au_headers_length_bytes;
  57. int cur_au_index;
  58. uint8_t buf[FFMAX(RTP_MAX_PACKET_LENGTH, MAX_AAC_HBR_FRAME_SIZE)];
  59. int buf_pos, buf_size;
  60. uint32_t timestamp;
  61. };
  62. typedef struct AttrNameMap {
  63. const char *str;
  64. uint16_t type;
  65. uint32_t offset;
  66. } AttrNameMap;
  67. /* All known fmtp parameters and the corresponding RTPAttrTypeEnum */
  68. #define ATTR_NAME_TYPE_INT 0
  69. #define ATTR_NAME_TYPE_STR 1
  70. static const AttrNameMap attr_names[] = {
  71. { "SizeLength", ATTR_NAME_TYPE_INT,
  72. offsetof(PayloadContext, sizelength) },
  73. { "IndexLength", ATTR_NAME_TYPE_INT,
  74. offsetof(PayloadContext, indexlength) },
  75. { "IndexDeltaLength", ATTR_NAME_TYPE_INT,
  76. offsetof(PayloadContext, indexdeltalength) },
  77. { "profile-level-id", ATTR_NAME_TYPE_INT,
  78. offsetof(PayloadContext, profile_level_id) },
  79. { "StreamType", ATTR_NAME_TYPE_INT,
  80. offsetof(PayloadContext, streamtype) },
  81. { "mode", ATTR_NAME_TYPE_STR,
  82. offsetof(PayloadContext, mode) },
  83. { NULL, -1, -1 },
  84. };
  85. static PayloadContext *new_context(void)
  86. {
  87. return av_mallocz(sizeof(PayloadContext));
  88. }
  89. static void free_context(PayloadContext *data)
  90. {
  91. av_free(data->au_headers);
  92. av_free(data->mode);
  93. av_free(data);
  94. }
  95. static int parse_fmtp_config(AVCodecContext *codec, char *value)
  96. {
  97. /* decode the hexa encoded parameter */
  98. int len = ff_hex_to_data(NULL, value);
  99. av_free(codec->extradata);
  100. codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  101. if (!codec->extradata)
  102. return AVERROR(ENOMEM);
  103. codec->extradata_size = len;
  104. ff_hex_to_data(codec->extradata, value);
  105. return 0;
  106. }
  107. static int rtp_parse_mp4_au(PayloadContext *data, const uint8_t *buf, int len)
  108. {
  109. int au_headers_length, au_header_size, i;
  110. GetBitContext getbitcontext;
  111. if (len < 2)
  112. return AVERROR_INVALIDDATA;
  113. /* decode the first 2 bytes where the AUHeader sections are stored
  114. length in bits */
  115. au_headers_length = AV_RB16(buf);
  116. if (au_headers_length > RTP_MAX_PACKET_LENGTH)
  117. return -1;
  118. data->au_headers_length_bytes = (au_headers_length + 7) / 8;
  119. /* skip AU headers length section (2 bytes) */
  120. buf += 2;
  121. len -= 2;
  122. if (len < data->au_headers_length_bytes)
  123. return AVERROR_INVALIDDATA;
  124. init_get_bits(&getbitcontext, buf, data->au_headers_length_bytes * 8);
  125. /* XXX: Wrong if optionnal additional sections are present (cts, dts etc...) */
  126. au_header_size = data->sizelength + data->indexlength;
  127. if (au_header_size <= 0 || (au_headers_length % au_header_size != 0))
  128. return -1;
  129. data->nb_au_headers = au_headers_length / au_header_size;
  130. if (!data->au_headers || data->au_headers_allocated < data->nb_au_headers) {
  131. av_free(data->au_headers);
  132. data->au_headers = av_malloc(sizeof(struct AUHeaders) * data->nb_au_headers);
  133. if (!data->au_headers)
  134. return AVERROR(ENOMEM);
  135. data->au_headers_allocated = data->nb_au_headers;
  136. }
  137. for (i = 0; i < data->nb_au_headers; ++i) {
  138. data->au_headers[i].size = get_bits_long(&getbitcontext, data->sizelength);
  139. data->au_headers[i].index = get_bits_long(&getbitcontext, data->indexlength);
  140. }
  141. return 0;
  142. }
  143. /* Follows RFC 3640 */
  144. static int aac_parse_packet(AVFormatContext *ctx, PayloadContext *data,
  145. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  146. const uint8_t *buf, int len, uint16_t seq,
  147. int flags)
  148. {
  149. int ret;
  150. if (!buf) {
  151. if (data->cur_au_index > data->nb_au_headers) {
  152. av_log(ctx, AV_LOG_ERROR, "Invalid parser state\n");
  153. return AVERROR_INVALIDDATA;
  154. }
  155. if (data->buf_size - data->buf_pos < data->au_headers[data->cur_au_index].size) {
  156. av_log(ctx, AV_LOG_ERROR, "Invalid AU size\n");
  157. return AVERROR_INVALIDDATA;
  158. }
  159. if ((ret = av_new_packet(pkt, data->au_headers[data->cur_au_index].size)) < 0) {
  160. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  161. return ret;
  162. }
  163. memcpy(pkt->data, &data->buf[data->buf_pos], data->au_headers[data->cur_au_index].size);
  164. data->buf_pos += data->au_headers[data->cur_au_index].size;
  165. pkt->stream_index = st->index;
  166. data->cur_au_index++;
  167. if (data->cur_au_index == data->nb_au_headers) {
  168. data->buf_pos = 0;
  169. return 0;
  170. }
  171. return 1;
  172. }
  173. if (rtp_parse_mp4_au(data, buf, len)) {
  174. av_log(ctx, AV_LOG_ERROR, "Error parsing AU headers\n");
  175. return -1;
  176. }
  177. buf += data->au_headers_length_bytes + 2;
  178. len -= data->au_headers_length_bytes + 2;
  179. if (data->nb_au_headers == 1 && len < data->au_headers[0].size) {
  180. /* Packet is fragmented */
  181. if (!data->buf_pos) {
  182. if (data->au_headers[0].size > MAX_AAC_HBR_FRAME_SIZE) {
  183. av_log(ctx, AV_LOG_ERROR, "Invalid AU size\n");
  184. return AVERROR_INVALIDDATA;
  185. }
  186. data->buf_size = data->au_headers[0].size;
  187. data->timestamp = *timestamp;
  188. }
  189. if (data->timestamp != *timestamp ||
  190. data->au_headers[0].size != data->buf_size ||
  191. data->buf_pos + len > MAX_AAC_HBR_FRAME_SIZE) {
  192. data->buf_pos = 0;
  193. data->buf_size = 0;
  194. av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
  195. return AVERROR_INVALIDDATA;
  196. }
  197. memcpy(&data->buf[data->buf_pos], buf, len);
  198. data->buf_pos += len;
  199. if (!(flags & RTP_FLAG_MARKER))
  200. return AVERROR(EAGAIN);
  201. if (data->buf_pos != data->buf_size) {
  202. data->buf_pos = 0;
  203. av_log(ctx, AV_LOG_ERROR, "Missed some packets, discarding frame\n");
  204. return AVERROR_INVALIDDATA;
  205. }
  206. data->buf_pos = 0;
  207. ret = av_new_packet(pkt, data->buf_size);
  208. if (ret < 0) {
  209. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  210. return ret;
  211. }
  212. pkt->stream_index = st->index;
  213. memcpy(pkt->data, data->buf, data->buf_size);
  214. return 0;
  215. }
  216. if (len < data->au_headers[0].size) {
  217. av_log(ctx, AV_LOG_ERROR, "First AU larger than packet size\n");
  218. return AVERROR_INVALIDDATA;
  219. }
  220. if ((ret = av_new_packet(pkt, data->au_headers[0].size)) < 0) {
  221. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  222. return ret;
  223. }
  224. memcpy(pkt->data, buf, data->au_headers[0].size);
  225. len -= data->au_headers[0].size;
  226. buf += data->au_headers[0].size;
  227. pkt->stream_index = st->index;
  228. if (len > 0 && data->nb_au_headers > 1) {
  229. data->buf_size = FFMIN(len, sizeof(data->buf));
  230. memcpy(data->buf, buf, data->buf_size);
  231. data->cur_au_index = 1;
  232. data->buf_pos = 0;
  233. return 1;
  234. }
  235. return 0;
  236. }
  237. static int parse_fmtp(AVFormatContext *s,
  238. AVStream *stream, PayloadContext *data,
  239. char *attr, char *value)
  240. {
  241. AVCodecContext *codec = stream->codec;
  242. int res, i;
  243. if (!strcmp(attr, "config")) {
  244. res = parse_fmtp_config(codec, value);
  245. if (res < 0)
  246. return res;
  247. }
  248. if (codec->codec_id == AV_CODEC_ID_AAC) {
  249. /* Looking for a known attribute */
  250. for (i = 0; attr_names[i].str; ++i) {
  251. if (!av_strcasecmp(attr, attr_names[i].str)) {
  252. if (attr_names[i].type == ATTR_NAME_TYPE_INT) {
  253. *(int *)((char *)data+
  254. attr_names[i].offset) = atoi(value);
  255. } else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
  256. *(char **)((char *)data+
  257. attr_names[i].offset) = av_strdup(value);
  258. }
  259. }
  260. }
  261. return 0;
  262. }
  263. static int parse_sdp_line(AVFormatContext *s, int st_index,
  264. PayloadContext *data, const char *line)
  265. {
  266. const char *p;
  267. if (st_index < 0)
  268. return 0;
  269. if (av_strstart(line, "fmtp:", &p))
  270. return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp);
  271. return 0;
  272. }
  273. RTPDynamicProtocolHandler ff_mp4v_es_dynamic_handler = {
  274. .enc_name = "MP4V-ES",
  275. .codec_type = AVMEDIA_TYPE_VIDEO,
  276. .codec_id = AV_CODEC_ID_MPEG4,
  277. .need_parsing = AVSTREAM_PARSE_FULL,
  278. .parse_sdp_a_line = parse_sdp_line,
  279. };
  280. RTPDynamicProtocolHandler ff_mpeg4_generic_dynamic_handler = {
  281. .enc_name = "mpeg4-generic",
  282. .codec_type = AVMEDIA_TYPE_AUDIO,
  283. .codec_id = AV_CODEC_ID_AAC,
  284. .parse_sdp_a_line = parse_sdp_line,
  285. .alloc = new_context,
  286. .free = free_context,
  287. .parse_packet = aac_parse_packet,
  288. };