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.5KB

  1. /*
  2. * Raw FLAC picture parser
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avformat.h"
  21. #include "flac_picture.h"
  22. #include "id3v2.h"
  23. #include "internal.h"
  24. int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
  25. {
  26. const CodecMime *mime = ff_id3v2_mime_tags;
  27. enum AVCodecID id = AV_CODEC_ID_NONE;
  28. AVBufferRef *data = NULL;
  29. uint8_t mimetype[64], *desc = NULL;
  30. AVIOContext *pb = NULL;
  31. AVStream *st;
  32. int width, height, ret = 0;
  33. unsigned int type, len;
  34. pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
  35. if (!pb)
  36. return AVERROR(ENOMEM);
  37. /* read the picture type */
  38. type = avio_rb32(pb);
  39. if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
  40. av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
  41. if (s->error_recognition & AV_EF_EXPLODE) {
  42. ret = AVERROR_INVALIDDATA;
  43. goto fail;
  44. }
  45. type = 0;
  46. }
  47. /* picture mimetype */
  48. len = avio_rb32(pb);
  49. if (!len || len >= 64 ||
  50. avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
  51. av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
  52. "picture.\n");
  53. if (s->error_recognition & AV_EF_EXPLODE)
  54. ret = AVERROR_INVALIDDATA;
  55. goto fail;
  56. }
  57. mimetype[len] = 0;
  58. while (mime->id != AV_CODEC_ID_NONE) {
  59. if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
  60. id = mime->id;
  61. break;
  62. }
  63. mime++;
  64. }
  65. if (id == AV_CODEC_ID_NONE) {
  66. av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
  67. mimetype);
  68. if (s->error_recognition & AV_EF_EXPLODE)
  69. ret = AVERROR_INVALIDDATA;
  70. goto fail;
  71. }
  72. /* picture description */
  73. len = avio_rb32(pb);
  74. if (len > 0) {
  75. if (!(desc = av_malloc(len + 1))) {
  76. ret = AVERROR(ENOMEM);
  77. goto fail;
  78. }
  79. if (avio_read(pb, desc, len) != len) {
  80. av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
  81. if (s->error_recognition & AV_EF_EXPLODE)
  82. ret = AVERROR(EIO);
  83. goto fail;
  84. }
  85. desc[len] = 0;
  86. }
  87. /* picture metadata */
  88. width = avio_rb32(pb);
  89. height = avio_rb32(pb);
  90. avio_skip(pb, 8);
  91. /* picture data */
  92. len = avio_rb32(pb);
  93. if (!len) {
  94. av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
  95. if (s->error_recognition & AV_EF_EXPLODE)
  96. ret = AVERROR_INVALIDDATA;
  97. goto fail;
  98. }
  99. if (!(data = av_buffer_alloc(len))) {
  100. ret = AVERROR(ENOMEM);
  101. goto fail;
  102. }
  103. if (avio_read(pb, data->data, len) != len) {
  104. av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
  105. if (s->error_recognition & AV_EF_EXPLODE)
  106. ret = AVERROR(EIO);
  107. goto fail;
  108. }
  109. st = avformat_new_stream(s, NULL);
  110. if (!st) {
  111. ret = AVERROR(ENOMEM);
  112. goto fail;
  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->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  122. st->codecpar->codec_id = id;
  123. st->codecpar->width = width;
  124. st->codecpar->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. avio_context_free(&pb);
  129. return 0;
  130. fail:
  131. av_buffer_unref(&data);
  132. av_freep(&desc);
  133. avio_context_free(&pb);
  134. return ret;
  135. }