sauce.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * SAUCE header parser
  3. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  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. /**
  22. * @file
  23. * SAUCE header parser
  24. */
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/dict.h"
  27. #include "avformat.h"
  28. #include "sauce.h"
  29. int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
  30. {
  31. AVIOContext *pb = avctx->pb;
  32. char buf[36];
  33. int datatype, filetype, t1, t2, nb_comments;
  34. uint64_t start_pos = avio_size(pb) - 128;
  35. avio_seek(pb, start_pos, SEEK_SET);
  36. if (avio_read(pb, buf, 7) != 7)
  37. return -1;
  38. if (memcmp(buf, "SAUCE00", 7))
  39. return -1;
  40. #define GET_SAUCE_META(name,size) \
  41. if (avio_read(pb, buf, size) == size && buf[0]) { \
  42. buf[size] = 0; \
  43. av_dict_set(&avctx->metadata, name, buf, 0); \
  44. }
  45. GET_SAUCE_META("title", 35)
  46. GET_SAUCE_META("artist", 20)
  47. GET_SAUCE_META("publisher", 20)
  48. GET_SAUCE_META("date", 8)
  49. avio_skip(pb, 4);
  50. datatype = avio_r8(pb);
  51. filetype = avio_r8(pb);
  52. t1 = avio_rl16(pb);
  53. t2 = avio_rl16(pb);
  54. nb_comments = avio_r8(pb);
  55. avio_skip(pb, 1); /* flags */
  56. avio_skip(pb, 4);
  57. GET_SAUCE_META("encoder", 22);
  58. if (got_width && datatype && filetype) {
  59. if ((datatype == 1 && filetype <=2) || (datatype == 5 && filetype == 255) || datatype == 6) {
  60. if (t1) {
  61. avctx->streams[0]->codec->width = t1<<3;
  62. *got_width = 1;
  63. }
  64. if (get_height && t2)
  65. avctx->streams[0]->codec->height = t2<<4;
  66. } else if (datatype == 5) {
  67. if (filetype > 1) {
  68. avctx->streams[0]->codec->width = (filetype == 1 ? t1 : filetype) << 4;
  69. *got_width = 1;
  70. }
  71. if (get_height && t2)
  72. avctx->streams[0]->codec->height = t2<<4;
  73. }
  74. }
  75. *fsize -= 128;
  76. if (nb_comments > 0) {
  77. avio_seek(pb, start_pos - 64*nb_comments - 5, SEEK_SET);
  78. if (avio_read(pb, buf, 5) == 5 && !memcmp(buf, "COMNT", 5)) {
  79. int i;
  80. char *str = av_malloc(65*nb_comments + 1);
  81. *fsize -= 64*nb_comments + 5;
  82. if (!str)
  83. return 0;
  84. for (i = 0; i < nb_comments; i++) {
  85. if (avio_read(pb, str + 65*i, 64) != 64)
  86. break;
  87. str[65*i + 64] = '\n';
  88. }
  89. str[65*i] = 0;
  90. av_dict_set(&avctx->metadata, "comment", str, AV_DICT_DONT_STRDUP_VAL);
  91. }
  92. }
  93. return 0;
  94. }