rtpdec_vp8.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * RTP VP8 Depacketizer
  3. * Copyright (c) 2010 Josh Allmann
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * @brief RTP support for the VP8 payload
  24. * @author Josh Allmann <joshua.allmann@gmail.com>
  25. * @see http://www.webmproject.org/code/specs/rtp/
  26. */
  27. #include "libavcodec/bytestream.h"
  28. #include "rtpdec_formats.h"
  29. struct PayloadContext {
  30. AVIOContext *data;
  31. uint32_t timestamp;
  32. int is_keyframe;
  33. };
  34. static void prepare_packet(AVPacket *pkt, PayloadContext *vp8, int stream)
  35. {
  36. av_init_packet(pkt);
  37. pkt->stream_index = stream;
  38. pkt->flags = vp8->is_keyframe ? AV_PKT_FLAG_KEY : 0;
  39. pkt->size = avio_close_dyn_buf(vp8->data, &pkt->data);
  40. pkt->destruct = av_destruct_packet;
  41. vp8->data = NULL;
  42. }
  43. static int vp8_handle_packet(AVFormatContext *ctx,
  44. PayloadContext *vp8,
  45. AVStream *st,
  46. AVPacket *pkt,
  47. uint32_t *timestamp,
  48. const uint8_t *buf,
  49. int len, int flags)
  50. {
  51. int start_packet, end_packet, has_au, ret = AVERROR(EAGAIN);
  52. if (!buf) {
  53. // only called when vp8_handle_packet returns 1
  54. if (!vp8->data) {
  55. av_log(ctx, AV_LOG_ERROR, "Invalid VP8 data passed\n");
  56. return AVERROR_INVALIDDATA;
  57. }
  58. prepare_packet(pkt, vp8, st->index);
  59. *timestamp = vp8->timestamp;
  60. return 0;
  61. }
  62. start_packet = *buf & 1;
  63. end_packet = flags & RTP_FLAG_MARKER;
  64. has_au = *buf & 2;
  65. buf++;
  66. len--;
  67. if (start_packet) {
  68. int res;
  69. uint32_t ts = *timestamp;
  70. if (vp8->data) {
  71. // missing end marker; return old frame anyway. untested
  72. prepare_packet(pkt, vp8, st->index);
  73. *timestamp = vp8->timestamp; // reset timestamp from old frame
  74. // if current frame fits into one rtp packet, need to hold
  75. // that for the next av_get_packet call
  76. ret = end_packet ? 1 : 0;
  77. }
  78. if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
  79. return res;
  80. vp8->is_keyframe = *buf & 1;
  81. vp8->timestamp = ts;
  82. }
  83. if (!vp8->data || vp8->timestamp != *timestamp && ret == AVERROR(EAGAIN)) {
  84. av_log(ctx, AV_LOG_WARNING,
  85. "Received no start marker; dropping frame\n");
  86. return AVERROR(EAGAIN);
  87. }
  88. // cycle through VP8AU headers if needed
  89. // not tested with actual VP8AUs
  90. while (len) {
  91. int au_len = len;
  92. if (has_au && len > 2) {
  93. au_len = AV_RB16(buf);
  94. buf += 2;
  95. len -= 2;
  96. if (buf + au_len > buf + len) {
  97. av_log(ctx, AV_LOG_ERROR, "Invalid VP8AU length\n");
  98. return AVERROR_INVALIDDATA;
  99. }
  100. }
  101. avio_write(vp8->data, buf, au_len);
  102. buf += au_len;
  103. len -= au_len;
  104. }
  105. if (ret != AVERROR(EAGAIN)) // did we miss a end marker?
  106. return ret;
  107. if (end_packet) {
  108. prepare_packet(pkt, vp8, st->index);
  109. return 0;
  110. }
  111. return AVERROR(EAGAIN);
  112. }
  113. static PayloadContext *vp8_new_context(void)
  114. {
  115. av_log(NULL, AV_LOG_ERROR, "RTP VP8 payload implementation is incompatible "
  116. "with the latest spec drafts.\n");
  117. return av_mallocz(sizeof(PayloadContext));
  118. }
  119. static void vp8_free_context(PayloadContext *vp8)
  120. {
  121. if (vp8->data) {
  122. uint8_t *tmp;
  123. avio_close_dyn_buf(vp8->data, &tmp);
  124. av_free(tmp);
  125. }
  126. av_free(vp8);
  127. }
  128. RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
  129. .enc_name = "VP8",
  130. .codec_type = AVMEDIA_TYPE_VIDEO,
  131. .codec_id = CODEC_ID_VP8,
  132. .alloc = vp8_new_context,
  133. .free = vp8_free_context,
  134. .parse_packet = vp8_handle_packet,
  135. };