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.

148 lines
3.3KB

  1. /*
  2. * Raw DV format
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #define NTSC_FRAME_SIZE 120000
  21. #define PAL_FRAME_SIZE 144000
  22. typedef struct DVDemuxContext {
  23. int is_audio;
  24. uint8_t buf[PAL_FRAME_SIZE];
  25. int size;
  26. } DVDemuxContext;
  27. /* raw input */
  28. static int dv_read_header(AVFormatContext *s,
  29. AVFormatParameters *ap)
  30. {
  31. AVStream *vst, *ast;
  32. DVDemuxContext *c = s->priv_data;
  33. vst = av_new_stream(s, 0);
  34. if (!vst)
  35. return AVERROR_NOMEM;
  36. vst->codec.codec_type = CODEC_TYPE_VIDEO;
  37. vst->codec.codec_id = CODEC_ID_DVVIDEO;
  38. ast = av_new_stream(s, 1);
  39. if (!ast)
  40. return AVERROR_NOMEM;
  41. ast->codec.codec_type = CODEC_TYPE_AUDIO;
  42. ast->codec.codec_id = CODEC_ID_DVAUDIO;
  43. ast->codec.channels = 2;
  44. c->is_audio = 0;
  45. return 0;
  46. }
  47. static void __destruct_pkt(struct AVPacket *pkt)
  48. {
  49. pkt->data = NULL; pkt->size = 0;
  50. return;
  51. }
  52. static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
  53. {
  54. int ret, dsf;
  55. DVDemuxContext *c = s->priv_data;
  56. if (!c->is_audio) {
  57. ret = get_buffer(&s->pb, c->buf, 4);
  58. if (ret <= 0)
  59. return -EIO;
  60. dsf = c->buf[3] & 0x80;
  61. if (!dsf)
  62. c->size = NTSC_FRAME_SIZE;
  63. else
  64. c->size = PAL_FRAME_SIZE;
  65. ret = get_buffer(&s->pb, c->buf + 4, c->size - 4);
  66. if (ret <= 0)
  67. return -EIO;
  68. }
  69. av_init_packet(pkt);
  70. pkt->destruct = __destruct_pkt;
  71. pkt->data = c->buf;
  72. pkt->size = c->size;
  73. pkt->stream_index = c->is_audio;
  74. c->is_audio = !c->is_audio;
  75. return c->size;
  76. }
  77. static int dv_read_close(AVFormatContext *s)
  78. {
  79. return 0;
  80. }
  81. static AVInputFormat dv_iformat = {
  82. "dv",
  83. "DV video format",
  84. sizeof(DVDemuxContext),
  85. NULL,
  86. dv_read_header,
  87. dv_read_packet,
  88. dv_read_close,
  89. .extensions = "dv",
  90. };
  91. #if 0
  92. int dv_write_header(struct AVFormatContext *s)
  93. {
  94. return 0;
  95. }
  96. int dv_write_packet(struct AVFormatContext *s,
  97. int stream_index,
  98. unsigned char *buf, int size, int force_pts)
  99. {
  100. put_buffer(&s->pb, buf, size);
  101. put_flush_packet(&s->pb);
  102. return 0;
  103. }
  104. int dv_write_trailer(struct AVFormatContext *s)
  105. {
  106. return 0;
  107. }
  108. AVOutputFormat dv_oformat = {
  109. "dv",
  110. "DV video format",
  111. NULL,
  112. "dv",
  113. 0,
  114. CODEC_ID_DVVIDEO,
  115. CODEC_ID_DVAUDIO,
  116. dv_write_header,
  117. dv_write_packet,
  118. dv_write_trailer,
  119. };
  120. #endif
  121. int dv_init(void)
  122. {
  123. av_register_input_format(&dv_iformat);
  124. // av_register_output_format(&dv_oformat);
  125. return 0;
  126. }