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.

201 lines
6.4KB

  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 "rtpdec_formats.h"
  29. #include "libavutil/attributes.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavcodec/get_bits.h"
  32. struct PayloadContext {
  33. AVIOContext *buf;
  34. uint8_t endbyte;
  35. int endbyte_bits;
  36. uint32_t timestamp;
  37. int newformat;
  38. };
  39. static void h263_free_context(PayloadContext *data)
  40. {
  41. if (data->buf) {
  42. uint8_t *p;
  43. avio_close_dyn_buf(data->buf, &p);
  44. av_free(p);
  45. }
  46. }
  47. static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  48. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  49. const uint8_t *buf, int len, uint16_t seq,
  50. int flags)
  51. {
  52. /* Corresponding to header fields in the RFC */
  53. int f, p, i, sbit, ebit, src, r;
  54. int header_size, ret;
  55. if (data->newformat)
  56. return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf, len,
  57. seq, flags);
  58. if (data->buf && data->timestamp != *timestamp) {
  59. /* Dropping old buffered, unfinished data */
  60. uint8_t *p;
  61. avio_close_dyn_buf(data->buf, &p);
  62. av_free(p);
  63. data->buf = NULL;
  64. data->endbyte_bits = 0;
  65. }
  66. if (len < 4) {
  67. av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet: %d\n", len);
  68. return AVERROR_INVALIDDATA;
  69. }
  70. f = buf[0] & 0x80;
  71. p = buf[0] & 0x40;
  72. if (!f) {
  73. /* Mode A */
  74. header_size = 4;
  75. i = buf[1] & 0x10;
  76. r = ((buf[1] & 0x01) << 3) | ((buf[2] & 0xe0) >> 5);
  77. } else if (!p) {
  78. /* Mode B */
  79. header_size = 8;
  80. if (len < header_size) {
  81. av_log(ctx, AV_LOG_ERROR,
  82. "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
  83. len, header_size);
  84. return AVERROR_INVALIDDATA;
  85. }
  86. r = buf[3] & 0x03;
  87. i = buf[4] & 0x80;
  88. } else {
  89. /* Mode C */
  90. header_size = 12;
  91. if (len < header_size) {
  92. av_log(ctx, AV_LOG_ERROR,
  93. "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
  94. len, header_size);
  95. return AVERROR_INVALIDDATA;
  96. }
  97. r = buf[3] & 0x03;
  98. i = buf[4] & 0x80;
  99. }
  100. sbit = (buf[0] >> 3) & 0x7;
  101. ebit = buf[0] & 0x7;
  102. src = (buf[1] & 0xe0) >> 5;
  103. if (!(buf[0] & 0xf8)) { /* Reserved bits in RFC 2429/4629 are zero */
  104. if ((src == 0 || src >= 6) && r) {
  105. /* Invalid src for this format, and bits that should be zero
  106. * according to RFC 2190 aren't zero. */
  107. av_log(ctx, AV_LOG_WARNING,
  108. "Interpreting H263 RTP data as RFC 2429/4629 even though "
  109. "signalled with a static payload type.\n");
  110. data->newformat = 1;
  111. return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf,
  112. len, seq, flags);
  113. }
  114. }
  115. buf += header_size;
  116. len -= header_size;
  117. if (!data->buf) {
  118. /* Check the picture start code, only start buffering a new frame
  119. * if this is correct */
  120. if (len > 4 && AV_RB32(buf) >> 10 == 0x20) {
  121. ret = avio_open_dyn_buf(&data->buf);
  122. if (ret < 0)
  123. return ret;
  124. data->timestamp = *timestamp;
  125. } else {
  126. /* Frame not started yet, skipping */
  127. return AVERROR(EAGAIN);
  128. }
  129. }
  130. if (data->endbyte_bits || sbit) {
  131. if (data->endbyte_bits == sbit) {
  132. data->endbyte |= buf[0] & (0xff >> sbit);
  133. data->endbyte_bits = 0;
  134. buf++;
  135. len--;
  136. avio_w8(data->buf, data->endbyte);
  137. } else {
  138. /* Start/end skip bits not matching - missed packets? */
  139. GetBitContext gb;
  140. init_get_bits(&gb, buf, len*8 - ebit);
  141. skip_bits(&gb, sbit);
  142. if (data->endbyte_bits) {
  143. data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits);
  144. avio_w8(data->buf, data->endbyte);
  145. }
  146. while (get_bits_left(&gb) >= 8)
  147. avio_w8(data->buf, get_bits(&gb, 8));
  148. data->endbyte_bits = get_bits_left(&gb);
  149. if (data->endbyte_bits)
  150. data->endbyte = get_bits(&gb, data->endbyte_bits) <<
  151. (8 - data->endbyte_bits);
  152. ebit = 0;
  153. len = 0;
  154. }
  155. }
  156. if (ebit) {
  157. if (len > 0)
  158. avio_write(data->buf, buf, len - 1);
  159. data->endbyte_bits = 8 - ebit;
  160. data->endbyte = buf[len - 1] & (0xff << ebit);
  161. } else {
  162. avio_write(data->buf, buf, len);
  163. }
  164. if (!(flags & RTP_FLAG_MARKER))
  165. return AVERROR(EAGAIN);
  166. if (data->endbyte_bits)
  167. avio_w8(data->buf, data->endbyte);
  168. data->endbyte_bits = 0;
  169. ret = ff_rtp_finalize_packet(pkt, &data->buf, st->index);
  170. if (ret < 0)
  171. return ret;
  172. if (!i)
  173. pkt->flags |= AV_PKT_FLAG_KEY;
  174. return 0;
  175. }
  176. RTPDynamicProtocolHandler ff_h263_rfc2190_dynamic_handler = {
  177. .codec_type = AVMEDIA_TYPE_VIDEO,
  178. .codec_id = AV_CODEC_ID_H263,
  179. .need_parsing = AVSTREAM_PARSE_FULL,
  180. .parse_packet = h263_handle_packet,
  181. .priv_data_size = sizeof(PayloadContext),
  182. .free = h263_free_context,
  183. .static_payload_id = 34,
  184. };