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.

176 lines
4.9KB

  1. /*
  2. * Raw Video Codec
  3. * Copyright (c) 2001 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. /**
  20. * @file raw.c
  21. * Raw Video Codec
  22. */
  23. #include "avcodec.h"
  24. typedef struct RawVideoContext {
  25. unsigned char * buffer; /* block of memory for holding one frame */
  26. unsigned char * p; /* current position in buffer */
  27. int length; /* number of bytes in buffer */
  28. AVFrame pic; ///< AVCodecContext.coded_frame
  29. } RawVideoContext;
  30. typedef struct PixleFormatTag {
  31. int pix_fmt;
  32. unsigned int fourcc;
  33. } PixelFormatTag;
  34. const PixelFormatTag pixelFormatTags[] = {
  35. { PIX_FMT_YUV420P, MKTAG('I', '4', '2', '0') }, /* Planar formats */
  36. { PIX_FMT_YUV420P, MKTAG('I', 'Y', 'U', 'V') },
  37. { PIX_FMT_YUV410P, MKTAG('Y', 'U', 'V', '9') },
  38. { PIX_FMT_YUV411P, MKTAG('Y', '4', '1', 'B') },
  39. { PIX_FMT_YUV422P, MKTAG('Y', '4', '2', 'B') },
  40. { PIX_FMT_GRAY8, MKTAG('Y', '8', '0', '0') },
  41. { PIX_FMT_GRAY8, MKTAG(' ', ' ', 'Y', '8') },
  42. { PIX_FMT_YUV422, MKTAG('Y', '4', '2', '2') }, /* Packed formats */
  43. { PIX_FMT_YUV422, MKTAG('U', 'Y', 'V', 'Y') },
  44. { PIX_FMT_GRAY8, MKTAG('G', 'R', 'E', 'Y') },
  45. { -1, 0 },
  46. };
  47. static int findPixelFormat(unsigned int fourcc)
  48. {
  49. const PixelFormatTag * tags = pixelFormatTags;
  50. while (tags->pix_fmt >= 0) {
  51. if (tags->fourcc == fourcc)
  52. return tags->pix_fmt;
  53. tags++;
  54. }
  55. return PIX_FMT_YUV420P;
  56. }
  57. static unsigned int findFourCC(int fmt)
  58. {
  59. const PixelFormatTag * tags = pixelFormatTags;
  60. while (tags->pix_fmt >= 0) {
  61. if (tags->pix_fmt == fmt)
  62. return tags->fourcc;
  63. tags++;
  64. }
  65. return 0;
  66. }
  67. /* RAW Decoder Implementation */
  68. static int raw_init_decoder(AVCodecContext *avctx)
  69. {
  70. RawVideoContext *context = avctx->priv_data;
  71. if (avctx->codec_tag)
  72. avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
  73. context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  74. context->buffer = av_malloc(context->length);
  75. context->p = context->buffer;
  76. context->pic.pict_type = FF_I_TYPE;
  77. context->pic.key_frame = 1;
  78. avctx->coded_frame= &context->pic;
  79. if (!context->buffer)
  80. return -1;
  81. return 0;
  82. }
  83. static int raw_decode(AVCodecContext *avctx,
  84. void *data, int *data_size,
  85. uint8_t *buf, int buf_size)
  86. {
  87. RawVideoContext *context = avctx->priv_data;
  88. int bytesNeeded;
  89. AVPicture * picture = (AVPicture *) data;
  90. /* Early out without copy if packet size == frame size */
  91. if (buf_size == context->length && context->p == context->buffer) {
  92. avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
  93. *data_size = sizeof(AVPicture);
  94. return buf_size;
  95. }
  96. bytesNeeded = context->length - (context->p - context->buffer);
  97. if (buf_size < bytesNeeded) {
  98. memcpy(context->p, buf, buf_size);
  99. context->p += buf_size;
  100. return buf_size;
  101. }
  102. memcpy(context->p, buf, bytesNeeded);
  103. context->p = context->buffer;
  104. avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
  105. *data_size = sizeof(AVPicture);
  106. return bytesNeeded;
  107. }
  108. static int raw_close_decoder(AVCodecContext *avctx)
  109. {
  110. RawVideoContext *context = avctx->priv_data;
  111. av_freep(&context->buffer);
  112. return 0;
  113. }
  114. /* RAW Encoder Implementation */
  115. static int raw_init_encoder(AVCodecContext *avctx)
  116. {
  117. avctx->coded_frame = (AVFrame *)avctx->priv_data;
  118. avctx->coded_frame->pict_type = FF_I_TYPE;
  119. avctx->coded_frame->key_frame = 1;
  120. avctx->codec_tag = findFourCC(avctx->pix_fmt);
  121. return 0;
  122. }
  123. static int raw_encode(AVCodecContext *avctx,
  124. unsigned char *frame, int buf_size, void *data)
  125. {
  126. return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
  127. avctx->height, frame, buf_size);
  128. }
  129. AVCodec rawvideo_encoder = {
  130. "rawvideo",
  131. CODEC_TYPE_VIDEO,
  132. CODEC_ID_RAWVIDEO,
  133. sizeof(AVFrame),
  134. raw_init_encoder,
  135. raw_encode,
  136. };
  137. AVCodec rawvideo_decoder = {
  138. "rawvideo",
  139. CODEC_TYPE_VIDEO,
  140. CODEC_ID_RAWVIDEO,
  141. sizeof(RawVideoContext),
  142. raw_init_decoder,
  143. NULL,
  144. raw_close_decoder,
  145. raw_decode,
  146. };