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.

216 lines
6.7KB

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