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.

149 lines
4.4KB

  1. /*
  2. * Raw FLAC demuxer
  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 "avformat.h"
  22. #include "flacdec.h"
  23. #include "id3v2.h"
  24. #include "internal.h"
  25. int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
  26. {
  27. const CodecMime *mime = ff_id3v2_mime_tags;
  28. enum AVCodecID id = AV_CODEC_ID_NONE;
  29. AVBufferRef *data = NULL;
  30. uint8_t mimetype[64], *desc = NULL;
  31. AVIOContext *pb = NULL;
  32. AVStream *st;
  33. int type, width, height;
  34. int len, ret = 0;
  35. pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
  36. if (!pb)
  37. return AVERROR(ENOMEM);
  38. /* read the picture type */
  39. type = avio_rb32(pb);
  40. if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
  41. av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
  42. if (s->error_recognition & AV_EF_EXPLODE) {
  43. RETURN_ERROR(AVERROR_INVALIDDATA);
  44. }
  45. type = 0;
  46. }
  47. /* picture mimetype */
  48. len = avio_rb32(pb);
  49. if (len <= 0 ||
  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. RETURN_ERROR(AVERROR(ENOMEM));
  77. }
  78. if (avio_read(pb, desc, len) != len) {
  79. av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
  80. if (s->error_recognition & AV_EF_EXPLODE)
  81. ret = AVERROR(EIO);
  82. goto fail;
  83. }
  84. desc[len] = 0;
  85. }
  86. /* picture metadata */
  87. width = avio_rb32(pb);
  88. height = avio_rb32(pb);
  89. avio_skip(pb, 8);
  90. /* picture data */
  91. len = avio_rb32(pb);
  92. if (len <= 0) {
  93. av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
  94. if (s->error_recognition & AV_EF_EXPLODE)
  95. ret = AVERROR_INVALIDDATA;
  96. goto fail;
  97. }
  98. if (!(data = av_buffer_alloc(len))) {
  99. RETURN_ERROR(AVERROR(ENOMEM));
  100. }
  101. if (avio_read(pb, data->data, len) != len) {
  102. av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
  103. if (s->error_recognition & AV_EF_EXPLODE)
  104. ret = AVERROR(EIO);
  105. goto fail;
  106. }
  107. st = avformat_new_stream(s, NULL);
  108. if (!st) {
  109. RETURN_ERROR(AVERROR(ENOMEM));
  110. }
  111. av_init_packet(&st->attached_pic);
  112. st->attached_pic.buf = data;
  113. st->attached_pic.data = data->data;
  114. st->attached_pic.size = len;
  115. st->attached_pic.stream_index = st->index;
  116. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  117. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  118. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  119. st->codec->codec_id = id;
  120. st->codec->width = width;
  121. st->codec->height = height;
  122. av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
  123. if (desc)
  124. av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
  125. av_freep(&pb);
  126. return 0;
  127. fail:
  128. av_buffer_unref(&data);
  129. av_freep(&desc);
  130. av_freep(&pb);
  131. return ret;
  132. }