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.

258 lines
8.9KB

  1. /*
  2. * RTP/Quicktime support.
  3. * Copyright (c) 2009 Ronald S. Bultje
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "libavcodec/bitstream.h"
  27. #include "avformat.h"
  28. #include "internal.h"
  29. #include "avio_internal.h"
  30. #include "rtp.h"
  31. #include "rtpdec.h"
  32. #include "isom.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, uint16_t seq, int flags)
  42. {
  43. AVIOContext pb;
  44. BitstreamContext bc;
  45. int packing_scheme, has_payload_desc, has_packet_info, alen,
  46. has_marker_bit = flags & RTP_FLAG_MARKER,
  47. keyframe;
  48. if (qt->remaining) {
  49. int num = qt->pkt.size / qt->bytes_per_frame;
  50. if (av_new_packet(pkt, qt->bytes_per_frame))
  51. return AVERROR(ENOMEM);
  52. pkt->stream_index = st->index;
  53. pkt->flags = qt->pkt.flags;
  54. memcpy(pkt->data,
  55. &qt->pkt.data[(num - qt->remaining) * qt->bytes_per_frame],
  56. qt->bytes_per_frame);
  57. if (--qt->remaining == 0) {
  58. av_freep(&qt->pkt.data);
  59. qt->pkt.size = 0;
  60. }
  61. return qt->remaining > 0;
  62. }
  63. /**
  64. * The RTP payload is described in:
  65. * http://developer.apple.com/quicktime/icefloe/dispatch026.html
  66. */
  67. bitstream_init8(&bc, buf, len);
  68. ffio_init_context(&pb, buf, len, 0, NULL, NULL, NULL, NULL);
  69. if (len < 4)
  70. return AVERROR_INVALIDDATA;
  71. bitstream_skip(&bc, 4); // version
  72. if ((packing_scheme = bitstream_read(&bc, 2)) == 0)
  73. return AVERROR_INVALIDDATA;
  74. keyframe = bitstream_read_bit(&bc);
  75. has_payload_desc = bitstream_read_bit(&bc);
  76. has_packet_info = bitstream_read_bit(&bc);
  77. bitstream_skip(&bc, 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 = bitstream_tell(&bc) >> 3;
  82. if (pos + 12 > len)
  83. return AVERROR_INVALIDDATA;
  84. bitstream_skip(&bc, 2); // has non-I-frames:1, is sparse:1
  85. is_start = bitstream_read_bit(&bc);
  86. is_finish = bitstream_read_bit(&bc);
  87. if (!is_start || !is_finish) {
  88. avpriv_request_sample(s, "RTP-X-QT with payload description "
  89. "split over several packets");
  90. return AVERROR_PATCHWELCOME;
  91. }
  92. bitstream_skip(&bc, 12); // reserved
  93. data_len = bitstream_read(&bc, 16);
  94. avio_seek(&pb, pos + 4, SEEK_SET);
  95. tag = avio_rl32(&pb);
  96. if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  97. tag != MKTAG('v','i','d','e')) ||
  98. (st->codecpar->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. avpriv_request_sample(s, "RTP-X-QT with packet-specific info");
  148. return AVERROR_PATCHWELCOME;
  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. int err;
  157. if ((err = av_reallocp(&qt->pkt.data, qt->pkt.size + alen +
  158. AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
  159. qt->pkt.size = 0;
  160. return err;
  161. }
  162. } else {
  163. av_freep(&qt->pkt.data);
  164. av_init_packet(&qt->pkt);
  165. qt->pkt.data = av_realloc(NULL, alen + AV_INPUT_BUFFER_PADDING_SIZE);
  166. if (!qt->pkt.data)
  167. return AVERROR(ENOMEM);
  168. qt->pkt.size = 0;
  169. qt->timestamp = *timestamp;
  170. }
  171. memcpy(qt->pkt.data + qt->pkt.size, buf + avio_tell(&pb), alen);
  172. qt->pkt.size += alen;
  173. if (has_marker_bit) {
  174. int ret = av_packet_from_data(pkt, qt->pkt.data, qt->pkt.size);
  175. if (ret < 0)
  176. return ret;
  177. qt->pkt.size = 0;
  178. qt->pkt.data = NULL;
  179. pkt->flags = keyframe ? AV_PKT_FLAG_KEY : 0;
  180. pkt->stream_index = st->index;
  181. memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  182. return 0;
  183. }
  184. return AVERROR(EAGAIN);
  185. case 1: /* constant packet size, multiple packets per RTP packet */
  186. if (qt->bytes_per_frame == 0 ||
  187. alen % qt->bytes_per_frame != 0)
  188. return AVERROR_INVALIDDATA; /* wrongly padded */
  189. qt->remaining = (alen / qt->bytes_per_frame) - 1;
  190. if (av_new_packet(pkt, qt->bytes_per_frame))
  191. return AVERROR(ENOMEM);
  192. memcpy(pkt->data, buf + avio_tell(&pb), qt->bytes_per_frame);
  193. pkt->flags = keyframe ? AV_PKT_FLAG_KEY : 0;
  194. pkt->stream_index = st->index;
  195. if (qt->remaining > 0) {
  196. av_freep(&qt->pkt.data);
  197. qt->pkt.data = av_realloc(NULL, qt->remaining * qt->bytes_per_frame);
  198. if (!qt->pkt.data) {
  199. av_packet_unref(pkt);
  200. return AVERROR(ENOMEM);
  201. }
  202. qt->pkt.size = qt->remaining * qt->bytes_per_frame;
  203. memcpy(qt->pkt.data,
  204. buf + avio_tell(&pb) + qt->bytes_per_frame,
  205. qt->remaining * qt->bytes_per_frame);
  206. qt->pkt.flags = pkt->flags;
  207. return 1;
  208. }
  209. return 0;
  210. default: /* unimplemented */
  211. avpriv_request_sample(NULL, "RTP-X-QT with packing scheme 2");
  212. return AVERROR_PATCHWELCOME;
  213. }
  214. }
  215. static void qt_rtp_close(PayloadContext *qt)
  216. {
  217. av_freep(&qt->pkt.data);
  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 = AV_CODEC_ID_NONE, \
  224. .priv_data_size = sizeof(PayloadContext), \
  225. .close = qt_rtp_close, \
  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);