libcdio.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/opt.h"
  34. #include "libavformat/avformat.h"
  35. #include "libavformat/demux.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->url, CDDA_MESSAGE_LOGIT, &err);
  55. if (!s->drive) {
  56. av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->url);
  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->url);
  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->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  77. if (s->drive->bigendianp)
  78. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
  79. else
  80. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  81. st->codecpar->sample_rate = 44100;
  82. st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
  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,
  89. 2 * st->codecpar->ch_layout.nb_channels * st->codecpar->sample_rate);
  90. for (i = 0; i < s->drive->tracks; i++) {
  91. char title[16];
  92. snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
  93. avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
  94. s->drive->disc_toc[i+1].dwStartSector, title);
  95. }
  96. s->last_sector = cdio_cddap_disc_lastsector(s->drive);
  97. return 0;
  98. }
  99. static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
  100. {
  101. CDIOContext *s = ctx->priv_data;
  102. int ret;
  103. uint16_t *buf;
  104. char *err = NULL;
  105. buf = cdio_paranoia_read(s->paranoia, NULL);
  106. if (!buf)
  107. return AVERROR_EOF;
  108. if (err = cdio_cddap_errors(s->drive)) {
  109. av_log(ctx, AV_LOG_ERROR, "%s\n", err);
  110. free(err);
  111. err = NULL;
  112. }
  113. if (err = cdio_cddap_messages(s->drive)) {
  114. av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
  115. free(err);
  116. err = NULL;
  117. }
  118. if ((ret = av_new_packet(pkt, CDIO_CD_FRAMESIZE_RAW)) < 0)
  119. return ret;
  120. memcpy(pkt->data, buf, CDIO_CD_FRAMESIZE_RAW);
  121. return 0;
  122. }
  123. static av_cold int read_close(AVFormatContext *ctx)
  124. {
  125. CDIOContext *s = ctx->priv_data;
  126. cdio_paranoia_free(s->paranoia);
  127. cdio_cddap_close(s->drive);
  128. return 0;
  129. }
  130. static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
  131. int flags)
  132. {
  133. CDIOContext *s = ctx->priv_data;
  134. AVStream *st = ctx->streams[0];
  135. cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
  136. avpriv_update_cur_dts(ctx, st, timestamp);
  137. return 0;
  138. }
  139. #define OFFSET(x) offsetof(CDIOContext, x)
  140. #define DEC AV_OPT_FLAG_DECODING_PARAM
  141. static const AVOption options[] = {
  142. { "speed", "set drive reading speed", OFFSET(speed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
  143. { "paranoia_mode", "set error recovery mode", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { .i64 = PARANOIA_MODE_DISABLE }, INT_MIN, INT_MAX, DEC, .unit = "paranoia_mode" },
  144. { "disable", "apply no fixups", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_DISABLE }, 0, 0, DEC, .unit = "paranoia_mode" },
  145. { "verify", "verify data integrity in overlap area", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_VERIFY }, 0, 0, DEC, .unit = "paranoia_mode" },
  146. { "overlap", "perform overlapped reads", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_OVERLAP }, 0, 0, DEC, .unit = "paranoia_mode" },
  147. { "neverskip", "do not skip failed reads", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, .unit = "paranoia_mode" },
  148. { "full", "apply all recovery modes", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_FULL }, 0, 0, DEC, .unit = "paranoia_mode" },
  149. { NULL },
  150. };
  151. static const AVClass libcdio_class = {
  152. .class_name = "libcdio indev",
  153. .item_name = av_default_item_name,
  154. .option = options,
  155. .version = LIBAVUTIL_VERSION_INT,
  156. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
  157. };
  158. const FFInputFormat ff_libcdio_demuxer = {
  159. .p.name = "libcdio",
  160. .p.flags = AVFMT_NOFILE,
  161. .p.priv_class = &libcdio_class,
  162. .read_header = read_header,
  163. .read_packet = read_packet,
  164. .read_close = read_close,
  165. .read_seek = read_seek,
  166. .priv_data_size = sizeof(CDIOContext),
  167. };