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.

153 lines
4.6KB

  1. /*
  2. * Raw FLAC picture parser
  3. * Copyright (c) 2001 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 "libavutil/avassert.h"
  22. #include "avformat.h"
  23. #include "flac_picture.h"
  24. #include "id3v2.h"
  25. #include "internal.h"
  26. int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
  27. {
  28. const CodecMime *mime = ff_id3v2_mime_tags;
  29. enum AVCodecID id = AV_CODEC_ID_NONE;
  30. AVBufferRef *data = NULL;
  31. uint8_t mimetype[64], *desc = NULL;
  32. AVIOContext *pb = NULL;
  33. AVStream *st;
  34. int type, width, height;
  35. int len, ret = 0;
  36. pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
  37. if (!pb)
  38. return AVERROR(ENOMEM);
  39. /* read the picture type */
  40. type = avio_rb32(pb);
  41. if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
  42. av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
  43. if (s->error_recognition & AV_EF_EXPLODE) {
  44. RETURN_ERROR(AVERROR_INVALIDDATA);
  45. }
  46. type = 0;
  47. }
  48. /* picture mimetype */
  49. len = avio_rb32(pb);
  50. if (len <= 0 ||
  51. avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
  52. av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
  53. "picture.\n");
  54. if (s->error_recognition & AV_EF_EXPLODE)
  55. ret = AVERROR_INVALIDDATA;
  56. goto fail;
  57. }
  58. av_assert0(len < sizeof(mimetype));
  59. mimetype[len] = 0;
  60. while (mime->id != AV_CODEC_ID_NONE) {
  61. if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
  62. id = mime->id;
  63. break;
  64. }
  65. mime++;
  66. }
  67. if (id == AV_CODEC_ID_NONE) {
  68. av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
  69. mimetype);
  70. if (s->error_recognition & AV_EF_EXPLODE)
  71. ret = AVERROR_INVALIDDATA;
  72. goto fail;
  73. }
  74. /* picture description */
  75. len = avio_rb32(pb);
  76. if (len > 0) {
  77. if (!(desc = av_malloc(len + 1))) {
  78. RETURN_ERROR(AVERROR(ENOMEM));
  79. }
  80. if (avio_read(pb, desc, len) != len) {
  81. av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
  82. if (s->error_recognition & AV_EF_EXPLODE)
  83. ret = AVERROR(EIO);
  84. goto fail;
  85. }
  86. desc[len] = 0;
  87. }
  88. /* picture metadata */
  89. width = avio_rb32(pb);
  90. height = avio_rb32(pb);
  91. avio_skip(pb, 8);
  92. /* picture data */
  93. len = avio_rb32(pb);
  94. if (len <= 0) {
  95. av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
  96. if (s->error_recognition & AV_EF_EXPLODE)
  97. ret = AVERROR_INVALIDDATA;
  98. goto fail;
  99. }
  100. if (!(data = av_buffer_alloc(len + FF_INPUT_BUFFER_PADDING_SIZE))) {
  101. RETURN_ERROR(AVERROR(ENOMEM));
  102. }
  103. memset(data->data + len, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  104. if (avio_read(pb, data->data, len) != len) {
  105. av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
  106. if (s->error_recognition & AV_EF_EXPLODE)
  107. ret = AVERROR(EIO);
  108. goto fail;
  109. }
  110. st = avformat_new_stream(s, NULL);
  111. if (!st) {
  112. RETURN_ERROR(AVERROR(ENOMEM));
  113. }
  114. av_init_packet(&st->attached_pic);
  115. st->attached_pic.buf = data;
  116. st->attached_pic.data = data->data;
  117. st->attached_pic.size = len;
  118. st->attached_pic.stream_index = st->index;
  119. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  120. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  121. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  122. st->codec->codec_id = id;
  123. st->codec->width = width;
  124. st->codec->height = height;
  125. av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
  126. if (desc)
  127. av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
  128. av_freep(&pb);
  129. return 0;
  130. fail:
  131. av_buffer_unref(&data);
  132. av_freep(&desc);
  133. av_freep(&pb);
  134. return ret;
  135. }