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.

349 lines
11KB

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