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.

367 lines
12KB

  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 MPEG-4 / 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. /** Range for integer values */
  67. struct Range {
  68. int min;
  69. int max;
  70. } range;
  71. } AttrNameMap;
  72. /* All known fmtp parameters and the corresponding RTPAttrTypeEnum */
  73. #define ATTR_NAME_TYPE_INT 0
  74. #define ATTR_NAME_TYPE_STR 1
  75. static const AttrNameMap attr_names[] = {
  76. { "SizeLength", ATTR_NAME_TYPE_INT,
  77. offsetof(PayloadContext, sizelength),
  78. {0, 32} }, // SizeLength number of bits used to encode AU-size integer value
  79. { "IndexLength", ATTR_NAME_TYPE_INT,
  80. offsetof(PayloadContext, indexlength),
  81. {0, 32} }, // IndexLength number of bits used to encode AU-Index integer value
  82. { "IndexDeltaLength", ATTR_NAME_TYPE_INT,
  83. offsetof(PayloadContext, indexdeltalength),
  84. {0, 32} }, // IndexDeltaLength number of bits to encode AU-Index-delta integer value
  85. { "profile-level-id", ATTR_NAME_TYPE_INT,
  86. offsetof(PayloadContext, profile_level_id),
  87. {INT32_MIN, INT32_MAX} }, // It differs depending on StreamType
  88. { "StreamType", ATTR_NAME_TYPE_INT,
  89. offsetof(PayloadContext, streamtype),
  90. {0x00, 0x3F} }, // Values from ISO/IEC 14496-1, 'StreamType Values' table
  91. { "mode", ATTR_NAME_TYPE_STR,
  92. offsetof(PayloadContext, mode),
  93. {0} },
  94. { NULL, -1, -1, {0} },
  95. };
  96. static void close_context(PayloadContext *data)
  97. {
  98. av_freep(&data->au_headers);
  99. av_freep(&data->mode);
  100. }
  101. static int parse_fmtp_config(AVCodecParameters *par, const char *value)
  102. {
  103. /* decode the hexa encoded parameter */
  104. int len = ff_hex_to_data(NULL, value), ret;
  105. if ((ret = ff_alloc_extradata(par, len)) < 0)
  106. return ret;
  107. ff_hex_to_data(par->extradata, value);
  108. return 0;
  109. }
  110. static int rtp_parse_mp4_au(PayloadContext *data, const uint8_t *buf, int len)
  111. {
  112. int au_headers_length, au_header_size, i;
  113. GetBitContext getbitcontext;
  114. if (len < 2)
  115. return AVERROR_INVALIDDATA;
  116. /* decode the first 2 bytes where the AUHeader sections are stored
  117. length in bits */
  118. au_headers_length = AV_RB16(buf);
  119. if (au_headers_length > RTP_MAX_PACKET_LENGTH)
  120. return -1;
  121. data->au_headers_length_bytes = (au_headers_length + 7) / 8;
  122. /* skip AU headers length section (2 bytes) */
  123. buf += 2;
  124. len -= 2;
  125. if (len < data->au_headers_length_bytes)
  126. return AVERROR_INVALIDDATA;
  127. init_get_bits(&getbitcontext, buf, data->au_headers_length_bytes * 8);
  128. /* XXX: Wrong if optional additional sections are present (cts, dts etc...) */
  129. au_header_size = data->sizelength + data->indexlength;
  130. if (au_header_size <= 0 || (au_headers_length % au_header_size != 0))
  131. return -1;
  132. data->nb_au_headers = au_headers_length / au_header_size;
  133. if (!data->au_headers || data->au_headers_allocated < data->nb_au_headers) {
  134. av_free(data->au_headers);
  135. data->au_headers = av_malloc(sizeof(struct AUHeaders) * data->nb_au_headers);
  136. if (!data->au_headers)
  137. return AVERROR(ENOMEM);
  138. data->au_headers_allocated = data->nb_au_headers;
  139. }
  140. for (i = 0; i < data->nb_au_headers; ++i) {
  141. data->au_headers[i].size = get_bits_long(&getbitcontext, data->sizelength);
  142. data->au_headers[i].index = get_bits_long(&getbitcontext, data->indexlength);
  143. }
  144. return 0;
  145. }
  146. /* Follows RFC 3640 */
  147. static int aac_parse_packet(AVFormatContext *ctx, PayloadContext *data,
  148. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  149. const uint8_t *buf, int len, uint16_t seq,
  150. int flags)
  151. {
  152. int ret;
  153. if (!buf) {
  154. if (data->cur_au_index > data->nb_au_headers) {
  155. av_log(ctx, AV_LOG_ERROR, "Invalid parser state\n");
  156. return AVERROR_INVALIDDATA;
  157. }
  158. if (data->buf_size - data->buf_pos < data->au_headers[data->cur_au_index].size) {
  159. av_log(ctx, AV_LOG_ERROR, "Invalid AU size\n");
  160. return AVERROR_INVALIDDATA;
  161. }
  162. if ((ret = av_new_packet(pkt, data->au_headers[data->cur_au_index].size)) < 0) {
  163. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  164. return ret;
  165. }
  166. memcpy(pkt->data, &data->buf[data->buf_pos], data->au_headers[data->cur_au_index].size);
  167. data->buf_pos += data->au_headers[data->cur_au_index].size;
  168. pkt->stream_index = st->index;
  169. data->cur_au_index++;
  170. if (data->cur_au_index == data->nb_au_headers) {
  171. data->buf_pos = 0;
  172. return 0;
  173. }
  174. return 1;
  175. }
  176. if (rtp_parse_mp4_au(data, buf, len)) {
  177. av_log(ctx, AV_LOG_ERROR, "Error parsing AU headers\n");
  178. return -1;
  179. }
  180. buf += data->au_headers_length_bytes + 2;
  181. len -= data->au_headers_length_bytes + 2;
  182. if (data->nb_au_headers == 1 && len < data->au_headers[0].size) {
  183. /* Packet is fragmented */
  184. if (!data->buf_pos) {
  185. if (data->au_headers[0].size > MAX_AAC_HBR_FRAME_SIZE) {
  186. av_log(ctx, AV_LOG_ERROR, "Invalid AU size\n");
  187. return AVERROR_INVALIDDATA;
  188. }
  189. data->buf_size = data->au_headers[0].size;
  190. data->timestamp = *timestamp;
  191. }
  192. if (data->timestamp != *timestamp ||
  193. data->au_headers[0].size != data->buf_size ||
  194. data->buf_pos + len > MAX_AAC_HBR_FRAME_SIZE) {
  195. data->buf_pos = 0;
  196. data->buf_size = 0;
  197. av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
  198. return AVERROR_INVALIDDATA;
  199. }
  200. memcpy(&data->buf[data->buf_pos], buf, len);
  201. data->buf_pos += len;
  202. if (!(flags & RTP_FLAG_MARKER))
  203. return AVERROR(EAGAIN);
  204. if (data->buf_pos != data->buf_size) {
  205. data->buf_pos = 0;
  206. av_log(ctx, AV_LOG_ERROR, "Missed some packets, discarding frame\n");
  207. return AVERROR_INVALIDDATA;
  208. }
  209. data->buf_pos = 0;
  210. ret = av_new_packet(pkt, data->buf_size);
  211. if (ret < 0) {
  212. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  213. return ret;
  214. }
  215. pkt->stream_index = st->index;
  216. memcpy(pkt->data, data->buf, data->buf_size);
  217. return 0;
  218. }
  219. if (len < data->au_headers[0].size) {
  220. av_log(ctx, AV_LOG_ERROR, "First AU larger than packet size\n");
  221. return AVERROR_INVALIDDATA;
  222. }
  223. if ((ret = av_new_packet(pkt, data->au_headers[0].size)) < 0) {
  224. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  225. return ret;
  226. }
  227. memcpy(pkt->data, buf, data->au_headers[0].size);
  228. len -= data->au_headers[0].size;
  229. buf += data->au_headers[0].size;
  230. pkt->stream_index = st->index;
  231. if (len > 0 && data->nb_au_headers > 1) {
  232. data->buf_size = FFMIN(len, sizeof(data->buf));
  233. memcpy(data->buf, buf, data->buf_size);
  234. data->cur_au_index = 1;
  235. data->buf_pos = 0;
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. static int parse_fmtp(AVFormatContext *s,
  241. AVStream *stream, PayloadContext *data,
  242. const char *attr, const char *value)
  243. {
  244. AVCodecParameters *par = stream->codecpar;
  245. int res, i;
  246. if (!strcmp(attr, "config")) {
  247. res = parse_fmtp_config(par, value);
  248. if (res < 0)
  249. return res;
  250. }
  251. if (par->codec_id == AV_CODEC_ID_AAC) {
  252. /* Looking for a known attribute */
  253. for (i = 0; attr_names[i].str; ++i) {
  254. if (!av_strcasecmp(attr, attr_names[i].str)) {
  255. if (attr_names[i].type == ATTR_NAME_TYPE_INT) {
  256. char *end_ptr = NULL;
  257. long long int val = strtoll(value, &end_ptr, 10);
  258. if (end_ptr == value || end_ptr[0] != '\0') {
  259. av_log(s, AV_LOG_ERROR,
  260. "The %s field value is not a valid number: %s\n",
  261. attr, value);
  262. return AVERROR_INVALIDDATA;
  263. }
  264. if (val < attr_names[i].range.min ||
  265. val > attr_names[i].range.max) {
  266. av_log(s, AV_LOG_ERROR,
  267. "fmtp field %s should be in range [%d,%d] (provided value: %lld)",
  268. attr, attr_names[i].range.min, attr_names[i].range.max, val);
  269. return AVERROR_INVALIDDATA;
  270. }
  271. *(int *)((char *)data+
  272. attr_names[i].offset) = (int) val;
  273. } else if (attr_names[i].type == ATTR_NAME_TYPE_STR) {
  274. char *val = av_strdup(value);
  275. if (!val)
  276. return AVERROR(ENOMEM);
  277. *(char **)((char *)data+
  278. attr_names[i].offset) = val;
  279. }
  280. }
  281. }
  282. }
  283. return 0;
  284. }
  285. static int parse_sdp_line(AVFormatContext *s, int st_index,
  286. PayloadContext *data, const char *line)
  287. {
  288. const char *p;
  289. if (st_index < 0)
  290. return 0;
  291. if (av_strstart(line, "fmtp:", &p))
  292. return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp);
  293. return 0;
  294. }
  295. const RTPDynamicProtocolHandler ff_mp4v_es_dynamic_handler = {
  296. .enc_name = "MP4V-ES",
  297. .codec_type = AVMEDIA_TYPE_VIDEO,
  298. .codec_id = AV_CODEC_ID_MPEG4,
  299. .need_parsing = AVSTREAM_PARSE_FULL,
  300. .priv_data_size = sizeof(PayloadContext),
  301. .parse_sdp_a_line = parse_sdp_line,
  302. };
  303. const RTPDynamicProtocolHandler ff_mpeg4_generic_dynamic_handler = {
  304. .enc_name = "mpeg4-generic",
  305. .codec_type = AVMEDIA_TYPE_AUDIO,
  306. .codec_id = AV_CODEC_ID_AAC,
  307. .priv_data_size = sizeof(PayloadContext),
  308. .parse_sdp_a_line = parse_sdp_line,
  309. .close = close_context,
  310. .parse_packet = aac_parse_packet,
  311. };