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.

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