rtpdec_h263_rfc2190.c 6.5 KB

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