libcdio.c 6.1 KB

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