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.

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