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.

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