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.

196 lines
5.6KB

  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_UYVY422, 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. unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat 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. else if (avctx->bits_per_sample){
  74. switch(avctx->bits_per_sample){
  75. case 15: avctx->pix_fmt= PIX_FMT_RGB555; break;
  76. case 16: avctx->pix_fmt= PIX_FMT_RGB565; break;
  77. case 24: avctx->pix_fmt= PIX_FMT_BGR24 ; break;
  78. case 32: avctx->pix_fmt= PIX_FMT_RGBA32; break;
  79. }
  80. }
  81. context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  82. context->buffer = av_malloc(context->length);
  83. context->p = context->buffer;
  84. context->pic.pict_type = FF_I_TYPE;
  85. context->pic.key_frame = 1;
  86. avctx->coded_frame= &context->pic;
  87. if (!context->buffer)
  88. return -1;
  89. return 0;
  90. }
  91. static void flip(AVCodecContext *avctx, AVPicture * picture){
  92. if(!avctx->codec_tag && avctx->bits_per_sample && picture->linesize[1]==0){
  93. picture->data[0] += picture->linesize[0] * (avctx->height-1);
  94. picture->linesize[0] *= -1;
  95. }
  96. }
  97. static int raw_decode(AVCodecContext *avctx,
  98. void *data, int *data_size,
  99. uint8_t *buf, int buf_size)
  100. {
  101. RawVideoContext *context = avctx->priv_data;
  102. int bytesNeeded;
  103. AVPicture * picture = (AVPicture *) data;
  104. /* Early out without copy if packet size == frame size */
  105. if (buf_size == context->length && context->p == context->buffer) {
  106. avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
  107. flip(avctx, picture);
  108. *data_size = sizeof(AVPicture);
  109. return buf_size;
  110. }
  111. bytesNeeded = context->length - (context->p - context->buffer);
  112. if (buf_size < bytesNeeded) {
  113. memcpy(context->p, buf, buf_size);
  114. context->p += buf_size;
  115. return buf_size;
  116. }
  117. memcpy(context->p, buf, bytesNeeded);
  118. context->p = context->buffer;
  119. avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
  120. flip(avctx, picture);
  121. *data_size = sizeof(AVPicture);
  122. return bytesNeeded;
  123. }
  124. static int raw_close_decoder(AVCodecContext *avctx)
  125. {
  126. RawVideoContext *context = avctx->priv_data;
  127. av_freep(&context->buffer);
  128. return 0;
  129. }
  130. /* RAW Encoder Implementation */
  131. static int raw_init_encoder(AVCodecContext *avctx)
  132. {
  133. avctx->coded_frame = (AVFrame *)avctx->priv_data;
  134. avctx->coded_frame->pict_type = FF_I_TYPE;
  135. avctx->coded_frame->key_frame = 1;
  136. if(!avctx->codec_tag)
  137. avctx->codec_tag = avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
  138. return 0;
  139. }
  140. static int raw_encode(AVCodecContext *avctx,
  141. unsigned char *frame, int buf_size, void *data)
  142. {
  143. return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
  144. avctx->height, frame, buf_size);
  145. }
  146. #ifdef CONFIG_RAWVIDEO_ENCODER
  147. AVCodec rawvideo_encoder = {
  148. "rawvideo",
  149. CODEC_TYPE_VIDEO,
  150. CODEC_ID_RAWVIDEO,
  151. sizeof(AVFrame),
  152. raw_init_encoder,
  153. raw_encode,
  154. };
  155. #endif // CONFIG_RAWVIDEO_ENCODER
  156. AVCodec rawvideo_decoder = {
  157. "rawvideo",
  158. CODEC_TYPE_VIDEO,
  159. CODEC_ID_RAWVIDEO,
  160. sizeof(RawVideoContext),
  161. raw_init_decoder,
  162. NULL,
  163. raw_close_decoder,
  164. raw_decode,
  165. };