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.

100 lines
3.2KB

  1. /*
  2. * @file
  3. * SAUCE header parser
  4. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "sauce.h"
  25. int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
  26. {
  27. ByteIOContext *pb = avctx->pb;
  28. char buf[36];
  29. int datatype, filetype, t1, t2, nb_comments, flags;
  30. uint64_t start_pos = url_fsize(pb) - 128;
  31. url_fseek(pb, start_pos, SEEK_SET);
  32. if (get_buffer(pb, buf, 7) != 7)
  33. return -1;
  34. if (memcmp(buf, "SAUCE00", 7))
  35. return -1;
  36. #define GET_SAUCE_META(name,size) \
  37. if (get_buffer(pb, buf, size) == size && buf[0]) { \
  38. buf[size] = 0; \
  39. av_metadata_set2(&avctx->metadata, name, buf, 0); \
  40. }
  41. GET_SAUCE_META("title", 35)
  42. GET_SAUCE_META("artist", 20)
  43. GET_SAUCE_META("publisher", 20)
  44. GET_SAUCE_META("date", 8)
  45. url_fskip(pb, 4);
  46. datatype = get_byte(pb);
  47. filetype = get_byte(pb);
  48. t1 = get_le16(pb);
  49. t2 = get_le16(pb);
  50. nb_comments = get_byte(pb);
  51. flags = get_byte(pb);
  52. url_fskip(pb, 4);
  53. GET_SAUCE_META("encoder", 22);
  54. if (got_width && datatype && filetype) {
  55. if ((datatype == 1 && filetype <=2) || (datatype == 5 && filetype == 255) || datatype == 6) {
  56. if (t1) {
  57. avctx->streams[0]->codec->width = t1<<3;
  58. *got_width = 1;
  59. }
  60. if (get_height && t2)
  61. avctx->streams[0]->codec->height = t2<<4;
  62. } else if (datatype == 5) {
  63. if (filetype > 1) {
  64. avctx->streams[0]->codec->width = (filetype == 1 ? t1 : filetype) << 4;
  65. *got_width = 1;
  66. }
  67. if (get_height && t2)
  68. avctx->streams[0]->codec->height = t2<<4;
  69. }
  70. }
  71. *fsize -= 128;
  72. if (nb_comments > 0) {
  73. url_fseek(pb, start_pos - 64*nb_comments - 5, SEEK_SET);
  74. if (get_buffer(pb, buf, 5) == 5 && !memcmp(buf, "COMNT", 5)) {
  75. int i;
  76. char *str = av_malloc(65*nb_comments + 1);
  77. *fsize -= 64*nb_comments + 5;
  78. if (!str)
  79. return 0;
  80. for (i = 0; i < nb_comments; i++) {
  81. if (get_buffer(pb, str + 65*i, 64) != 64)
  82. break;
  83. str[65*i + 64] = '\n';
  84. }
  85. str[65*i] = 0;
  86. av_metadata_set2(&avctx->metadata, "comment", str, AV_METADATA_DONT_STRDUP_VAL);
  87. }
  88. }
  89. return 0;
  90. }