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.

278 lines
8.1KB

  1. /*
  2. * Common code for the RTP depacketization of MPEG-1/2 formats.
  3. * Copyright (c) 2002 Fabrice Bellard
  4. *
  5. * RTP parser for loss tolerant payload format for MP3 audio (RFC 5219)
  6. * Copyright (c) 2015 Gilles Chanteperdrix <gch@xenomai.org>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "rtpdec_formats.h"
  27. #define RTP_MPA_PAYLOAD_HEADER_SIZE 1
  28. static av_cold int mpeg_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
  29. {
  30. if (st_index < 0)
  31. return 0;
  32. ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
  33. return 0;
  34. }
  35. static int mpeg_parse_packet(AVFormatContext *ctx, PayloadContext *data,
  36. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  37. const uint8_t *buf, int len, uint16_t seq,
  38. int flags)
  39. {
  40. unsigned int h;
  41. if (len <= 4)
  42. return AVERROR_INVALIDDATA;
  43. h = AV_RB32(buf);
  44. buf += 4;
  45. len -= 4;
  46. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && h & (1 << 26)) {
  47. /* MPEG-2 */
  48. if (len <= 4)
  49. return AVERROR_INVALIDDATA;
  50. buf += 4;
  51. len -= 4;
  52. }
  53. if (av_new_packet(pkt, len) < 0)
  54. return AVERROR(ENOMEM);
  55. memcpy(pkt->data, buf, len);
  56. pkt->stream_index = st->index;
  57. return 0;
  58. }
  59. RTPDynamicProtocolHandler ff_mpeg_audio_dynamic_handler = {
  60. .codec_type = AVMEDIA_TYPE_AUDIO,
  61. .codec_id = AV_CODEC_ID_MP3,
  62. .init = mpeg_init,
  63. .parse_packet = mpeg_parse_packet,
  64. .static_payload_id = 14,
  65. };
  66. RTPDynamicProtocolHandler ff_mpeg_video_dynamic_handler = {
  67. .codec_type = AVMEDIA_TYPE_VIDEO,
  68. .codec_id = AV_CODEC_ID_MPEG2VIDEO,
  69. .init = mpeg_init,
  70. .parse_packet = mpeg_parse_packet,
  71. .static_payload_id = 32,
  72. };
  73. /* MPA-ROBUST, RFC 5219 */
  74. struct PayloadContext {
  75. unsigned adu_size;
  76. unsigned cur_size;
  77. uint32_t timestamp;
  78. uint8_t *split_buf;
  79. int split_pos, split_buf_size, split_pkts;
  80. AVIOContext *fragment;
  81. };
  82. static av_cold int mpa_robust_init(AVFormatContext *ctx, int st_index,
  83. PayloadContext *data)
  84. {
  85. if (st_index < 0)
  86. return 0;
  87. ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_HEADERS;
  88. return 0;
  89. }
  90. static PayloadContext *mpa_robust_new_context(void)
  91. {
  92. return av_mallocz(sizeof(PayloadContext));
  93. }
  94. static inline void free_fragment_if_needed(PayloadContext *data)
  95. {
  96. if (data->fragment) {
  97. uint8_t *p;
  98. avio_close_dyn_buf(data->fragment, &p);
  99. av_free(p);
  100. data->fragment = NULL;
  101. }
  102. }
  103. static void mpa_robust_free_context(PayloadContext *data)
  104. {
  105. free_fragment_if_needed(data);
  106. av_free(data);
  107. }
  108. static int mpa_robust_parse_rtp_header(AVFormatContext *ctx,
  109. const uint8_t *buf, int len,
  110. unsigned *adu_size, unsigned *cont)
  111. {
  112. unsigned header_size;
  113. if (len < RTP_MPA_PAYLOAD_HEADER_SIZE + 1) {
  114. av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
  115. return AVERROR_INVALIDDATA;
  116. }
  117. *cont = !!(buf[0] & 0x80);
  118. if (!(buf[0] & 0x40)) {
  119. header_size = 1;
  120. *adu_size = buf[0] & ~0xc0;
  121. } else {
  122. header_size = 2;
  123. *adu_size = AV_RB16(buf) & ~0xc000;
  124. }
  125. return header_size;
  126. }
  127. static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data,
  128. AVStream *st, AVPacket *pkt,
  129. uint32_t *timestamp, const uint8_t *buf,
  130. int len, uint16_t seq, int flags)
  131. {
  132. unsigned adu_size, continuation;
  133. int err, header_size;
  134. if (!buf) {
  135. buf = &data->split_buf[data->split_pos];
  136. len = data->split_buf_size - data->split_pos;
  137. header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
  138. &continuation);
  139. if (header_size < 0) {
  140. av_freep(&data->split_buf);
  141. return header_size;
  142. }
  143. buf += header_size;
  144. len -= header_size;
  145. if (continuation || adu_size > len) {
  146. av_freep(&data->split_buf);
  147. av_log(ctx, AV_LOG_ERROR, "Invalid frame\n");
  148. return AVERROR_INVALIDDATA;
  149. }
  150. if (av_new_packet(pkt, adu_size)) {
  151. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  152. return AVERROR(ENOMEM);
  153. }
  154. pkt->stream_index = st->index;
  155. memcpy(pkt->data, buf, adu_size);
  156. data->split_pos = (buf - data->split_buf) + adu_size;
  157. if (data->split_pos == data->split_buf_size) {
  158. av_freep(&data->split_buf);
  159. return 0;
  160. }
  161. return 1;
  162. }
  163. header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
  164. &continuation);
  165. if (header_size < 0)
  166. return header_size;
  167. buf += header_size;
  168. len -= header_size;
  169. if (!continuation && adu_size <= len) {
  170. /* One or more complete frames */
  171. if (av_new_packet(pkt, adu_size)) {
  172. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  173. return AVERROR(ENOMEM);
  174. }
  175. pkt->stream_index = st->index;
  176. memcpy(pkt->data, buf, adu_size);
  177. buf += adu_size;
  178. len -= adu_size;
  179. if (len) {
  180. data->split_buf_size = len;
  181. data->split_buf = av_malloc(data->split_buf_size);
  182. data->split_pos = 0;
  183. if (!data->split_buf) {
  184. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  185. av_free_packet(pkt);
  186. return AVERROR(ENOMEM);
  187. }
  188. memcpy(data->split_buf, buf, data->split_buf_size);
  189. return 1;
  190. }
  191. return 0;
  192. } else if (!continuation) { /* && adu_size > len */
  193. /* First fragment */
  194. free_fragment_if_needed(data);
  195. data->adu_size = adu_size;
  196. data->cur_size = len;
  197. data->timestamp = *timestamp;
  198. err = avio_open_dyn_buf(&data->fragment);
  199. if (err < 0)
  200. return err;
  201. avio_write(data->fragment, buf, len);
  202. return AVERROR(EAGAIN);
  203. }
  204. /* else continuation == 1 */
  205. /* Fragment other than first */
  206. if (!data->fragment) {
  207. av_log(ctx, AV_LOG_WARNING,
  208. "Received packet without a start fragment; dropping.\n");
  209. return AVERROR(EAGAIN);
  210. }
  211. if (adu_size = data->adu_size ||
  212. data->timestamp != *timestamp) {
  213. free_fragment_if_needed(data);
  214. av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
  215. return AVERROR_INVALIDDATA;
  216. }
  217. avio_write(data->fragment, buf, len);
  218. data->cur_size += len;
  219. if (data->cur_size < data->adu_size)
  220. return AVERROR(EAGAIN);
  221. err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
  222. if (err < 0) {
  223. av_log(ctx, AV_LOG_ERROR,
  224. "Error occurred when getting fragment buffer.");
  225. return err;
  226. }
  227. return 0;
  228. }
  229. RTPDynamicProtocolHandler ff_mpeg_audio_robust_dynamic_handler = {
  230. .codec_type = AVMEDIA_TYPE_AUDIO,
  231. .codec_id = AV_CODEC_ID_MP3ADU,
  232. .init = mpa_robust_init,
  233. .alloc = mpa_robust_new_context,
  234. .free = mpa_robust_free_context,
  235. .parse_packet = mpa_robust_parse_packet,
  236. .enc_name = "mpa-robust",
  237. };