rtpdec_qt.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * RTP/Quicktime support.
  3. * Copyright (c) 2009 Ronald S. Bultje
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * @brief Quicktime-style RTP support
  24. * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
  25. */
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include "avio_internal.h"
  29. #include "rtp.h"
  30. #include "rtpdec.h"
  31. #include "isom.h"
  32. #include "libavcodec/get_bits.h"
  33. struct PayloadContext {
  34. AVPacket pkt;
  35. int bytes_per_frame, remaining;
  36. uint32_t timestamp;
  37. };
  38. static int qt_rtp_parse_packet(AVFormatContext *s, PayloadContext *qt,
  39. AVStream *st, AVPacket *pkt,
  40. uint32_t *timestamp, const uint8_t *buf,
  41. int len, int flags)
  42. {
  43. AVIOContext pb;
  44. GetBitContext gb;
  45. int packing_scheme, has_payload_desc, has_packet_info, alen,
  46. has_marker_bit = flags & RTP_FLAG_MARKER;
  47. if (qt->remaining) {
  48. int num = qt->pkt.size / qt->bytes_per_frame;
  49. if (av_new_packet(pkt, qt->bytes_per_frame))
  50. return AVERROR(ENOMEM);
  51. pkt->stream_index = st->index;
  52. pkt->flags = qt->pkt.flags;
  53. memcpy(pkt->data,
  54. &qt->pkt.data[(num - qt->remaining) * qt->bytes_per_frame],
  55. qt->bytes_per_frame);
  56. if (--qt->remaining == 0) {
  57. av_freep(&qt->pkt.data);
  58. qt->pkt.size = 0;
  59. }
  60. return qt->remaining > 0;
  61. }
  62. /**
  63. * The RTP payload is described in:
  64. * http://developer.apple.com/quicktime/icefloe/dispatch026.html
  65. */
  66. init_get_bits(&gb, buf, len << 3);
  67. ffio_init_context(&pb, buf, len, 0, NULL, NULL, NULL, NULL);
  68. if (len < 4)
  69. return AVERROR_INVALIDDATA;
  70. skip_bits(&gb, 4); // version
  71. if ((packing_scheme = get_bits(&gb, 2)) == 0)
  72. return AVERROR_INVALIDDATA;
  73. if (get_bits1(&gb))
  74. flags |= RTP_FLAG_KEY;
  75. has_payload_desc = get_bits1(&gb);
  76. has_packet_info = get_bits1(&gb);
  77. skip_bits(&gb, 23); // reserved:7, cache payload info:1, payload ID:15
  78. if (has_payload_desc) {
  79. int data_len, pos, is_start, is_finish;
  80. uint32_t tag;
  81. pos = get_bits_count(&gb) >> 3;
  82. if (pos + 12 > len)
  83. return AVERROR_INVALIDDATA;
  84. skip_bits(&gb, 2); // has non-I frames:1, is sparse:1
  85. is_start = get_bits1(&gb);
  86. is_finish = get_bits1(&gb);
  87. if (!is_start || !is_finish) {
  88. av_log_missing_feature(s, "RTP-X-QT with payload description "
  89. "split over several packets", 1);
  90. return AVERROR(ENOSYS);
  91. }
  92. skip_bits(&gb, 12); // reserved
  93. data_len = get_bits(&gb, 16);
  94. avio_seek(&pb, pos + 4, SEEK_SET);
  95. tag = avio_rl32(&pb);
  96. if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  97. tag != MKTAG('v','i','d','e')) ||
  98. (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  99. tag != MKTAG('s','o','u','n')))
  100. return AVERROR_INVALIDDATA;
  101. avpriv_set_pts_info(st, 32, 1, avio_rb32(&pb));
  102. if (pos + data_len > len)
  103. return AVERROR_INVALIDDATA;
  104. /* TLVs */
  105. while (avio_tell(&pb) + 4 < pos + data_len) {
  106. int tlv_len = avio_rb16(&pb);
  107. tag = avio_rl16(&pb);
  108. if (avio_tell(&pb) + tlv_len > pos + data_len)
  109. return AVERROR_INVALIDDATA;
  110. #define MKTAG16(a,b) MKTAG(a,b,0,0)
  111. switch (tag) {
  112. case MKTAG16('s','d'): {
  113. MOVStreamContext *msc;
  114. void *priv_data = st->priv_data;
  115. int nb_streams = s->nb_streams;
  116. MOVContext *mc = av_mallocz(sizeof(*mc));
  117. if (!mc)
  118. return AVERROR(ENOMEM);
  119. mc->fc = s;
  120. st->priv_data = msc = av_mallocz(sizeof(MOVStreamContext));
  121. if (!msc) {
  122. av_free(mc);
  123. st->priv_data = priv_data;
  124. return AVERROR(ENOMEM);
  125. }
  126. /* ff_mov_read_stsd_entries updates stream s->nb_streams-1,
  127. * so set it temporarily to indicate which stream to update. */
  128. s->nb_streams = st->index + 1;
  129. ff_mov_read_stsd_entries(mc, &pb, 1);
  130. qt->bytes_per_frame = msc->bytes_per_frame;
  131. av_free(msc);
  132. av_free(mc);
  133. st->priv_data = priv_data;
  134. s->nb_streams = nb_streams;
  135. break;
  136. }
  137. default:
  138. avio_skip(&pb, tlv_len);
  139. break;
  140. }
  141. }
  142. /* 32-bit alignment */
  143. avio_skip(&pb, ((avio_tell(&pb) + 3) & ~3) - avio_tell(&pb));
  144. } else
  145. avio_seek(&pb, 4, SEEK_SET);
  146. if (has_packet_info) {
  147. av_log_missing_feature(s, "RTP-X-QT with packet specific info", 1);
  148. return AVERROR(ENOSYS);
  149. }
  150. alen = len - avio_tell(&pb);
  151. if (alen <= 0)
  152. return AVERROR_INVALIDDATA;
  153. switch (packing_scheme) {
  154. case 3: /* one data packet spread over 1 or multiple RTP packets */
  155. if (qt->pkt.size > 0 && qt->timestamp == *timestamp) {
  156. qt->pkt.data = av_realloc(qt->pkt.data, qt->pkt.size + alen +
  157. FF_INPUT_BUFFER_PADDING_SIZE);
  158. } else {
  159. av_freep(&qt->pkt.data);
  160. av_init_packet(&qt->pkt);
  161. qt->pkt.data = av_malloc(alen + FF_INPUT_BUFFER_PADDING_SIZE);
  162. qt->pkt.size = 0;
  163. qt->timestamp = *timestamp;
  164. }
  165. if (!qt->pkt.data)
  166. return AVERROR(ENOMEM);
  167. memcpy(qt->pkt.data + qt->pkt.size, buf + avio_tell(&pb), alen);
  168. qt->pkt.size += alen;
  169. if (has_marker_bit) {
  170. *pkt = qt->pkt;
  171. qt->pkt.size = 0;
  172. qt->pkt.data = NULL;
  173. pkt->flags = flags & RTP_FLAG_KEY ? AV_PKT_FLAG_KEY : 0;
  174. pkt->stream_index = st->index;
  175. pkt->destruct = av_destruct_packet;
  176. memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  177. return 0;
  178. }
  179. return AVERROR(EAGAIN);
  180. case 1: /* constant packet size, multiple packets per RTP packet */
  181. if (qt->bytes_per_frame == 0 ||
  182. alen % qt->bytes_per_frame != 0)
  183. return AVERROR_INVALIDDATA; /* wrongly padded */
  184. qt->remaining = (alen / qt->bytes_per_frame) - 1;
  185. if (av_new_packet(pkt, qt->bytes_per_frame))
  186. return AVERROR(ENOMEM);
  187. memcpy(pkt->data, buf + avio_tell(&pb), qt->bytes_per_frame);
  188. pkt->flags = flags & RTP_FLAG_KEY ? AV_PKT_FLAG_KEY : 0;
  189. pkt->stream_index = st->index;
  190. if (qt->remaining > 0) {
  191. av_freep(&qt->pkt.data);
  192. qt->pkt.data = av_malloc(qt->remaining * qt->bytes_per_frame);
  193. if (!qt->pkt.data) {
  194. av_free_packet(pkt);
  195. return AVERROR(ENOMEM);
  196. }
  197. qt->pkt.size = qt->remaining * qt->bytes_per_frame;
  198. memcpy(qt->pkt.data,
  199. buf + avio_tell(&pb) + qt->bytes_per_frame,
  200. qt->remaining * qt->bytes_per_frame);
  201. qt->pkt.flags = pkt->flags;
  202. return 1;
  203. }
  204. return 0;
  205. default: /* unimplemented */
  206. av_log_missing_feature(NULL, "RTP-X-QT with packing scheme 2", 1);
  207. return AVERROR(ENOSYS);
  208. }
  209. }
  210. static PayloadContext *qt_rtp_new(void)
  211. {
  212. return av_mallocz(sizeof(PayloadContext));
  213. }
  214. static void qt_rtp_free(PayloadContext *qt)
  215. {
  216. av_freep(&qt->pkt.data);
  217. av_free(qt);
  218. }
  219. #define RTP_QT_HANDLER(m, n, s, t) \
  220. RTPDynamicProtocolHandler ff_ ## m ## _rtp_ ## n ## _handler = { \
  221. .enc_name = s, \
  222. .codec_type = t, \
  223. .codec_id = CODEC_ID_NONE, \
  224. .alloc = qt_rtp_new, \
  225. .free = qt_rtp_free, \
  226. .parse_packet = qt_rtp_parse_packet, \
  227. }
  228. RTP_QT_HANDLER(qt, vid, "X-QT", AVMEDIA_TYPE_VIDEO);
  229. RTP_QT_HANDLER(qt, aud, "X-QT", AVMEDIA_TYPE_AUDIO);
  230. RTP_QT_HANDLER(quicktime, vid, "X-QUICKTIME", AVMEDIA_TYPE_VIDEO);
  231. RTP_QT_HANDLER(quicktime, aud, "X-QUICKTIME", AVMEDIA_TYPE_AUDIO);