libcdio.c 6.2 KB

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