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.

335 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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 void close_context(PayloadContext *data)
  86. {
  87. av_freep(&data->au_headers);
  88. av_freep(&data->mode);
  89. }
  90. static int parse_fmtp_config(AVCodecContext *codec, const char *value)
  91. {
  92. /* decode the hexa encoded parameter */
  93. int len = ff_hex_to_data(NULL, value);
  94. av_freep(&codec->extradata);
  95. if (ff_alloc_extradata(codec, len))
  96. return AVERROR(ENOMEM);
  97. ff_hex_to_data(codec->extradata, value);
  98. return 0;
  99. }
  100. static int rtp_parse_mp4_au(PayloadContext *data, const uint8_t *buf, int len)
  101. {
  102. int au_headers_length, au_header_size, i;
  103. GetBitContext getbitcontext;
  104. if (len < 2)
  105. return AVERROR_INVALIDDATA;
  106. /* decode the first 2 bytes where the AUHeader sections are stored
  107. length in bits */
  108. au_headers_length = AV_RB16(buf);
  109. if (au_headers_length > RTP_MAX_PACKET_LENGTH)
  110. return -1;
  111. data->au_headers_length_bytes = (au_headers_length + 7) / 8;
  112. /* skip AU headers length section (2 bytes) */
  113. buf += 2;
  114. len -= 2;
  115. if (len < data->au_headers_length_bytes)
  116. return AVERROR_INVALIDDATA;
  117. init_get_bits(&getbitcontext, buf, data->au_headers_length_bytes * 8);
  118. /* XXX: Wrong if optional additional sections are present (cts, dts etc...) */
  119. au_header_size = data->sizelength + data->indexlength;
  120. if (au_header_size <= 0 || (au_headers_length % au_header_size != 0))
  121. return -1;
  122. data->nb_au_headers = au_headers_length / au_header_size;
  123. if (!data->au_headers || data->au_headers_allocated < data->nb_au_headers) {
  124. av_free(data->au_headers);
  125. data->au_headers = av_malloc(sizeof(struct AUHeaders) * data->nb_au_headers);
  126. if (!data->au_headers)
  127. return AVERROR(ENOMEM);
  128. data->au_headers_allocated = data->nb_au_headers;
  129. }
  130. for (i = 0; i < data->nb_au_headers; ++i) {
  131. data->au_headers[i].size = get_bits_long(&getbitcontext, data->sizelength);
  132. data->au_headers[i].index = get_bits_long(&getbitcontext, data->indexlength);
  133. }
  134. return 0;
  135. }
  136. /* Follows RFC 3640 */
  137. static int aac_parse_packet(AVFormatContext *ctx, PayloadContext *data,
  138. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  139. const uint8_t *buf, int len, uint16_t seq,
  140. int flags)
  141. {
  142. int ret;
  143. if (!buf) {
  144. if (data->cur_au_index > data->nb_au_headers) {
  145. av_log(ctx, AV_LOG_ERROR, "Invalid parser state\n");
  146. return AVERROR_INVALIDDATA;
  147. }
  148. if (data->buf_size - data->buf_pos < data->au_headers[data->cur_au_index].size) {
  149. av_log(ctx, AV_LOG_ERROR, "Invalid AU size\n");
  150. return AVERROR_INVALIDDATA;
  151. }
  152. if ((ret = av_new_packet(pkt, data->au_headers[data->cur_au_index].size)) < 0) {
  153. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  154. return ret;
  155. }
  156. memcpy(pkt->data, &data->buf[data->buf_pos], data->au_headers[data->cur_au_index].size);
  157. data->buf_pos += data->au_headers[data->cur_au_index].size;
  158. pkt->stream_index = st->index;
  159. data->cur_au_index++;
  160. if (data->cur_au_index == data->nb_au_headers) {
  161. data->buf_pos = 0;
  162. return 0;
  163. }
  164. return 1;
  165. }
  166. if (rtp_parse_mp4_au(data, buf, len)) {
  167. av_log(ctx, AV_LOG_ERROR, "Error parsing AU headers\n");
  168. return -1;
  169. }
  170. buf += data->au_headers_length_bytes + 2;
  171. len -= data->au_headers_length_bytes + 2;
  172. if (data->nb_au_headers == 1 && len < data->au_headers[0].size) {
  173. /* Packet is fragmented */
  174. if (!data->buf_pos) {
  175. if (data->au_headers[0].size > MAX_AAC_HBR_FRAME_SIZE) {
  176. av_log(ctx, AV_LOG_ERROR, "Invalid AU size\n");
  177. return AVERROR_INVALIDDATA;
  178. }
  179. data->buf_size = data->au_headers[0].size;
  180. data->timestamp = *timestamp;
  181. }
  182. if (data->timestamp != *timestamp ||
  183. data->au_headers[0].size != data->buf_size ||
  184. data->buf_pos + len > MAX_AAC_HBR_FRAME_SIZE) {
  185. data->buf_pos = 0;
  186. data->buf_size = 0;
  187. av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
  188. return AVERROR_INVALIDDATA;
  189. }
  190. memcpy(&data->buf[data->buf_pos], buf, len);
  191. data->buf_pos += len;
  192. if (!(flags & RTP_FLAG_MARKER))
  193. return AVERROR(EAGAIN);
  194. if (data->buf_pos != data->buf_size) {
  195. data->buf_pos = 0;
  196. av_log(ctx, AV_LOG_ERROR, "Missed some packets, discarding frame\n");
  197. return AVERROR_INVALIDDATA;
  198. }
  199. data->buf_pos = 0;
  200. ret = av_new_packet(pkt, data->buf_size);
  201. if (ret < 0) {
  202. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  203. return ret;
  204. }
  205. pkt->stream_index = st->index;
  206. memcpy(pkt->data, data->buf, data->buf_size);
  207. return 0;
  208. }
  209. if (len < data->au_headers[0].size) {
  210. av_log(ctx, AV_LOG_ERROR, "First AU larger than packet size\n");
  211. return AVERROR_INVALIDDATA;
  212. }
  213. if ((ret = av_new_packet(pkt, data->au_headers[0].size)) < 0) {
  214. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  215. return ret;
  216. }
  217. memcpy(pkt->data, buf, data->au_headers[0].size);
  218. len -= data->au_headers[0].size;
  219. buf += data->au_headers[0].size;
  220. pkt->stream_index = st->index;
  221. if (len > 0 && data->nb_au_headers > 1) {
  222. data->buf_size = FFMIN(len, sizeof(data->buf));
  223. memcpy(data->buf, buf, data->buf_size);
  224. data->cur_au_index = 1;
  225. data->buf_pos = 0;
  226. return 1;
  227. }
  228. return 0;
  229. }
  230. static int parse_fmtp(AVFormatContext *s,
  231. AVStream *stream, PayloadContext *data,
  232. const char *attr, const char *value)
  233. {
  234. AVCodecContext *codec = stream->codec;
  235. int res, i;
  236. if (!strcmp(attr, "config")) {
  237. res = parse_fmtp_config(codec, value);
  238. if (res < 0)
  239. return res;
  240. }
  241. if (codec->codec_id == AV_CODEC_ID_AAC) {
  242. /* Looking for a known attribute */
  243. for (i = 0; attr_names[i].str; ++i) {
  244. if (!av_strcasecmp(attr, attr_names[i].str)) {
  245. if (attr_names[i].type == ATTR_NAME_TYPE_INT) {
  246. *(int *)((char *)data+
  247. attr_names[i].offset) = atoi(value);
  248. } else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
  249. *(char **)((char *)data+
  250. attr_names[i].offset) = av_strdup(value);
  251. }
  252. }
  253. }
  254. return 0;
  255. }
  256. static int parse_sdp_line(AVFormatContext *s, int st_index,
  257. PayloadContext *data, const char *line)
  258. {
  259. const char *p;
  260. if (st_index < 0)
  261. return 0;
  262. if (av_strstart(line, "fmtp:", &p))
  263. return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp);
  264. return 0;
  265. }
  266. RTPDynamicProtocolHandler ff_mp4v_es_dynamic_handler = {
  267. .enc_name = "MP4V-ES",
  268. .codec_type = AVMEDIA_TYPE_VIDEO,
  269. .codec_id = AV_CODEC_ID_MPEG4,
  270. .need_parsing = AVSTREAM_PARSE_FULL,
  271. .priv_data_size = sizeof(PayloadContext),
  272. .parse_sdp_a_line = parse_sdp_line,
  273. };
  274. RTPDynamicProtocolHandler ff_mpeg4_generic_dynamic_handler = {
  275. .enc_name = "mpeg4-generic",
  276. .codec_type = AVMEDIA_TYPE_AUDIO,
  277. .codec_id = AV_CODEC_ID_AAC,
  278. .priv_data_size = sizeof(PayloadContext),
  279. .parse_sdp_a_line = parse_sdp_line,
  280. .close = close_context,
  281. .parse_packet = aac_parse_packet,
  282. };