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.

197 lines
5.7KB

  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', 'U', 'Y', '2') }, /* Packed formats */
  43. { PIX_FMT_YUV422, MKTAG('Y', '4', '2', '2') },
  44. { PIX_FMT_UYVY422, MKTAG('U', 'Y', 'V', 'Y') },
  45. { PIX_FMT_GRAY8, MKTAG('G', 'R', 'E', 'Y') },
  46. { -1, 0 },
  47. };
  48. static int findPixelFormat(unsigned int fourcc)
  49. {
  50. const PixelFormatTag * tags = pixelFormatTags;
  51. while (tags->pix_fmt >= 0) {
  52. if (tags->fourcc == fourcc)
  53. return tags->pix_fmt;
  54. tags++;
  55. }
  56. return PIX_FMT_YUV420P;
  57. }
  58. unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat fmt)
  59. {
  60. const PixelFormatTag * tags = pixelFormatTags;
  61. while (tags->pix_fmt >= 0) {
  62. if (tags->pix_fmt == fmt)
  63. return tags->fourcc;
  64. tags++;
  65. }
  66. return 0;
  67. }
  68. /* RAW Decoder Implementation */
  69. static int raw_init_decoder(AVCodecContext *avctx)
  70. {
  71. RawVideoContext *context = avctx->priv_data;
  72. if (avctx->codec_tag)
  73. avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
  74. else if (avctx->bits_per_sample){
  75. switch(avctx->bits_per_sample){
  76. case 15: avctx->pix_fmt= PIX_FMT_RGB555; break;
  77. case 16: avctx->pix_fmt= PIX_FMT_RGB565; break;
  78. case 24: avctx->pix_fmt= PIX_FMT_BGR24 ; break;
  79. case 32: avctx->pix_fmt= PIX_FMT_RGBA32; break;
  80. }
  81. }
  82. context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  83. context->buffer = av_malloc(context->length);
  84. context->p = context->buffer;
  85. context->pic.pict_type = FF_I_TYPE;
  86. context->pic.key_frame = 1;
  87. avctx->coded_frame= &context->pic;
  88. if (!context->buffer)
  89. return -1;
  90. return 0;
  91. }
  92. static void flip(AVCodecContext *avctx, AVPicture * picture){
  93. if(!avctx->codec_tag && avctx->bits_per_sample && picture->linesize[1]==0){
  94. picture->data[0] += picture->linesize[0] * (avctx->height-1);
  95. picture->linesize[0] *= -1;
  96. }
  97. }
  98. static int raw_decode(AVCodecContext *avctx,
  99. void *data, int *data_size,
  100. uint8_t *buf, int buf_size)
  101. {
  102. RawVideoContext *context = avctx->priv_data;
  103. int bytesNeeded;
  104. AVPicture * picture = (AVPicture *) data;
  105. /* Early out without copy if packet size == frame size */
  106. if (buf_size == context->length && context->p == context->buffer) {
  107. avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
  108. flip(avctx, picture);
  109. *data_size = sizeof(AVPicture);
  110. return buf_size;
  111. }
  112. bytesNeeded = context->length - (context->p - context->buffer);
  113. if (buf_size < bytesNeeded) {
  114. memcpy(context->p, buf, buf_size);
  115. context->p += buf_size;
  116. return buf_size;
  117. }
  118. memcpy(context->p, buf, bytesNeeded);
  119. context->p = context->buffer;
  120. avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
  121. flip(avctx, picture);
  122. *data_size = sizeof(AVPicture);
  123. return bytesNeeded;
  124. }
  125. static int raw_close_decoder(AVCodecContext *avctx)
  126. {
  127. RawVideoContext *context = avctx->priv_data;
  128. av_freep(&context->buffer);
  129. return 0;
  130. }
  131. /* RAW Encoder Implementation */
  132. static int raw_init_encoder(AVCodecContext *avctx)
  133. {
  134. avctx->coded_frame = (AVFrame *)avctx->priv_data;
  135. avctx->coded_frame->pict_type = FF_I_TYPE;
  136. avctx->coded_frame->key_frame = 1;
  137. if(!avctx->codec_tag)
  138. avctx->codec_tag = avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
  139. return 0;
  140. }
  141. static int raw_encode(AVCodecContext *avctx,
  142. unsigned char *frame, int buf_size, void *data)
  143. {
  144. return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
  145. avctx->height, frame, buf_size);
  146. }
  147. #ifdef CONFIG_RAWVIDEO_ENCODER
  148. AVCodec rawvideo_encoder = {
  149. "rawvideo",
  150. CODEC_TYPE_VIDEO,
  151. CODEC_ID_RAWVIDEO,
  152. sizeof(AVFrame),
  153. raw_init_encoder,
  154. raw_encode,
  155. };
  156. #endif // CONFIG_RAWVIDEO_ENCODER
  157. AVCodec rawvideo_decoder = {
  158. "rawvideo",
  159. CODEC_TYPE_VIDEO,
  160. CODEC_ID_RAWVIDEO,
  161. sizeof(RawVideoContext),
  162. raw_init_decoder,
  163. NULL,
  164. raw_close_decoder,
  165. raw_decode,
  166. };