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.

143 lines
3.9KB

  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 "rtpdec_formats.h"
  30. struct PayloadContext {
  31. AVIOContext *data;
  32. uint32_t timestamp;
  33. };
  34. static int vp8_handle_packet(AVFormatContext *ctx, PayloadContext *vp8,
  35. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  36. const uint8_t *buf, int len, uint16_t seq,
  37. int flags)
  38. {
  39. int start_partition, end_packet;
  40. int extended_bits, part_id;
  41. int pictureid_present = 0, tl0picidx_present = 0, tid_present = 0,
  42. keyidx_present = 0;
  43. if (len < 1)
  44. return AVERROR_INVALIDDATA;
  45. extended_bits = buf[0] & 0x80;
  46. start_partition = buf[0] & 0x10;
  47. part_id = buf[0] & 0x0f;
  48. end_packet = flags & RTP_FLAG_MARKER;
  49. buf++;
  50. len--;
  51. if (extended_bits) {
  52. if (len < 1)
  53. return AVERROR_INVALIDDATA;
  54. pictureid_present = buf[0] & 0x80;
  55. tl0picidx_present = buf[0] & 0x40;
  56. tid_present = buf[0] & 0x20;
  57. keyidx_present = buf[0] & 0x10;
  58. buf++;
  59. len--;
  60. }
  61. if (pictureid_present) {
  62. int size;
  63. if (len < 1)
  64. return AVERROR_INVALIDDATA;
  65. size = buf[0] & 0x80 ? 2 : 1;
  66. buf += size;
  67. len -= size;
  68. }
  69. if (tl0picidx_present) {
  70. // Ignoring temporal level zero index
  71. buf++;
  72. len--;
  73. }
  74. if (tid_present || keyidx_present) {
  75. // Ignoring temporal layer index, layer sync bit and keyframe index
  76. buf++;
  77. len--;
  78. }
  79. if (len < 1)
  80. return AVERROR_INVALIDDATA;
  81. if (start_partition && part_id == 0) {
  82. int res;
  83. if (vp8->data) {
  84. uint8_t *tmp;
  85. avio_close_dyn_buf(vp8->data, &tmp);
  86. av_free(tmp);
  87. vp8->data = NULL;
  88. }
  89. if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
  90. return res;
  91. vp8->timestamp = *timestamp;
  92. }
  93. if (!vp8->data || vp8->timestamp != *timestamp) {
  94. av_log(ctx, AV_LOG_WARNING,
  95. "Received no start marker; dropping frame\n");
  96. return AVERROR(EAGAIN);
  97. }
  98. avio_write(vp8->data, buf, len);
  99. if (end_packet) {
  100. int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
  101. if (ret < 0)
  102. return ret;
  103. return 0;
  104. }
  105. return AVERROR(EAGAIN);
  106. }
  107. static PayloadContext *vp8_new_context(void)
  108. {
  109. return av_mallocz(sizeof(PayloadContext));
  110. }
  111. static void vp8_free_context(PayloadContext *vp8)
  112. {
  113. if (vp8->data) {
  114. uint8_t *tmp;
  115. avio_close_dyn_buf(vp8->data, &tmp);
  116. av_free(tmp);
  117. }
  118. av_free(vp8);
  119. }
  120. RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
  121. .enc_name = "VP8",
  122. .codec_type = AVMEDIA_TYPE_VIDEO,
  123. .codec_id = AV_CODEC_ID_VP8,
  124. .alloc = vp8_new_context,
  125. .free = vp8_free_context,
  126. .parse_packet = vp8_handle_packet,
  127. };