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.

146 lines
3.2KB

  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. c->is_audio = 0;
  44. return 0;
  45. }
  46. static void __destruct_pkt(struct AVPacket *pkt)
  47. {
  48. pkt->data = NULL; pkt->size = 0;
  49. return;
  50. }
  51. static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
  52. {
  53. int ret, dsf;
  54. DVDemuxContext *c = s->priv_data;
  55. if (!c->is_audio) {
  56. ret = get_buffer(&s->pb, c->buf, 4);
  57. if (ret <= 0)
  58. return -EIO;
  59. dsf = c->buf[3] & 0x80;
  60. if (!dsf)
  61. c->size = NTSC_FRAME_SIZE;
  62. else
  63. c->size = PAL_FRAME_SIZE;
  64. ret = get_buffer(&s->pb, c->buf + 4, c->size - 4);
  65. if (ret <= 0)
  66. return -EIO;
  67. }
  68. av_init_packet(pkt);
  69. pkt->destruct = __destruct_pkt;
  70. pkt->data = c->buf;
  71. pkt->size = c->size;
  72. pkt->stream_index = c->is_audio;
  73. c->is_audio = !c->is_audio;
  74. return c->size;
  75. }
  76. static int dv_read_close(AVFormatContext *s)
  77. {
  78. return 0;
  79. }
  80. static AVInputFormat dv_iformat = {
  81. "dv",
  82. "DV video format",
  83. sizeof(DVDemuxContext),
  84. NULL,
  85. dv_read_header,
  86. dv_read_packet,
  87. dv_read_close,
  88. .extensions = "dv",
  89. };
  90. int dv_write_header(struct AVFormatContext *s)
  91. {
  92. return 0;
  93. }
  94. int dv_write_packet(struct AVFormatContext *s,
  95. int stream_index,
  96. unsigned char *buf, int size, int force_pts)
  97. {
  98. put_buffer(&s->pb, buf, size);
  99. put_flush_packet(&s->pb);
  100. return 0;
  101. }
  102. int dv_write_trailer(struct AVFormatContext *s)
  103. {
  104. return 0;
  105. }
  106. AVOutputFormat dv_oformat = {
  107. "dv",
  108. "DV video format",
  109. NULL,
  110. "dv",
  111. 0,
  112. CODEC_ID_DVVIDEO,
  113. CODEC_ID_DVAUDIO,
  114. dv_write_header,
  115. dv_write_packet,
  116. dv_write_trailer,
  117. };
  118. int dv_init(void)
  119. {
  120. av_register_input_format(&dv_iformat);
  121. av_register_output_format(&dv_oformat);
  122. return 0;
  123. }