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.

118 lines
3.5KB

  1. /*
  2. * RTP packetization for MPEG video
  3. * Copyright (c) 2002 Fabrice Bellard.
  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 "rtp_internal.h"
  23. #include "mpegvideo.h"
  24. /* NOTE: a single frame must be passed with sequence header if
  25. needed. XXX: use slices. */
  26. void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size)
  27. {
  28. RTPDemuxContext *s = s1->priv_data;
  29. int len, h, max_packet_size;
  30. uint8_t *q;
  31. int begin_of_slice, end_of_slice, frame_type, temporal_reference;
  32. max_packet_size = s->max_payload_size;
  33. begin_of_slice = 1;
  34. end_of_slice = 0;
  35. frame_type = 0;
  36. temporal_reference = 0;
  37. while (size > 0) {
  38. int begin_of_sequence;
  39. begin_of_sequence = 0;
  40. len = max_packet_size - 4;
  41. if (len >= size) {
  42. len = size;
  43. end_of_slice = 1;
  44. } else {
  45. const uint8_t *r, *r1;
  46. int start_code;
  47. r1 = buf1;
  48. while (1) {
  49. start_code = -1;
  50. r = ff_find_start_code(r1, buf1 + size, &start_code);
  51. if((start_code & 0xFFFFFF00) == 0x100) {
  52. /* New start code found */
  53. if (start_code == 0x100) {
  54. frame_type = (r[1] & 0x38) >> 3;
  55. temporal_reference = (int)r[0] << 2 | r[1] >> 6;
  56. }
  57. if (start_code == 0x1B8) {
  58. begin_of_sequence = 1;
  59. }
  60. if (r - buf1 < len) {
  61. /* The current slice fits in the packet */
  62. if (begin_of_slice == 0) {
  63. /* no slice at the beginning of the packet... */
  64. end_of_slice = 1;
  65. len = r - buf1 - 4;
  66. break;
  67. }
  68. r1 = r;
  69. } else {
  70. if (r - r1 < max_packet_size) {
  71. len = r1 - buf1 - 4;
  72. end_of_slice = 1;
  73. }
  74. break;
  75. }
  76. } else {
  77. break;
  78. }
  79. }
  80. }
  81. h = 0;
  82. h |= temporal_reference << 16;
  83. h |= begin_of_sequence << 13;
  84. h |= begin_of_slice << 12;
  85. h |= end_of_slice << 11;
  86. h |= frame_type << 8;
  87. q = s->buf;
  88. *q++ = h >> 24;
  89. *q++ = h >> 16;
  90. *q++ = h >> 8;
  91. *q++ = h;
  92. memcpy(q, buf1, len);
  93. q += len;
  94. /* 90 KHz time stamp */
  95. s->timestamp = s->cur_timestamp;
  96. ff_rtp_send_data(s1, s->buf, q - s->buf, (len == size));
  97. buf1 += len;
  98. size -= len;
  99. begin_of_slice = end_of_slice;
  100. end_of_slice = 0;
  101. }
  102. }