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.

185 lines
6.0KB

  1. /*
  2. * Copyright (c) 2011 Anton Khirnov <anton@khirnov.net>
  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. /**
  21. * @file
  22. * libcdio CD grabbing
  23. */
  24. #include <cdio/cdda.h>
  25. #include <cdio/paranoia.h>
  26. #include "libavutil/log.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/opt.h"
  29. #include "libavformat/avformat.h"
  30. #include "libavformat/internal.h"
  31. typedef struct CDIOContext {
  32. AVClass *class;
  33. cdrom_drive_t *drive;
  34. cdrom_paranoia_t *paranoia;
  35. int32_t last_sector;
  36. /* private options */
  37. int speed;
  38. int paranoia_mode;
  39. } CDIOContext;
  40. static av_cold int read_header(AVFormatContext *ctx)
  41. {
  42. CDIOContext *s = ctx->priv_data;
  43. AVStream *st;
  44. int ret, i;
  45. char *err = NULL;
  46. if (!(st = avformat_new_stream(ctx, NULL)))
  47. return AVERROR(ENOMEM);
  48. s->drive = cdio_cddap_identify(ctx->filename, CDDA_MESSAGE_LOGIT, &err);
  49. if (!s->drive) {
  50. av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->filename);
  51. return AVERROR(EINVAL);
  52. }
  53. if (err) {
  54. av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
  55. free(err);
  56. }
  57. if ((ret = cdio_cddap_open(s->drive)) < 0 || !s->drive->opened) {
  58. av_log(ctx, AV_LOG_ERROR, "Could not open disk in drive %s.\n", ctx->filename);
  59. return AVERROR(EINVAL);
  60. }
  61. cdio_cddap_verbose_set(s->drive, CDDA_MESSAGE_LOGIT, CDDA_MESSAGE_LOGIT);
  62. if (s->speed)
  63. cdio_cddap_speed_set(s->drive, s->speed);
  64. s->paranoia = cdio_paranoia_init(s->drive);
  65. if (!s->paranoia) {
  66. av_log(ctx, AV_LOG_ERROR, "Could not init paranoia.\n");
  67. return AVERROR(EINVAL);
  68. }
  69. cdio_paranoia_modeset(s->paranoia, s->paranoia_mode);
  70. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  71. if (s->drive->bigendianp)
  72. st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  73. else
  74. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  75. st->codec->sample_rate = 44100;
  76. st->codec->channels = 2;
  77. if (s->drive->audio_last_sector != CDIO_INVALID_LSN &&
  78. s->drive->audio_first_sector != CDIO_INVALID_LSN)
  79. st->duration = s->drive->audio_last_sector - s->drive->audio_first_sector;
  80. else if (s->drive->tracks)
  81. st->duration = s->drive->disc_toc[s->drive->tracks].dwStartSector;
  82. avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2*st->codec->channels*st->codec->sample_rate);
  83. for (i = 0; i < s->drive->tracks; i++) {
  84. char title[16];
  85. snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
  86. avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
  87. s->drive->disc_toc[i+1].dwStartSector, title);
  88. }
  89. s->last_sector = cdio_cddap_disc_lastsector(s->drive);
  90. return 0;
  91. }
  92. static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
  93. {
  94. CDIOContext *s = ctx->priv_data;
  95. int ret;
  96. uint16_t *buf;
  97. char *err = NULL;
  98. if (ctx->streams[0]->cur_dts > s->last_sector)
  99. return AVERROR_EOF;
  100. buf = cdio_paranoia_read(s->paranoia, NULL);
  101. if (!buf)
  102. return AVERROR_EOF;
  103. if (err = cdio_cddap_errors(s->drive)) {
  104. av_log(ctx, AV_LOG_ERROR, "%s\n", err);
  105. free(err);
  106. err = NULL;
  107. }
  108. if (err = cdio_cddap_messages(s->drive)) {
  109. av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
  110. free(err);
  111. err = NULL;
  112. }
  113. if ((ret = av_new_packet(pkt, CDIO_CD_FRAMESIZE_RAW)) < 0)
  114. return ret;
  115. memcpy(pkt->data, buf, CDIO_CD_FRAMESIZE_RAW);
  116. return 0;
  117. }
  118. static av_cold int read_close(AVFormatContext *ctx)
  119. {
  120. CDIOContext *s = ctx->priv_data;
  121. cdio_paranoia_free(s->paranoia);
  122. cdio_cddap_close(s->drive);
  123. return 0;
  124. }
  125. static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
  126. int flags)
  127. {
  128. CDIOContext *s = ctx->priv_data;
  129. AVStream *st = ctx->streams[0];
  130. cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
  131. st->cur_dts = timestamp;
  132. return 0;
  133. }
  134. #define OFFSET(x) offsetof(CDIOContext, x)
  135. #define DEC AV_OPT_FLAG_DECODING_PARAM
  136. static const AVOption options[] = {
  137. { "speed", "Drive reading speed.", OFFSET(speed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
  138. { "paranoia_mode", "Error recovery mode.", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT_MIN, INT_MAX, DEC, "paranoia_mode" },
  139. { "verify", "Verify data integrity in overlap area", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_VERIFY }, 0, 0, DEC, "paranoia_mode" },
  140. { "overlap", "Perform overlapped reads.", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_OVERLAP }, 0, 0, DEC, "paranoia_mode" },
  141. { "neverskip", "Do not skip failed reads.", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, "paranoia_mode" },
  142. { NULL },
  143. };
  144. static const AVClass libcdio_class = {
  145. .class_name = "libcdio indev",
  146. .item_name = av_default_item_name,
  147. .option = options,
  148. .version = LIBAVUTIL_VERSION_INT,
  149. };
  150. AVInputFormat ff_libcdio_demuxer = {
  151. .name = "libcdio",
  152. .read_header = read_header,
  153. .read_packet = read_packet,
  154. .read_close = read_close,
  155. .read_seek = read_seek,
  156. .priv_data_size = sizeof(CDIOContext),
  157. .flags = AVFMT_NOFILE,
  158. .priv_class = &libcdio_class,
  159. };