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.

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