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.

215 lines
6.6KB

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