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.

116 lines
3.7KB

  1. /*
  2. * RTP H.263 Depacketizer, RFC 4629
  3. * Copyright (c) 2010 Martin Storsjo
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "rtpdec_formats.h"
  23. #include "libavutil/attributes.h"
  24. #include "libavutil/intreadwrite.h"
  25. static av_cold int h263_init(AVFormatContext *ctx, int st_index,
  26. PayloadContext *data)
  27. {
  28. if (st_index < 0)
  29. return 0;
  30. ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
  31. return 0;
  32. }
  33. int ff_h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  34. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  35. const uint8_t *buf, int len, uint16_t seq, int flags)
  36. {
  37. uint8_t *ptr;
  38. uint16_t header;
  39. int startcode, vrc, picture_header;
  40. if (len < 2) {
  41. av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet\n");
  42. return AVERROR_INVALIDDATA;
  43. }
  44. /* Decode the 16 bit H.263+ payload header, as described in section
  45. * 5.1 of RFC 4629. The fields of this header are:
  46. * - 5 reserved bits, should be ignored.
  47. * - One bit (P, startcode), indicating a picture start, picture segment
  48. * start or video sequence end. If set, two zero bytes should be
  49. * prepended to the payload.
  50. * - One bit (V, vrc), indicating the presence of an 8 bit Video
  51. * Redundancy Coding field after this 16 bit header.
  52. * - 6 bits (PLEN, picture_header), the length (in bytes) of an extra
  53. * picture header, following the VRC field.
  54. * - 3 bits (PEBIT), the number of bits to ignore of the last byte
  55. * of the extra picture header. (Not used at the moment.)
  56. */
  57. header = AV_RB16(buf);
  58. startcode = (header & 0x0400) >> 9;
  59. vrc = header & 0x0200;
  60. picture_header = (header & 0x01f8) >> 3;
  61. buf += 2;
  62. len -= 2;
  63. if (vrc) {
  64. /* Skip VRC header if present, not used at the moment. */
  65. buf += 1;
  66. len -= 1;
  67. }
  68. if (picture_header) {
  69. /* Skip extra picture header if present, not used at the moment. */
  70. buf += picture_header;
  71. len -= picture_header;
  72. }
  73. if (len < 0) {
  74. av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet\n");
  75. return AVERROR_INVALIDDATA;
  76. }
  77. if (av_new_packet(pkt, len + startcode)) {
  78. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  79. return AVERROR(ENOMEM);
  80. }
  81. pkt->stream_index = st->index;
  82. ptr = pkt->data;
  83. if (startcode) {
  84. *ptr++ = 0;
  85. *ptr++ = 0;
  86. }
  87. memcpy(ptr, buf, len);
  88. return 0;
  89. }
  90. RTPDynamicProtocolHandler ff_h263_1998_dynamic_handler = {
  91. .enc_name = "H263-1998",
  92. .codec_type = AVMEDIA_TYPE_VIDEO,
  93. .codec_id = AV_CODEC_ID_H263,
  94. .init = h263_init,
  95. .parse_packet = ff_h263_handle_packet,
  96. };
  97. RTPDynamicProtocolHandler ff_h263_2000_dynamic_handler = {
  98. .enc_name = "H263-2000",
  99. .codec_type = AVMEDIA_TYPE_VIDEO,
  100. .codec_id = AV_CODEC_ID_H263,
  101. .init = h263_init,
  102. .parse_packet = ff_h263_handle_packet,
  103. };