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.

401 lines
12KB

  1. /*
  2. * YUV4MPEG format
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #define Y4M_MAGIC "YUV4MPEG2"
  23. #define Y4M_FRAME_MAGIC "FRAME"
  24. #define Y4M_LINE_MAX 256
  25. struct frame_attributes {
  26. int interlaced_frame;
  27. int top_field_first;
  28. };
  29. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  30. {
  31. AVStream *st;
  32. int width, height;
  33. int raten, rated, aspectn, aspectd, n;
  34. char inter;
  35. const char *colorspace = "";
  36. st = s->streams[0];
  37. width = st->codec->width;
  38. height = st->codec->height;
  39. av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
  40. aspectn = st->codec->sample_aspect_ratio.num;
  41. aspectd = st->codec->sample_aspect_ratio.den;
  42. if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
  43. inter = 'p'; /* progressive is the default */
  44. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
  45. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  46. }
  47. switch(st->codec->pix_fmt) {
  48. case PIX_FMT_GRAY8:
  49. colorspace = " Cmono";
  50. break;
  51. case PIX_FMT_YUV411P:
  52. colorspace = " C411 XYSCSS=411";
  53. break;
  54. case PIX_FMT_YUV420P:
  55. colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
  56. break;
  57. case PIX_FMT_YUV422P:
  58. colorspace = " C422 XYSCSS=422";
  59. break;
  60. case PIX_FMT_YUV444P:
  61. colorspace = " C444 XYSCSS=444";
  62. break;
  63. }
  64. /* construct stream header, if this is the first frame */
  65. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  66. Y4M_MAGIC,
  67. width,
  68. height,
  69. raten, rated,
  70. inter,
  71. aspectn, aspectd,
  72. colorspace);
  73. return n;
  74. }
  75. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  76. {
  77. AVStream *st = s->streams[pkt->stream_index];
  78. ByteIOContext *pb = s->pb;
  79. AVPicture *picture;
  80. int* first_pkt = s->priv_data;
  81. int width, height, h_chroma_shift, v_chroma_shift;
  82. int i, m;
  83. char buf2[Y4M_LINE_MAX+1];
  84. char buf1[20];
  85. uint8_t *ptr, *ptr1, *ptr2;
  86. picture = (AVPicture *)pkt->data;
  87. /* for the first packet we have to output the header as well */
  88. if (*first_pkt) {
  89. *first_pkt = 0;
  90. if (yuv4_generate_header(s, buf2) < 0) {
  91. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  92. return AVERROR(EIO);
  93. } else {
  94. put_buffer(pb, buf2, strlen(buf2));
  95. }
  96. }
  97. /* construct frame header */
  98. m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  99. put_buffer(pb, buf1, strlen(buf1));
  100. width = st->codec->width;
  101. height = st->codec->height;
  102. ptr = picture->data[0];
  103. for(i=0;i<height;i++) {
  104. put_buffer(pb, ptr, width);
  105. ptr += picture->linesize[0];
  106. }
  107. if (st->codec->pix_fmt != PIX_FMT_GRAY8){
  108. // Adjust for smaller Cb and Cr planes
  109. avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  110. width >>= h_chroma_shift;
  111. height >>= v_chroma_shift;
  112. ptr1 = picture->data[1];
  113. ptr2 = picture->data[2];
  114. for(i=0;i<height;i++) { /* Cb */
  115. put_buffer(pb, ptr1, width);
  116. ptr1 += picture->linesize[1];
  117. }
  118. for(i=0;i<height;i++) { /* Cr */
  119. put_buffer(pb, ptr2, width);
  120. ptr2 += picture->linesize[2];
  121. }
  122. }
  123. put_flush_packet(pb);
  124. return 0;
  125. }
  126. static int yuv4_write_header(AVFormatContext *s)
  127. {
  128. int* first_pkt = s->priv_data;
  129. if (s->nb_streams != 1)
  130. return AVERROR(EIO);
  131. if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
  132. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
  133. }
  134. else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
  135. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
  136. (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
  137. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
  138. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n");
  139. return AVERROR(EIO);
  140. }
  141. *first_pkt = 1;
  142. return 0;
  143. }
  144. #ifdef CONFIG_YUV4MPEGPIPE_MUXER
  145. AVOutputFormat yuv4mpegpipe_muxer = {
  146. "yuv4mpegpipe",
  147. "YUV4MPEG pipe format",
  148. "",
  149. "y4m",
  150. sizeof(int),
  151. CODEC_ID_NONE,
  152. CODEC_ID_RAWVIDEO,
  153. yuv4_write_header,
  154. yuv4_write_packet,
  155. .flags = AVFMT_RAWPICTURE,
  156. };
  157. #endif
  158. /* Header size increased to allow room for optional flags */
  159. #define MAX_YUV4_HEADER 80
  160. #define MAX_FRAME_HEADER 80
  161. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  162. {
  163. char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
  164. char *tokstart,*tokend,*header_end;
  165. int i;
  166. ByteIOContext *pb = s->pb;
  167. int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
  168. enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
  169. AVStream *st;
  170. struct frame_attributes *s1 = s->priv_data;
  171. for (i=0; i<MAX_YUV4_HEADER; i++) {
  172. header[i] = get_byte(pb);
  173. if (header[i] == '\n') {
  174. header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
  175. header[i+2] = 0;
  176. break;
  177. }
  178. }
  179. if (i == MAX_YUV4_HEADER) return -1;
  180. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  181. s1->interlaced_frame = 0;
  182. s1->top_field_first = 0;
  183. header_end = &header[i+1]; // Include space
  184. for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
  185. if (*tokstart==0x20) continue;
  186. switch (*tokstart++) {
  187. case 'W': // Width. Required.
  188. width = strtol(tokstart, &tokend, 10);
  189. tokstart=tokend;
  190. break;
  191. case 'H': // Height. Required.
  192. height = strtol(tokstart, &tokend, 10);
  193. tokstart=tokend;
  194. break;
  195. case 'C': // Color space
  196. if (strncmp("420jpeg",tokstart,7)==0)
  197. pix_fmt = PIX_FMT_YUV420P;
  198. else if (strncmp("420mpeg2",tokstart,8)==0)
  199. pix_fmt = PIX_FMT_YUV420P;
  200. else if (strncmp("420paldv", tokstart, 8)==0)
  201. pix_fmt = PIX_FMT_YUV420P;
  202. else if (strncmp("411", tokstart, 3)==0)
  203. pix_fmt = PIX_FMT_YUV411P;
  204. else if (strncmp("422", tokstart, 3)==0)
  205. pix_fmt = PIX_FMT_YUV422P;
  206. else if (strncmp("444alpha", tokstart, 8)==0) {
  207. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
  208. return -1;
  209. } else if (strncmp("444", tokstart, 3)==0)
  210. pix_fmt = PIX_FMT_YUV444P;
  211. else if (strncmp("mono",tokstart, 4)==0) {
  212. pix_fmt = PIX_FMT_GRAY8;
  213. } else {
  214. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
  215. return -1;
  216. }
  217. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  218. break;
  219. case 'I': // Interlace type
  220. switch (*tokstart++){
  221. case '?':
  222. break;
  223. case 'p':
  224. s1->interlaced_frame=0;
  225. break;
  226. case 't':
  227. s1->interlaced_frame=1;
  228. s1->top_field_first=1;
  229. break;
  230. case 'b':
  231. s1->interlaced_frame=1;
  232. s1->top_field_first=0;
  233. break;
  234. case 'm':
  235. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
  236. return -1;
  237. default:
  238. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  239. return -1;
  240. }
  241. break;
  242. case 'F': // Frame rate
  243. sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
  244. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  245. break;
  246. case 'A': // Pixel aspect
  247. sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
  248. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  249. break;
  250. case 'X': // Vendor extensions
  251. if (strncmp("YSCSS=",tokstart,6)==0) {
  252. // Older nonstandard pixel format representation
  253. tokstart+=6;
  254. if (strncmp("420JPEG",tokstart,7)==0)
  255. alt_pix_fmt=PIX_FMT_YUV420P;
  256. else if (strncmp("420MPEG2",tokstart,8)==0)
  257. alt_pix_fmt=PIX_FMT_YUV420P;
  258. else if (strncmp("420PALDV",tokstart,8)==0)
  259. alt_pix_fmt=PIX_FMT_YUV420P;
  260. else if (strncmp("411",tokstart,3)==0)
  261. alt_pix_fmt=PIX_FMT_YUV411P;
  262. else if (strncmp("422",tokstart,3)==0)
  263. alt_pix_fmt=PIX_FMT_YUV422P;
  264. else if (strncmp("444",tokstart,3)==0)
  265. alt_pix_fmt=PIX_FMT_YUV444P;
  266. }
  267. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  268. break;
  269. }
  270. }
  271. if ((width == -1) || (height == -1)) {
  272. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  273. return -1;
  274. }
  275. if (pix_fmt == PIX_FMT_NONE) {
  276. if (alt_pix_fmt == PIX_FMT_NONE)
  277. pix_fmt = PIX_FMT_YUV420P;
  278. else
  279. pix_fmt = alt_pix_fmt;
  280. }
  281. if (raten == 0 && rated == 0) {
  282. // Frame rate unknown
  283. raten = 25;
  284. rated = 1;
  285. }
  286. if (aspectn == 0 && aspectd == 0) {
  287. // Pixel aspect unknown
  288. aspectd = 1;
  289. }
  290. st = av_new_stream(s, 0);
  291. st = s->streams[0];
  292. st->codec->width = width;
  293. st->codec->height = height;
  294. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  295. av_set_pts_info(st, 64, rated, raten);
  296. st->codec->pix_fmt = pix_fmt;
  297. st->codec->codec_type = CODEC_TYPE_VIDEO;
  298. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  299. st->codec->sample_aspect_ratio= (AVRational){aspectn, aspectd};
  300. return 0;
  301. }
  302. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  303. {
  304. int i;
  305. char header[MAX_FRAME_HEADER+1];
  306. int packet_size, width, height;
  307. AVStream *st = s->streams[0];
  308. struct frame_attributes *s1 = s->priv_data;
  309. for (i=0; i<MAX_FRAME_HEADER; i++) {
  310. header[i] = get_byte(s->pb);
  311. if (header[i] == '\n') {
  312. header[i+1] = 0;
  313. break;
  314. }
  315. }
  316. if (i == MAX_FRAME_HEADER) return -1;
  317. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  318. width = st->codec->width;
  319. height = st->codec->height;
  320. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  321. if (packet_size < 0)
  322. return -1;
  323. if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
  324. return AVERROR(EIO);
  325. if (s->streams[0]->codec->coded_frame) {
  326. s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  327. s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
  328. }
  329. pkt->stream_index = 0;
  330. return 0;
  331. }
  332. static int yuv4_read_close(AVFormatContext *s)
  333. {
  334. return 0;
  335. }
  336. static int yuv4_probe(AVProbeData *pd)
  337. {
  338. /* check file header */
  339. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
  340. return AVPROBE_SCORE_MAX;
  341. else
  342. return 0;
  343. }
  344. #ifdef CONFIG_YUV4MPEGPIPE_DEMUXER
  345. AVInputFormat yuv4mpegpipe_demuxer = {
  346. "yuv4mpegpipe",
  347. "YUV4MPEG pipe format",
  348. sizeof(struct frame_attributes),
  349. yuv4_probe,
  350. yuv4_read_header,
  351. yuv4_read_packet,
  352. yuv4_read_close,
  353. .extensions = "y4m"
  354. };
  355. #endif