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.

289 lines
9.8KB

  1. /*
  2. * RTP VP8 Depacketizer
  3. * Copyright (c) 2010 Josh Allmann
  4. * Copyright (c) 2012 Martin Storsjo
  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 RTP support for the VP8 payload
  25. * @author Josh Allmann <joshua.allmann@gmail.com>
  26. * @see http://tools.ietf.org/html/draft-ietf-payload-vp8-05
  27. */
  28. #include "libavcodec/bytestream.h"
  29. #include "avio_internal.h"
  30. #include "rtpdec_formats.h"
  31. struct PayloadContext {
  32. AVIOContext *data;
  33. uint32_t timestamp;
  34. int is_keyframe;
  35. /* If sequence_ok is set, we keep returning data (even if we might have
  36. * lost some data, but we haven't lost any too critical data that would
  37. * cause the decoder to desynchronize and output random garbage).
  38. */
  39. int sequence_ok;
  40. int first_part_size;
  41. uint16_t prev_seq;
  42. int prev_pictureid;
  43. int broken_frame;
  44. /* If sequence_dirty is set, we have lost some data (critical or
  45. * non-critical) and decoding will have some sort of artifacts, and
  46. * we thus should request a new keyframe.
  47. */
  48. int sequence_dirty;
  49. int got_keyframe;
  50. };
  51. static int vp8_broken_sequence(AVFormatContext *ctx, PayloadContext *vp8,
  52. const char *msg)
  53. {
  54. vp8->sequence_ok = 0;
  55. av_log(ctx, AV_LOG_WARNING, "%s", msg);
  56. ffio_free_dyn_buf(&vp8->data);
  57. return AVERROR(EAGAIN);
  58. }
  59. static int vp8_handle_packet(AVFormatContext *ctx, PayloadContext *vp8,
  60. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  61. const uint8_t *buf, int len, uint16_t seq,
  62. int flags)
  63. {
  64. int start_partition, end_packet;
  65. int extended_bits, part_id;
  66. int pictureid_present = 0, tl0picidx_present = 0, tid_present = 0,
  67. keyidx_present = 0;
  68. int pictureid = -1, pictureid_mask = 0;
  69. int returned_old_frame = 0;
  70. uint32_t old_timestamp = 0;
  71. if (!buf) {
  72. if (vp8->data) {
  73. int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
  74. if (ret < 0)
  75. return ret;
  76. *timestamp = vp8->timestamp;
  77. if (vp8->sequence_dirty)
  78. pkt->flags |= AV_PKT_FLAG_CORRUPT;
  79. return 0;
  80. }
  81. return AVERROR(EAGAIN);
  82. }
  83. if (len < 1)
  84. return AVERROR_INVALIDDATA;
  85. extended_bits = buf[0] & 0x80;
  86. start_partition = buf[0] & 0x10;
  87. part_id = buf[0] & 0x0f;
  88. end_packet = flags & RTP_FLAG_MARKER;
  89. buf++;
  90. len--;
  91. if (extended_bits) {
  92. if (len < 1)
  93. return AVERROR_INVALIDDATA;
  94. pictureid_present = buf[0] & 0x80;
  95. tl0picidx_present = buf[0] & 0x40;
  96. tid_present = buf[0] & 0x20;
  97. keyidx_present = buf[0] & 0x10;
  98. buf++;
  99. len--;
  100. }
  101. if (pictureid_present) {
  102. if (len < 1)
  103. return AVERROR_INVALIDDATA;
  104. if (buf[0] & 0x80) {
  105. if (len < 2)
  106. return AVERROR_INVALIDDATA;
  107. pictureid = AV_RB16(buf) & 0x7fff;
  108. pictureid_mask = 0x7fff;
  109. buf += 2;
  110. len -= 2;
  111. } else {
  112. pictureid = buf[0] & 0x7f;
  113. pictureid_mask = 0x7f;
  114. buf++;
  115. len--;
  116. }
  117. }
  118. if (tl0picidx_present) {
  119. // Ignoring temporal level zero index
  120. buf++;
  121. len--;
  122. }
  123. if (tid_present || keyidx_present) {
  124. // Ignoring temporal layer index, layer sync bit and keyframe index
  125. buf++;
  126. len--;
  127. }
  128. if (len < 1)
  129. return AVERROR_INVALIDDATA;
  130. if (start_partition && part_id == 0 && len >= 3) {
  131. int res;
  132. int non_key = buf[0] & 0x01;
  133. if (!non_key) {
  134. ffio_free_dyn_buf(&vp8->data);
  135. // Keyframe, decoding ok again
  136. vp8->sequence_ok = 1;
  137. vp8->sequence_dirty = 0;
  138. vp8->got_keyframe = 1;
  139. } else {
  140. int can_continue = vp8->data && !vp8->is_keyframe &&
  141. avio_tell(vp8->data) >= vp8->first_part_size;
  142. if (!vp8->sequence_ok)
  143. return AVERROR(EAGAIN);
  144. if (!vp8->got_keyframe)
  145. return vp8_broken_sequence(ctx, vp8, "Keyframe missing\n");
  146. if (pictureid >= 0) {
  147. if (pictureid != ((vp8->prev_pictureid + 1) & pictureid_mask)) {
  148. return vp8_broken_sequence(ctx, vp8,
  149. "Missed a picture, sequence broken\n");
  150. } else {
  151. if (vp8->data && !can_continue)
  152. return vp8_broken_sequence(ctx, vp8,
  153. "Missed a picture, sequence broken\n");
  154. }
  155. } else {
  156. uint16_t expected_seq = vp8->prev_seq + 1;
  157. int16_t diff = seq - expected_seq;
  158. if (vp8->data) {
  159. // No picture id, so we can't know if missed packets
  160. // contained any new frames. If diff == 0, we did get
  161. // later packets from the same frame (matching timestamp),
  162. // so we know we didn't miss any frame. If diff == 1 and
  163. // we still have data (not flushed by the end of frame
  164. // marker), the single missed packet must have been part
  165. // of the same frame.
  166. if ((diff == 0 || diff == 1) && can_continue) {
  167. // Proceed with what we have
  168. } else {
  169. return vp8_broken_sequence(ctx, vp8,
  170. "Missed too much, sequence broken\n");
  171. }
  172. } else {
  173. if (diff != 0)
  174. return vp8_broken_sequence(ctx, vp8,
  175. "Missed unknown data, sequence broken\n");
  176. }
  177. }
  178. if (vp8->data) {
  179. vp8->sequence_dirty = 1;
  180. if (avio_tell(vp8->data) >= vp8->first_part_size) {
  181. int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
  182. if (ret < 0)
  183. return ret;
  184. pkt->flags |= AV_PKT_FLAG_CORRUPT;
  185. returned_old_frame = 1;
  186. old_timestamp = vp8->timestamp;
  187. } else {
  188. // Shouldn't happen
  189. ffio_free_dyn_buf(&vp8->data);
  190. }
  191. }
  192. }
  193. vp8->first_part_size = (AV_RL16(&buf[1]) << 3 | buf[0] >> 5) + 3;
  194. if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
  195. return res;
  196. vp8->timestamp = *timestamp;
  197. vp8->broken_frame = 0;
  198. vp8->prev_pictureid = pictureid;
  199. vp8->is_keyframe = !non_key;
  200. } else {
  201. uint16_t expected_seq = vp8->prev_seq + 1;
  202. if (!vp8->sequence_ok)
  203. return AVERROR(EAGAIN);
  204. if (vp8->timestamp != *timestamp) {
  205. // Missed the start of the new frame, sequence broken
  206. return vp8_broken_sequence(ctx, vp8,
  207. "Received no start marker; dropping frame\n");
  208. }
  209. if (seq != expected_seq) {
  210. if (vp8->is_keyframe) {
  211. return vp8_broken_sequence(ctx, vp8,
  212. "Missed part of a keyframe, sequence broken\n");
  213. } else if (vp8->data && avio_tell(vp8->data) >= vp8->first_part_size) {
  214. vp8->broken_frame = 1;
  215. vp8->sequence_dirty = 1;
  216. } else {
  217. return vp8_broken_sequence(ctx, vp8,
  218. "Missed part of the first partition, sequence broken\n");
  219. }
  220. }
  221. }
  222. if (!vp8->data)
  223. return vp8_broken_sequence(ctx, vp8, "Received no start marker\n");
  224. vp8->prev_seq = seq;
  225. if (!vp8->broken_frame)
  226. avio_write(vp8->data, buf, len);
  227. if (returned_old_frame) {
  228. *timestamp = old_timestamp;
  229. return end_packet ? 1 : 0;
  230. }
  231. if (end_packet) {
  232. int ret;
  233. ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
  234. if (ret < 0)
  235. return ret;
  236. if (vp8->sequence_dirty)
  237. pkt->flags |= AV_PKT_FLAG_CORRUPT;
  238. if (vp8->is_keyframe)
  239. pkt->flags |= AV_PKT_FLAG_KEY;
  240. return 0;
  241. }
  242. return AVERROR(EAGAIN);
  243. }
  244. static av_cold int vp8_init(AVFormatContext *s, int st_index, PayloadContext *vp8)
  245. {
  246. vp8->sequence_ok = 1;
  247. return 0;
  248. }
  249. static void vp8_close_context(PayloadContext *vp8)
  250. {
  251. ffio_free_dyn_buf(&vp8->data);
  252. }
  253. static int vp8_need_keyframe(PayloadContext *vp8)
  254. {
  255. return vp8->sequence_dirty || !vp8->sequence_ok;
  256. }
  257. RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
  258. .enc_name = "VP8",
  259. .codec_type = AVMEDIA_TYPE_VIDEO,
  260. .codec_id = AV_CODEC_ID_VP8,
  261. .priv_data_size = sizeof(PayloadContext),
  262. .init = vp8_init,
  263. .close = vp8_close_context,
  264. .parse_packet = vp8_handle_packet,
  265. .need_keyframe = vp8_need_keyframe,
  266. };