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;
  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. return 0;
  86. }
  87. return AVERROR(EAGAIN);
  88. }
  89. if (len < 1)
  90. return AVERROR_INVALIDDATA;
  91. extended_bits = buf[0] & 0x80;
  92. start_partition = buf[0] & 0x10;
  93. part_id = buf[0] & 0x0f;
  94. end_packet = flags & RTP_FLAG_MARKER;
  95. buf++;
  96. len--;
  97. if (extended_bits) {
  98. if (len < 1)
  99. return AVERROR_INVALIDDATA;
  100. pictureid_present = buf[0] & 0x80;
  101. tl0picidx_present = buf[0] & 0x40;
  102. tid_present = buf[0] & 0x20;
  103. keyidx_present = buf[0] & 0x10;
  104. buf++;
  105. len--;
  106. }
  107. if (pictureid_present) {
  108. if (len < 1)
  109. return AVERROR_INVALIDDATA;
  110. if (buf[0] & 0x80) {
  111. if (len < 2)
  112. return AVERROR_INVALIDDATA;
  113. pictureid = AV_RB16(buf) & 0x7fff;
  114. pictureid_mask = 0x7fff;
  115. buf += 2;
  116. len -= 2;
  117. } else {
  118. pictureid = buf[0] & 0x7f;
  119. pictureid_mask = 0x7f;
  120. buf++;
  121. len--;
  122. }
  123. }
  124. if (tl0picidx_present) {
  125. // Ignoring temporal level zero index
  126. buf++;
  127. len--;
  128. }
  129. if (tid_present || keyidx_present) {
  130. // Ignoring temporal layer index, layer sync bit and keyframe index
  131. buf++;
  132. len--;
  133. }
  134. if (len < 1)
  135. return AVERROR_INVALIDDATA;
  136. if (start_partition && part_id == 0 && len >= 3) {
  137. int res;
  138. int non_key = buf[0] & 0x01;
  139. if (!non_key) {
  140. vp8_free_buffer(vp8);
  141. // Keyframe, decoding ok again
  142. vp8->sequence_ok = 1;
  143. vp8->sequence_dirty = 0;
  144. vp8->got_keyframe = 1;
  145. } else {
  146. int can_continue = vp8->data && !vp8->is_keyframe &&
  147. avio_tell(vp8->data) >= vp8->first_part_size;
  148. if (!vp8->sequence_ok)
  149. return AVERROR(EAGAIN);
  150. if (!vp8->got_keyframe)
  151. return vp8_broken_sequence(ctx, vp8, "Keyframe missing\n");
  152. if (pictureid >= 0) {
  153. if (pictureid != ((vp8->prev_pictureid + 1) & pictureid_mask)) {
  154. return vp8_broken_sequence(ctx, vp8,
  155. "Missed a picture, sequence broken\n");
  156. } else {
  157. if (vp8->data && !can_continue)
  158. return vp8_broken_sequence(ctx, vp8,
  159. "Missed a picture, sequence broken\n");
  160. }
  161. } else {
  162. uint16_t expected_seq = vp8->prev_seq + 1;
  163. int16_t diff = seq - expected_seq;
  164. if (vp8->data) {
  165. // No picture id, so we can't know if missed packets
  166. // contained any new frames. If diff == 0, we did get
  167. // later packets from the same frame (matching timestamp),
  168. // so we know we didn't miss any frame. If diff == 1 and
  169. // we still have data (not flushed by the end of frame
  170. // marker), the single missed packet must have been part
  171. // of the same frame.
  172. if ((diff == 0 || diff == 1) && can_continue) {
  173. // Proceed with what we have
  174. } else {
  175. return vp8_broken_sequence(ctx, vp8,
  176. "Missed too much, sequence broken\n");
  177. }
  178. } else {
  179. if (diff != 0)
  180. return vp8_broken_sequence(ctx, vp8,
  181. "Missed unknown data, sequence broken\n");
  182. }
  183. }
  184. if (vp8->data) {
  185. vp8->sequence_dirty = 1;
  186. if (avio_tell(vp8->data) >= vp8->first_part_size) {
  187. int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
  188. if (ret < 0)
  189. return ret;
  190. pkt->size = vp8->first_part_size;
  191. returned_old_frame = 1;
  192. old_timestamp = vp8->timestamp;
  193. } else {
  194. // Shouldn't happen
  195. vp8_free_buffer(vp8);
  196. }
  197. }
  198. }
  199. vp8->first_part_size = (AV_RL16(&buf[1]) << 3 | buf[0] >> 5) + 3;
  200. if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
  201. return res;
  202. vp8->timestamp = *timestamp;
  203. vp8->broken_frame = 0;
  204. vp8->prev_pictureid = pictureid;
  205. vp8->is_keyframe = !non_key;
  206. } else {
  207. uint16_t expected_seq = vp8->prev_seq + 1;
  208. if (!vp8->sequence_ok)
  209. return AVERROR(EAGAIN);
  210. if (vp8->timestamp != *timestamp) {
  211. // Missed the start of the new frame, sequence broken
  212. vp8->sequence_ok = 0;
  213. av_log(ctx, AV_LOG_WARNING,
  214. "Received no start marker; dropping frame\n");
  215. vp8_free_buffer(vp8);
  216. return AVERROR(EAGAIN);
  217. }
  218. if (seq != expected_seq) {
  219. if (vp8->is_keyframe) {
  220. return vp8_broken_sequence(ctx, vp8,
  221. "Missed part of a keyframe, sequence broken\n");
  222. } else if (vp8->data && avio_tell(vp8->data) >= vp8->first_part_size) {
  223. vp8->broken_frame = 1;
  224. vp8->sequence_dirty = 1;
  225. } else {
  226. return vp8_broken_sequence(ctx, vp8,
  227. "Missed part of the first partition, sequence broken\n");
  228. }
  229. }
  230. }
  231. if (!vp8->data)
  232. return vp8_broken_sequence(ctx, vp8, "Received no start marker\n");
  233. vp8->prev_seq = seq;
  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->broken_frame)
  245. pkt->size = vp8->first_part_size;
  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. };