/libavformat/h264dec.c

http://github.com/FFmpeg/FFmpeg · C · 120 lines · 88 code · 12 blank · 20 comment · 33 complexity · 8a9388625ffe2c0e0ca36fc39f6a0463 MD5 · raw file

  1. /*
  2. * RAW H.264 video demuxer
  3. * Copyright (c) 2008 Michael Niedermayer <michaelni@gmx.at>
  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 "libavcodec/get_bits.h"
  22. #include "libavcodec/golomb.h"
  23. #include "avformat.h"
  24. #include "rawdec.h"
  25. #include "libavcodec/internal.h"
  26. #define MAX_SPS_COUNT 32
  27. #define MAX_PPS_COUNT 256
  28. static int h264_probe(const AVProbeData *p)
  29. {
  30. uint32_t code = -1;
  31. int sps = 0, pps = 0, idr = 0, res = 0, sli = 0;
  32. int i, ret;
  33. int pps_ids[MAX_PPS_COUNT+1] = {0};
  34. int sps_ids[MAX_SPS_COUNT+1] = {0};
  35. unsigned pps_id, sps_id;
  36. GetBitContext gb;
  37. for (i = 0; i + 2 < p->buf_size; i++) {
  38. code = (code << 8) + p->buf[i];
  39. if ((code & 0xffffff00) == 0x100) {
  40. int ref_idc = (code >> 5) & 3;
  41. int type = code & 0x1F;
  42. static const int8_t ref_zero[] = {
  43. 2, 0, 0, 0, 0, -1, 1, -1,
  44. -1, 1, 1, 1, 1, -1, 2, 2,
  45. 2, 2, 2, 0, 2, 2, 2, 2,
  46. 2, 2, 2, 2, 2, 2, 2, 2
  47. };
  48. if (code & 0x80) // forbidden_bit
  49. return 0;
  50. if (ref_zero[type] == 1 && ref_idc)
  51. return 0;
  52. if (ref_zero[type] == -1 && !ref_idc)
  53. return 0;
  54. if (ref_zero[type] == 2) {
  55. if (!(code == 0x100 && !p->buf[i + 1] && !p->buf[i + 2]))
  56. res++;
  57. }
  58. ret = init_get_bits8(&gb, p->buf + i + 1, p->buf_size - i - 1);
  59. if (ret < 0)
  60. return 0;
  61. switch (type) {
  62. case 1:
  63. case 5:
  64. get_ue_golomb_long(&gb);
  65. if (get_ue_golomb_long(&gb) > 9U)
  66. return 0;
  67. pps_id = get_ue_golomb_long(&gb);
  68. if (pps_id > MAX_PPS_COUNT)
  69. return 0;
  70. if (!pps_ids[pps_id])
  71. break;
  72. if (type == 1)
  73. sli++;
  74. else
  75. idr++;
  76. break;
  77. case 7:
  78. skip_bits(&gb, 14);
  79. if (get_bits(&gb, 2))
  80. return 0;
  81. skip_bits(&gb, 8);
  82. sps_id = get_ue_golomb_long(&gb);
  83. if (sps_id > MAX_SPS_COUNT)
  84. return 0;
  85. sps_ids[sps_id] = 1;
  86. sps++;
  87. break;
  88. case 8:
  89. pps_id = get_ue_golomb_long(&gb);
  90. if (pps_id > MAX_PPS_COUNT)
  91. return 0;
  92. sps_id = get_ue_golomb_long(&gb);
  93. if (sps_id > MAX_SPS_COUNT)
  94. return 0;
  95. if (!sps_ids[sps_id])
  96. break;
  97. pps_ids[pps_id] = 1;
  98. pps++;
  99. break;
  100. }
  101. }
  102. }
  103. ff_tlog(NULL, "sps:%d pps:%d idr:%d sli:%d res:%d\n", sps, pps, idr, sli, res);
  104. if (sps && pps && (idr || sli > 3) && res < (sps + pps + idr))
  105. return AVPROBE_SCORE_EXTENSION + 1; // 1 more than .mpg
  106. return 0;
  107. }
  108. FF_DEF_RAWVIDEO_DEMUXER(h264, "raw H.264 video", h264_probe, "h26l,h264,264,avc", AV_CODEC_ID_H264)