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.

196 lines
6.3KB

  1. /*
  2. * RTP H.263 Depacketizer, RFC 2190
  3. * Copyright (c) 2012 Martin Storsjo
  4. * Based on the GStreamer H.263 Depayloder:
  5. * Copyright 2005 Wim Taymans
  6. * Copyright 2007 Edward Hervey
  7. * Copyright 2007 Nokia Corporation
  8. * Copyright 2007 Collabora Ltd, Philippe Kalaf
  9. * Copyright 2010 Mark Nauwelaerts
  10. *
  11. * This file is part of Libav.
  12. *
  13. * Libav is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * Libav is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with Libav; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #include "avformat.h"
  28. #include "avio_internal.h"
  29. #include "rtpdec_formats.h"
  30. #include "libavutil/attributes.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavcodec/bitstream.h"
  33. struct PayloadContext {
  34. AVIOContext *buf;
  35. uint8_t endbyte;
  36. int endbyte_bits;
  37. uint32_t timestamp;
  38. int newformat;
  39. };
  40. static void h263_close_context(PayloadContext *data)
  41. {
  42. ffio_free_dyn_buf(&data->buf);
  43. }
  44. static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  45. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  46. const uint8_t *buf, int len, uint16_t seq,
  47. int flags)
  48. {
  49. /* Corresponding to header fields in the RFC */
  50. int f, p, i, sbit, ebit, src, r;
  51. int header_size, ret;
  52. if (data->newformat)
  53. return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf, len,
  54. seq, flags);
  55. if (data->buf && data->timestamp != *timestamp) {
  56. /* Dropping old buffered, unfinished data */
  57. ffio_free_dyn_buf(&data->buf);
  58. data->endbyte_bits = 0;
  59. }
  60. if (len < 4) {
  61. av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet: %d\n", len);
  62. return AVERROR_INVALIDDATA;
  63. }
  64. f = buf[0] & 0x80;
  65. p = buf[0] & 0x40;
  66. if (!f) {
  67. /* Mode A */
  68. header_size = 4;
  69. i = buf[1] & 0x10;
  70. r = ((buf[1] & 0x01) << 3) | ((buf[2] & 0xe0) >> 5);
  71. } else if (!p) {
  72. /* Mode B */
  73. header_size = 8;
  74. if (len < header_size) {
  75. av_log(ctx, AV_LOG_ERROR,
  76. "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
  77. len, header_size);
  78. return AVERROR_INVALIDDATA;
  79. }
  80. r = buf[3] & 0x03;
  81. i = buf[4] & 0x80;
  82. } else {
  83. /* Mode C */
  84. header_size = 12;
  85. if (len < header_size) {
  86. av_log(ctx, AV_LOG_ERROR,
  87. "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
  88. len, header_size);
  89. return AVERROR_INVALIDDATA;
  90. }
  91. r = buf[3] & 0x03;
  92. i = buf[4] & 0x80;
  93. }
  94. sbit = (buf[0] >> 3) & 0x7;
  95. ebit = buf[0] & 0x7;
  96. src = (buf[1] & 0xe0) >> 5;
  97. if (!(buf[0] & 0xf8)) { /* Reserved bits in RFC 2429/4629 are zero */
  98. if ((src == 0 || src >= 6) && r) {
  99. /* Invalid src for this format, and bits that should be zero
  100. * according to RFC 2190 aren't zero. */
  101. av_log(ctx, AV_LOG_WARNING,
  102. "Interpreting H.263 RTP data as RFC 2429/4629 even though "
  103. "signalled with a static payload type.\n");
  104. data->newformat = 1;
  105. return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf,
  106. len, seq, flags);
  107. }
  108. }
  109. buf += header_size;
  110. len -= header_size;
  111. if (!data->buf) {
  112. /* Check the picture start code, only start buffering a new frame
  113. * if this is correct */
  114. if (len > 4 && AV_RB32(buf) >> 10 == 0x20) {
  115. ret = avio_open_dyn_buf(&data->buf);
  116. if (ret < 0)
  117. return ret;
  118. data->timestamp = *timestamp;
  119. } else {
  120. /* Frame not started yet, skipping */
  121. return AVERROR(EAGAIN);
  122. }
  123. }
  124. if (data->endbyte_bits || sbit) {
  125. if (data->endbyte_bits == sbit) {
  126. data->endbyte |= buf[0] & (0xff >> sbit);
  127. data->endbyte_bits = 0;
  128. buf++;
  129. len--;
  130. avio_w8(data->buf, data->endbyte);
  131. } else {
  132. /* Start/end skip bits not matching - missed packets? */
  133. BitstreamContext bc;
  134. bitstream_init(&bc, buf, len * 8 - ebit);
  135. bitstream_skip(&bc, sbit);
  136. if (data->endbyte_bits) {
  137. data->endbyte |= bitstream_read(&bc, 8 - data->endbyte_bits);
  138. avio_w8(data->buf, data->endbyte);
  139. }
  140. while (bitstream_bits_left(&bc) >= 8)
  141. avio_w8(data->buf, bitstream_read(&bc, 8));
  142. data->endbyte_bits = bitstream_bits_left(&bc);
  143. if (data->endbyte_bits)
  144. data->endbyte = bitstream_read(&bc, data->endbyte_bits) <<
  145. (8 - data->endbyte_bits);
  146. ebit = 0;
  147. len = 0;
  148. }
  149. }
  150. if (ebit) {
  151. if (len > 0)
  152. avio_write(data->buf, buf, len - 1);
  153. data->endbyte_bits = 8 - ebit;
  154. data->endbyte = buf[len - 1] & (0xff << ebit);
  155. } else {
  156. avio_write(data->buf, buf, len);
  157. }
  158. if (!(flags & RTP_FLAG_MARKER))
  159. return AVERROR(EAGAIN);
  160. if (data->endbyte_bits)
  161. avio_w8(data->buf, data->endbyte);
  162. data->endbyte_bits = 0;
  163. ret = ff_rtp_finalize_packet(pkt, &data->buf, st->index);
  164. if (ret < 0)
  165. return ret;
  166. if (!i)
  167. pkt->flags |= AV_PKT_FLAG_KEY;
  168. return 0;
  169. }
  170. RTPDynamicProtocolHandler ff_h263_rfc2190_dynamic_handler = {
  171. .codec_type = AVMEDIA_TYPE_VIDEO,
  172. .codec_id = AV_CODEC_ID_H263,
  173. .need_parsing = AVSTREAM_PARSE_FULL,
  174. .parse_packet = h263_handle_packet,
  175. .priv_data_size = sizeof(PayloadContext),
  176. .close = h263_close_context,
  177. .static_payload_id = 34,
  178. };